commit 97f4431c005f9be10b7a1eb7f41685ca2e31e6c5
parent 9981ff277fb9f5b01771176250267574782d59f9
Author: lash <dev@holbrook.no>
Date: Mon, 31 Jan 2022 09:55:56 +0000
Add matcher with optional pure only
Diffstat:
5 files changed, 54 insertions(+), 15 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,4 @@
+__pycache__
+*.egg-info
+*.pyc
+*.d/
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,4 +1,5 @@
- 0.0.2
* Fix rename module name errors
+ * Add listing and matcher
- 0.0.1
* Add bases states and aliases
diff --git a/run_tests.sh b/run_tests.sh
@@ -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
diff --git a/shep/state.py b/shep/state.py
@@ -1,6 +1,5 @@
# standard imports
import enum
-import logging
# local imports
from shep.error import (
@@ -8,21 +7,18 @@ 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 = {}
+ self.__logger = logger
+ self.__store = store
- def __store(self):
- pass
-
def __is_pure(self, v):
c = 1
for i in range(self.__bits):
@@ -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
diff --git a/tests/test_report.py b/tests/test_report.py
@@ -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()