Add mask
This commit is contained in:
parent
6680b897b3
commit
2beeb4c725
@ -5,6 +5,7 @@
|
|||||||
* Dynamic bits
|
* Dynamic bits
|
||||||
* Optional binary contents
|
* Optional binary contents
|
||||||
* Sync all if no state passed as argument
|
* Sync all if no state passed as argument
|
||||||
|
* Mask method for client-side state manipulation
|
||||||
- 0.1.0
|
- 0.1.0
|
||||||
* Release version bump
|
* Release version bump
|
||||||
- 0.0.19:
|
- 0.0.19:
|
||||||
|
@ -92,7 +92,8 @@ class State:
|
|||||||
|
|
||||||
|
|
||||||
# enforces state value within bit limit of instantiation
|
# enforces state value within bit limit of instantiation
|
||||||
def __check_limit(self, v):
|
def __check_limit(self, v, pure=True):
|
||||||
|
if pure:
|
||||||
if self.__initial_bits == 0:
|
if self.__initial_bits == 0:
|
||||||
self.__bits += 1
|
self.__bits += 1
|
||||||
self.__limit = (1 << self.__bits) - 1
|
self.__limit = (1 << self.__bits) - 1
|
||||||
@ -197,7 +198,7 @@ class State:
|
|||||||
v = 0
|
v = 0
|
||||||
for a in args:
|
for a in args:
|
||||||
a = self.__check_value_cursor(a)
|
a = self.__check_value_cursor(a)
|
||||||
v = self.__check_limit(v | a)
|
v = self.__check_limit(v | a, pure=False)
|
||||||
if self.__is_pure(v):
|
if self.__is_pure(v):
|
||||||
raise ValueError('use add to add pure values')
|
raise ValueError('use add to add pure values')
|
||||||
self.__set(k, v)
|
self.__set(k, v)
|
||||||
@ -593,3 +594,11 @@ class State:
|
|||||||
|
|
||||||
def register_modify(self, key):
|
def register_modify(self, key):
|
||||||
self.modified_last[key] = datetime.datetime.now().timestamp()
|
self.modified_last[key] = datetime.datetime.now().timestamp()
|
||||||
|
|
||||||
|
|
||||||
|
def mask(self, key, states):
|
||||||
|
statemask = self.__limit + 1
|
||||||
|
statemask |= states
|
||||||
|
statemask = ~statemask
|
||||||
|
statemask &= self.__limit
|
||||||
|
return statemask
|
||||||
|
@ -216,5 +216,26 @@ class TestState(unittest.TestCase):
|
|||||||
states.alias('baz', states.FOO | states.BAR)
|
states.alias('baz', states.FOO | states.BAR)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_mask(self):
|
||||||
|
states = State(3)
|
||||||
|
states.add('foo')
|
||||||
|
states.add('bar')
|
||||||
|
states.add('baz')
|
||||||
|
states.alias('all', states.FOO | states.BAR | states.BAZ)
|
||||||
|
mask = states.mask('xyzzy', states.FOO | states.BAZ)
|
||||||
|
self.assertEqual(mask, states.BAR)
|
||||||
|
|
||||||
|
|
||||||
|
def test_mask_dynamic(self):
|
||||||
|
states = State(0)
|
||||||
|
states.add('foo')
|
||||||
|
states.add('bar')
|
||||||
|
states.add('baz')
|
||||||
|
states.alias('all', states.FOO | states.BAR | states.BAZ)
|
||||||
|
mask = states.mask('xyzzy', states.FOO | states.BAZ)
|
||||||
|
self.assertEqual(mask, states.BAR)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user