diff options
Diffstat (limited to 'libbe/storage/util')
-rw-r--r-- | libbe/storage/util/properties.py | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/libbe/storage/util/properties.py b/libbe/storage/util/properties.py index 4aa3ce6..ad9d06c 100644 --- a/libbe/storage/util/properties.py +++ b/libbe/storage/util/properties.py @@ -42,10 +42,15 @@ import copy import types import libbe -if libbe.TESTING == True: +if libbe.TESTING: import unittest +# https://stackoverflow.com/a/56719588/164233 +def cmp(a, b): + return (int(a) > int(b)) - (int(a) < int(b)) + + class ValueCheckError (ValueError): def __init__(self, name, value, allowed): action = "in" # some list of allowed values @@ -103,7 +108,7 @@ def local_property(name, null=None, mutable_null=False): def _fget(self): if fget is not None: fget(self) - if mutable_null == True: + if mutable_null: ret_null = copy.deepcopy(null) else: ret_null = null @@ -203,7 +208,7 @@ def defaulting_property(default=None, null=None, def _fget(self): value = fget(self) if value == null: - if mutable_default == True: + if mutable_default: return copy.deepcopy(default) else: return default @@ -380,7 +385,7 @@ def change_hook_property(hook, mutable=False, default=None): fset = funcs.get("fset") name = funcs.get("name", "<unknown>") def _fget(self, new_value=None, from_fset=False): # only used if mutable == True - if from_fset == True: + if from_fset: value = new_value # compare new value with cached else: value = fget(self) # compare current value with cached @@ -388,26 +393,26 @@ def change_hook_property(hook, mutable=False, default=None): # there has been a change, cache new value old_value = _get_cached_mutable_property(self, "change hook property", name, default) _set_cached_mutable_property(self, "change hook property", name, value) - if from_fset == True: # return previously cached value + if from_fset: # return previously cached value value = old_value else: # the value changed while we weren't looking hook(self, old_value, value) return value def _fset(self, value): - if mutable == True: # get cached previous value + if mutable: # get cached previous value old_value = _fget(self, new_value=value, from_fset=True) else: old_value = fget(self) fset(self, value) if value != old_value: hook(self, old_value, value) - if mutable == True: + if mutable: funcs["fget"] = _fget funcs["fset"] = _fset return funcs return decorator -if libbe.TESTING == True: +if libbe.TESTING: class DecoratorTests(unittest.TestCase): def testLocalDoc(self): class Test(object): |