Add cumulative bit changes

This commit is contained in:
lash 2022-02-01 07:47:07 +00:00
parent e94f366d3c
commit 10f8617bab
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 78 additions and 3 deletions

View File

@ -1,3 +1,5 @@
- 0.0.8
* Add single bit transition to aliases
- 0.0.7
* Add content for keys
- 0.0.6

View File

@ -1,6 +1,6 @@
[metadata]
name = shep
version = 0.0.7
version = 0.0.8
description = Multi-state key stores using bit masks
author = Louis Holbrook
author_email = dev@holbrook.no

View File

@ -172,7 +172,11 @@ class State:
if new_state == None:
raise StateInvalid(to_state)
current_state_list = self.__keys.get(current_state)
self.__move(key, current_state, to_state)
def __move(self, key, from_state, to_state):
current_state_list = self.__keys.get(from_state)
if current_state_list == None:
raise StateCorruptionError(current_state)
@ -186,12 +190,46 @@ class State:
current_state_list.pop(idx)
def set(self, key, or_state):
if not self.__is_pure(or_state):
raise ValueError('can only apply using single bit states')
current_state = self.__keys_reverse.get(key)
if current_state == None:
raise StateItemNotFound(key)
to_state = current_state | or_state
new_state = self.__reverse.get(to_state)
if new_state == None:
raise StateInvalid('resulting to state is unknown: {}'.format(to_state))
self.__move(key, current_state, to_state)
def unset(self, key, not_state):
if not self.__is_pure(not_state):
raise ValueError('can only apply using single bit states')
current_state = self.__keys_reverse.get(key)
if current_state == None:
raise StateItemNotFound(key)
to_state = current_state & (~not_state)
if to_state == current_state:
raise ValueError('invalid change for state {}: {}'.format(key, not_state))
new_state = self.__reverse.get(to_state)
if new_state == None:
raise StateInvalid('resulting to state is unknown: {}'.format(to_state))
self.__move(key, current_state, to_state)
def purge(self, key):
current_state = self.__keys_reverse.get(key)
if current_state == None:
raise StateItemNotFound(key)
del self.__keys_reverse[key]
current_state_list = self.__keys.get(current_state)
idx = self.__state_list_index(key, current_state_list)

View File

@ -82,5 +82,39 @@ class TestStateItems(unittest.TestCase):
self.assertEqual(v, 'bar')
def test_item_set(self):
item = b'foo'
self.states.put(item, self.states.FOO)
self.states.set(item, self.states.BAR)
self.assertEqual(self.states.state(item), self.states.PLUGH)
def test_item_set_invalid(self):
item = b'foo'
self.states.put(item, self.states.FOO)
with self.assertRaises(StateInvalid):
self.states.set(item, self.states.BAZ)
item = b'bar'
self.states.put(item, self.states.BAR)
with self.assertRaises(ValueError):
self.states.set(item, self.states.XYZZY)
def test_item_set_invalid(self):
item = b'foo'
self.states.put(item, self.states.XYZZY)
self.states.unset(item, self.states.BAZ)
self.assertEqual(self.states.state(item), self.states.BAR)
item = b'bar'
self.states.put(item, self.states.XYZZY)
with self.assertRaises(ValueError):
self.states.unset(item, self.states.PLUGH)
with self.assertRaises(ValueError):
self.states.unset(item, self.states.FOO) # bit not set
if __name__ == '__main__':
unittest.main()

View File

@ -74,6 +74,7 @@ class TestState(unittest.TestCase):
states.add('bar')
with self.assertRaises(StateInvalid):
states.alias('baz', 5)
if __name__ == '__main__':