Add matcher with optional pure only

This commit is contained in:
lash 2022-01-31 09:55:56 +00:00
parent 9981ff277f
commit 97f4431c00
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 54 additions and 15 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
__pycache__
*.egg-info
*.pyc
*.d/

View File

@ -1,4 +1,5 @@
- 0.0.2 - 0.0.2
* Fix rename module name errors * Fix rename module name errors
* Add listing and matcher
- 0.0.1 - 0.0.1
* Add bases states and aliases * Add bases states and aliases

14
run_tests.sh Normal file
View 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

View File

@ -1,6 +1,5 @@
# standard imports # standard imports
import enum import enum
import logging
# local imports # local imports
from shep.error import ( from shep.error import (
@ -8,21 +7,18 @@ from shep.error import (
StateInvalid, StateInvalid,
) )
logg = logging.getLogger(__name__)
class State: class State:
def __init__(self, bits): def __init__(self, bits, logger=None, store=None):
self.__bits = bits self.__bits = bits
self.__limit = (1 << bits) - 1 self.__limit = (1 << bits) - 1
self.__c = 0 self.__c = 0
self.__reverse = {} self.__reverse = {}
self.__logger = logger
self.__store = store
def __store(self):
pass
def __is_pure(self, v): def __is_pure(self, v):
c = 1 c = 1
for i in range(self.__bits): for i in range(self.__bits):
@ -47,7 +43,7 @@ class State:
def __check_valid(self, v): def __check_valid(self, v):
v = int(v) v = int(v)
if self.__reverse.get(v): if self.__reverse.get(v):
raise StateValueExists(v) raise StateExists(v)
return v return v
@ -98,17 +94,20 @@ class State:
return l return l
def have(self, v): def match(self, v, pure=False):
r = [] r = []
m = self.__reverse.get(k) if not pure:
if m != None: m = self.__reverse.get(v)
r.append(m) if m != None:
r.append(m)
c = 1 c = 1
for i in range(self.__bits): for i in range(self.__bits):
if v & c > 0: if v & c > 0:
self.__check_value_cursor(c) try:
k = self.__reverse[c] k = self.__reverse[c]
r.append(k) r.append(k)
except KeyError:
pass
c <<= 1 c <<= 1
return r return r

View File

@ -29,5 +29,26 @@ class TestStateReport(unittest.TestCase):
getattr(self.states, k) 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__': if __name__ == '__main__':
unittest.main() unittest.main()