summaryrefslogtreecommitdiff
path: root/taskflow/utils/misc.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2015-03-02 14:20:14 -0800
committerJoshua Harlow <harlowja@gmail.com>2015-03-02 23:05:46 -0800
commit3c806b1d6a6aee7825e022bbdda499fe1adca547 (patch)
tree623bfc05b6e6fdd5ece8ca445969767ae7a3bb5f /taskflow/utils/misc.py
parentf391702f0e63351ef77685569db7635b363fbda5 (diff)
downloadtaskflow-3c806b1d6a6aee7825e022bbdda499fe1adca547.tar.gz
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
Diffstat (limited to 'taskflow/utils/misc.py')
-rw-r--r--taskflow/utils/misc.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/taskflow/utils/misc.py b/taskflow/utils/misc.py
index 6c5c9de..ec9eda8 100644
--- a/taskflow/utils/misc.py
+++ b/taskflow/utils/misc.py
@@ -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: