Dynamic state

This commit is contained in:
lash 2022-03-16 19:25:29 +00:00
parent 798262f00f
commit 57a9ea44ff
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 12 additions and 0 deletions

View File

@ -2,6 +2,7 @@
* Optional, pluggable verifier to protect state transition * Optional, pluggable verifier to protect state transition
* Change method for atomic simultaneous set and unset * Change method for atomic simultaneous set and unset
* Optionally allow undefined composite states * Optionally allow undefined composite states
* Dynamic bits
- 0.1.0 - 0.1.0
* Release version bump * Release version bump
- 0.0.19: - 0.0.19:

View File

@ -31,6 +31,7 @@ class State:
base_state_name = 'NEW' base_state_name = 'NEW'
def __init__(self, bits, logger=None, verifier=None, check_alias=True, event_callback=None): def __init__(self, bits, logger=None, verifier=None, check_alias=True, event_callback=None):
self.__initial_bits = bits
self.__bits = bits self.__bits = bits
self.__limit = (1 << bits) - 1 self.__limit = (1 << bits) - 1
self.__c = 0 self.__c = 0
@ -92,6 +93,9 @@ 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):
if self.__initial_bits == 0:
self.__bits += 1
self.__limit = (1 << self.__bits) - 1
if v > self.__limit: if v > self.__limit:
raise OverflowError(v) raise OverflowError(v)
return v return v

View File

@ -208,5 +208,12 @@ class TestState(unittest.TestCase):
self.assertEqual(events[3], states.BAZ) 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__': if __name__ == '__main__':
unittest.main() unittest.main()