Add matcher with optional pure only
This commit is contained in:
parent
9981ff277f
commit
97f4431c00
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
__pycache__
|
||||
*.egg-info
|
||||
*.pyc
|
||||
*.d/
|
@ -1,4 +1,5 @@
|
||||
- 0.0.2
|
||||
* Fix rename module name errors
|
||||
* Add listing and matcher
|
||||
- 0.0.1
|
||||
* Add bases states and aliases
|
||||
|
14
run_tests.sh
Normal file
14
run_tests.sh
Normal file
@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -a
|
||||
set -e
|
||||
set -x
|
||||
default_pythonpath=$PYTHONPATH:.
|
||||
export PYTHONPATH=${default_pythonpath:-.}
|
||||
>&2 echo using pythonpath $PYTHONPATH
|
||||
for f in `ls tests/*.py`; do
|
||||
python $f
|
||||
done
|
||||
set +x
|
||||
set +e
|
||||
set +a
|
@ -1,6 +1,5 @@
|
||||
# standard imports
|
||||
import enum
|
||||
import logging
|
||||
|
||||
# local imports
|
||||
from shep.error import (
|
||||
@ -8,19 +7,16 @@ from shep.error import (
|
||||
StateInvalid,
|
||||
)
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class State:
|
||||
|
||||
def __init__(self, bits):
|
||||
def __init__(self, bits, logger=None, store=None):
|
||||
self.__bits = bits
|
||||
self.__limit = (1 << bits) - 1
|
||||
self.__c = 0
|
||||
self.__reverse = {}
|
||||
|
||||
def __store(self):
|
||||
pass
|
||||
self.__logger = logger
|
||||
self.__store = store
|
||||
|
||||
|
||||
def __is_pure(self, v):
|
||||
@ -47,7 +43,7 @@ class State:
|
||||
def __check_valid(self, v):
|
||||
v = int(v)
|
||||
if self.__reverse.get(v):
|
||||
raise StateValueExists(v)
|
||||
raise StateExists(v)
|
||||
return v
|
||||
|
||||
|
||||
@ -98,17 +94,20 @@ class State:
|
||||
return l
|
||||
|
||||
|
||||
def have(self, v):
|
||||
def match(self, v, pure=False):
|
||||
r = []
|
||||
m = self.__reverse.get(k)
|
||||
if m != None:
|
||||
r.append(m)
|
||||
if not pure:
|
||||
m = self.__reverse.get(v)
|
||||
if m != None:
|
||||
r.append(m)
|
||||
c = 1
|
||||
for i in range(self.__bits):
|
||||
if v & c > 0:
|
||||
self.__check_value_cursor(c)
|
||||
k = self.__reverse[c]
|
||||
r.append(k)
|
||||
try:
|
||||
k = self.__reverse[c]
|
||||
r.append(k)
|
||||
except KeyError:
|
||||
pass
|
||||
c <<= 1
|
||||
|
||||
return r
|
||||
|
@ -29,5 +29,26 @@ class TestStateReport(unittest.TestCase):
|
||||
getattr(self.states, k)
|
||||
|
||||
|
||||
def test_match(self):
|
||||
r = self.states.match(self.states.FOO)
|
||||
self.assertEqual(getattr(self.states, r[0]), 1)
|
||||
|
||||
|
||||
def test_match_alias(self):
|
||||
self.states.alias('xyzzy', self.states.FOO | self.states.BAZ)
|
||||
r = self.states.match(self.states.XYZZY)
|
||||
for k in ['FOO', 'BAZ', 'XYZZY']:
|
||||
self.assertIn(k, r)
|
||||
self.assertNotIn('BAR', r)
|
||||
|
||||
|
||||
def test_match_alias_pure(self):
|
||||
self.states.alias('xyzzy', self.states.FOO | self.states.BAZ)
|
||||
r = self.states.match(self.states.XYZZY, pure=True)
|
||||
for k in ['FOO', 'BAZ']:
|
||||
self.assertIn(k, r)
|
||||
self.assertNotIn('XYZZY', r)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Loading…
Reference in New Issue
Block a user