Add state peek
This commit is contained in:
parent
02be210eb4
commit
b9925a5038
@ -1,3 +1,5 @@
|
||||
- 0.0.13
|
||||
* Add next state peek
|
||||
- 0.0.12
|
||||
* Add path method
|
||||
- 0.0.11
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user