Add a frozen checking decorator

Since quite a few of the types check for being
frozen and disallow mutations on there instances
we can take advantage of a common decorator that
checks the frozen attribute and raises instead
of duplicating the same logic at the start of
the mutating methods.

Change-Id: I8c81a26d2d39bb9da4f68d64e07f67ac26ee0b08
This commit is contained in:
Joshua Harlow
2015-03-02 14:20:14 -08:00
committed by Joshua Harlow
parent f391702f0e
commit 3c806b1d6a
3 changed files with 28 additions and 8 deletions

View File

@@ -227,6 +227,23 @@ def look_for(haystack, needles, extractor=None):
return [needles[i] for (_hay_i, i) in sorted(matches)]
def disallow_when_frozen(excp_cls):
"""Frozen checking/raising method decorator."""
def decorator(f):
@six.wraps(f)
def wrapper(self, *args, **kwargs):
if self.frozen:
raise excp_cls()
else:
return f(self, *args, **kwargs)
return wrapper
return decorator
def clamp(value, minimum, maximum, on_clamped=None):
"""Clamps a value to ensure its >= minimum and <= maximum."""
if minimum > maximum: