shep

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

commit 57a9ea44ff34fc5a58aecfff1f8e86e7415748ca
parent 798262f00f3af1cd59320ae489274f1cf999d099
Author: lash <dev@holbrook.no>
Date:   Wed, 16 Mar 2022 19:25:29 +0000

Dynamic state

Diffstat:
MCHANGELOG | 1+
Mshep/state.py | 4++++
Mtests/test_state.py | 7+++++++
3 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -2,6 +2,7 @@ * Optional, pluggable verifier to protect state transition * Change method for atomic simultaneous set and unset * Optionally allow undefined composite states + * Dynamic bits - 0.1.0 * Release version bump - 0.0.19: diff --git a/shep/state.py b/shep/state.py @@ -31,6 +31,7 @@ class State: base_state_name = 'NEW' def __init__(self, bits, logger=None, verifier=None, check_alias=True, event_callback=None): + self.__initial_bits = bits self.__bits = bits self.__limit = (1 << bits) - 1 self.__c = 0 @@ -92,6 +93,9 @@ class State: # enforces state value within bit limit of instantiation def __check_limit(self, v): + if self.__initial_bits == 0: + self.__bits += 1 + self.__limit = (1 << self.__bits) - 1 if v > self.__limit: raise OverflowError(v) return v diff --git a/tests/test_state.py b/tests/test_state.py @@ -208,5 +208,12 @@ class TestState(unittest.TestCase): self.assertEqual(events[3], states.BAZ) + def test_dynamic(self): + states = State(0) + states.add('foo') + states.add('bar') + states.alias('baz', states.FOO | states.BAR) + + if __name__ == '__main__': unittest.main()