shep

Multi-state key stores using bit masks for python3
git clone git://git.defalsify.org/shep.git
Log | Files | Refs | LICENSE

commit b9925a5038efd117f70ea7199c69f8338452578c
parent 02be210eb4359408a6d8db4fb1d7dfaff679eaa3
Author: lash <dev@holbrook.no>
Date:   Wed,  2 Feb 2022 07:59:52 +0000

Add state peek

Diffstat:
MCHANGELOG | 2++
Msetup.cfg | 2+-
Mshep/state.py | 27+++++++++++++++++++++++++--
Mtests/test_state.py | 16++++++++++++++++
4 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,5 @@ +- 0.0.13 + * Add next state peek - 0.0.12 * Add path method - 0.0.11 diff --git a/setup.cfg b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = shep -version = 0.0.12 +version = 0.0.13 description = Multi-state key stores using bit masks author = Louis Holbrook author_email = dev@holbrook.no diff --git a/shep/state.py b/shep/state.py @@ -22,6 +22,8 @@ class State: def __is_pure(self, v): + if v == 0: + return True c = 1 for i in range(self.__bits): if c & v > 0: @@ -34,6 +36,7 @@ class State: if not k.isalpha(): raise ValueError('only alpha') + def __check_name(self, k): self.__check_name_valid(k) @@ -53,10 +56,13 @@ class State: return v - def __check_value(self, v): - v = self.__check_valid(v) + def __check_limit(self, v): if v > self.__limit: raise OverflowError(v) + + def __check_value(self, v): + v = self.__check_valid(v) + self.__check_limit(v) return v @@ -261,3 +267,20 @@ class State: if self.__reverse.get(state) == None: raise StateInvalid(state) return self.__keys[state] + + + def peek(self, key): + state = self.__keys_reverse.get(key) + if state == None: + raise StateItemNotFound(key) + if not self.__is_pure(state): + raise StateInvalid('cannot run next on an alias state') + + if state == 0: + state = 1 + else: + state <<= 1 + if state > self.__c: + raise StateInvalid('unknown state {}'.format(state)) + + return state diff --git a/tests/test_state.py b/tests/test_state.py @@ -76,6 +76,22 @@ class TestState(unittest.TestCase): states.alias('baz', 5) + def test_peek(self): + states = State(3) + states.add('foo') + states.add('bar') + + states.put('abcd') + self.assertEqual(states.peek('abcd'), states.FOO) + + states.move('abcd', states.FOO) + self.assertEqual(states.peek('abcd'), states.BAR) + + states.move('abcd', states.BAR) + + with self.assertRaises(StateInvalid): + self.assertEqual(states.peek('abcd')) + if __name__ == '__main__': unittest.main()