Access to is_pure, numeric output of elements
This commit is contained in:
parent
ee6820ef60
commit
765d634d5c
@ -1,3 +1,8 @@
|
||||
- 0.2.9
|
||||
* Enable access to is_pure method
|
||||
* Numeric option for elements return value
|
||||
- 0.2.8
|
||||
* Remove sync on persist set
|
||||
- 0.2.7
|
||||
* Handle missing files in fs readdir list
|
||||
- 0.2.6
|
||||
|
@ -1,6 +1,6 @@
|
||||
[metadata]
|
||||
name = shep
|
||||
version = 0.2.7
|
||||
version = 0.2.9
|
||||
description = Multi-state key stores using bit masks
|
||||
author = Louis Holbrook
|
||||
author_email = dev@holbrook.no
|
||||
|
@ -69,7 +69,7 @@ class PersistedState(State):
|
||||
super(PersistedState, self).unset(key, or_state, allow_base=True)
|
||||
raise e
|
||||
|
||||
self.sync(to_state)
|
||||
#self.sync(to_state)
|
||||
|
||||
return to_state
|
||||
|
||||
|
@ -53,7 +53,7 @@ class State:
|
||||
|
||||
|
||||
# return true if v is a single-bit state
|
||||
def __is_pure(self, v):
|
||||
def is_pure(self, v):
|
||||
if v == 0:
|
||||
return True
|
||||
c = 1
|
||||
@ -139,7 +139,7 @@ class State:
|
||||
def __add_state_list(self, state, item):
|
||||
if self.__keys.get(state) == None:
|
||||
self.__keys[state] = []
|
||||
if not self.__is_pure(state) or state == 0:
|
||||
if not self.is_pure(state) or state == 0:
|
||||
self.__keys[state].append(item)
|
||||
c = 1
|
||||
for i in range(self.__bits):
|
||||
@ -196,7 +196,7 @@ class State:
|
||||
for a in args:
|
||||
a = self.__check_value_cursor(a)
|
||||
v = self.__check_limit(v | a, pure=False)
|
||||
if self.__is_pure(v):
|
||||
if self.is_pure(v):
|
||||
raise ValueError('use add to add pure values')
|
||||
return self.__set(k, v)
|
||||
|
||||
@ -232,7 +232,7 @@ class State:
|
||||
continue
|
||||
if pure:
|
||||
state = self.from_name(k)
|
||||
if not self.__is_pure(state):
|
||||
if not self.is_pure(state):
|
||||
continue
|
||||
if numeric:
|
||||
if state == None:
|
||||
@ -244,15 +244,22 @@ class State:
|
||||
return l
|
||||
|
||||
|
||||
def elements(self, v):
|
||||
def elements(self, v, numeric=False, as_string=True):
|
||||
r = []
|
||||
if v == None or v == 0:
|
||||
return self.base_state_name
|
||||
c = 1
|
||||
for i in range(self.__bits):
|
||||
if v & c > 0:
|
||||
r.append(self.name(c))
|
||||
if numeric:
|
||||
r.append(c)
|
||||
else:
|
||||
r.append(self.name(c))
|
||||
c <<= 1
|
||||
|
||||
if numeric or not as_string:
|
||||
return r
|
||||
|
||||
return '_' + '.'.join(r)
|
||||
|
||||
|
||||
@ -433,7 +440,7 @@ class State:
|
||||
:rtype: int
|
||||
:returns: Resulting state
|
||||
"""
|
||||
if not self.__is_pure(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)
|
||||
@ -463,7 +470,7 @@ class State:
|
||||
:rtype: int
|
||||
:returns: Resulting state
|
||||
"""
|
||||
if not self.__is_pure(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)
|
||||
@ -579,7 +586,7 @@ class State:
|
||||
state = self.__keys_reverse.get(key)
|
||||
if state == None:
|
||||
raise StateItemNotFound(key)
|
||||
if not self.__is_pure(state):
|
||||
if not self.is_pure(state):
|
||||
raise StateInvalid('cannot run next on an alias state')
|
||||
|
||||
if state == 0:
|
||||
|
@ -270,5 +270,40 @@ class TestState(unittest.TestCase):
|
||||
states.state('xyzzy')
|
||||
|
||||
|
||||
def test_elements(self):
|
||||
states = State(2)
|
||||
states.add('foo')
|
||||
states.add('bar')
|
||||
states.alias('baz', states.FOO, states.BAR)
|
||||
|
||||
v = states.elements(states.BAZ)
|
||||
self.assertIn('FOO', v)
|
||||
self.assertIn('BAR', v)
|
||||
self.assertIsInstance(v, str)
|
||||
|
||||
v = states.elements(states.BAZ, numeric=True)
|
||||
self.assertIn(states.FOO, v)
|
||||
self.assertIn(states.BAR, v)
|
||||
|
||||
v = states.elements(states.BAZ, as_string=False)
|
||||
self.assertIn('FOO', v)
|
||||
self.assertIn('BAR', v)
|
||||
self.assertNotIsInstance(v, str)
|
||||
self.assertIsInstance(v, list)
|
||||
|
||||
|
||||
def test_pure(self):
|
||||
states = State(2)
|
||||
states.add('foo')
|
||||
states.add('bar')
|
||||
states.alias('baz', states.FOO, states.BAR)
|
||||
|
||||
v = states.is_pure(states.BAZ)
|
||||
self.assertFalse(v)
|
||||
|
||||
v = states.is_pure(states.FOO)
|
||||
self.assertTrue(v)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Loading…
Reference in New Issue
Block a user