shep

Multi-state key stores using bit masks for python3
git clone git://git.defalsify.org/shep.git
Log | Files | Refs | LICENSE

commit 9981ff277fb9f5b01771176250267574782d59f9
parent 6768bd326d156db90b6c8b4a52b842a500ae7432
Author: lash <dev@holbrook.no>
Date:   Mon, 31 Jan 2022 09:33:21 +0000

Fix faulty module name in test, enforce cursor on have

Diffstat:
MCHANGELOG | 2++
Dschiz/state.py | 102-------------------------------------------------------------------------------
Asetup.cfg | 27+++++++++++++++++++++++++++
Asetup.py | 3+++
Rschiz/__init__.py -> shep/__init__.py | 0
Rschiz/error.py -> shep/error.py | 0
Ashep/state.py | 114+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/test_report.py | 4++--
Mtests/test_state.py | 6+++---
9 files changed, 151 insertions(+), 107 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,2 +1,4 @@ +- 0.0.2 + * Fix rename module name errors - 0.0.1 * Add bases states and aliases diff --git a/schiz/state.py b/schiz/state.py @@ -1,102 +0,0 @@ -# standard imports -import enum -import logging - -# local imports -from schiz.error import ( - StateExists, - StateInvalid, - ) - -logg = logging.getLogger(__name__) - - -class State: - - def __init__(self, bits): - self.__bits = bits - self.__limit = (1 << bits) - 1 - self.__c = 0 - self.__reverse = {} - - def __store(self): - pass - - - def __is_pure(self, v): - c = 1 - for i in range(self.__bits): - if c & v > 0: - break - c <<= 1 - return c == v - - - def __check_name(self, k): - if not k.isalpha(): - raise ValueError('only alpha') - k = k.upper() - try: - getattr(self, k) - raise StateExists(k) - except AttributeError: - pass - return k - - - def __check_cover(self, v): - z = 0 - c = 1 - for i in range(self.__bits): - if c & v > 0: - if self.__reverse.get(c) == None: - raise StateInvalid(v) - c <<= 1 - return c == v - - - def __check_value(self, v): - v = int(v) - if self.__reverse.get(v): - raise StateValueExists(v) - if v > self.__limit: - raise OverflowError(v) - return v - - - def __check(self, k, v): - k = self.__check_name(k) - v = self.__check_value(v) - return (k, v,) - - - def __set(self, k, v): - setattr(self, k, v) - self.__reverse[v] = k - self.__c += 1 - - - def add(self, k): - v = 1 << self.__c - (k, v) = self.__check(k, v) - self.__set(k, v) - - - def alias(self, k, v): - (k, v) = self.__check(k, v) - if self.__is_pure(v): - raise ValueError('use add to add pure values') - self.__check_cover(v) - self.__set(k, v) - - - def all(self): - l = [] - for k in dir(self): - if k[0] == '_': - continue - if k.upper() != k: - continue - l.append(k) - l.sort() - return l diff --git a/setup.cfg b/setup.cfg @@ -0,0 +1,27 @@ +[metadata] +name = shep +version = 0.0.1 +description = Multi-state key value stores using bitmaskings +author = Louis Holbrook +author_email = dev@holbrook.no +url = https://git.defalsify.org/python-shep +keywords = + dict + queue +classifiers = + Programming Language :: Python :: 3 + Operating System :: OS Independent + Development Status :: 3 - Alpha + Topic :: Software Development :: Libraries + Intended Audience :: Developers + License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+) +# Topic :: Blockchain :: EVM +license = GPL3 +licence_files = + LICENSE + +[options] +include_package_data = True +python_requires = >= 3.6 +packages = + shep diff --git a/setup.py b/setup.py @@ -0,0 +1,3 @@ +from setuptools import setup + +setup() diff --git a/schiz/__init__.py b/shep/__init__.py diff --git a/schiz/error.py b/shep/error.py diff --git a/shep/state.py b/shep/state.py @@ -0,0 +1,114 @@ +# standard imports +import enum +import logging + +# local imports +from shep.error import ( + StateExists, + StateInvalid, + ) + +logg = logging.getLogger(__name__) + + +class State: + + def __init__(self, bits): + self.__bits = bits + self.__limit = (1 << bits) - 1 + self.__c = 0 + self.__reverse = {} + + def __store(self): + pass + + + def __is_pure(self, v): + c = 1 + for i in range(self.__bits): + if c & v > 0: + break + c <<= 1 + return c == v + + + def __check_name(self, k): + if not k.isalpha(): + raise ValueError('only alpha') + k = k.upper() + try: + getattr(self, k) + raise StateExists(k) + except AttributeError: + pass + return k + + + def __check_valid(self, v): + v = int(v) + if self.__reverse.get(v): + raise StateValueExists(v) + return v + + + def __check_value(self, v): + v = self.__check_valid(v) + if v > self.__limit: + raise OverflowError(v) + return v + + + def __check_value_cursor(self, v): + v = self.__check_valid(v) + if v > 1 << self.__c: + raise StateInvalid(v) + return v + + + def __set(self, k, v): + setattr(self, k, v) + self.__reverse[v] = k + self.__c += 1 + + + def add(self, k): + v = 1 << self.__c + k = self.__check_name(k) + v = self.__check_value(v) + self.__set(k, v) + + + def alias(self, k, v): + k = self.__check_name(k) + v = self.__check_value_cursor(v) + if self.__is_pure(v): + raise ValueError('use add to add pure values') + self.__set(k, v) + + + def all(self): + l = [] + for k in dir(self): + if k[0] == '_': + continue + if k.upper() != k: + continue + l.append(k) + l.sort() + return l + + + def have(self, v): + r = [] + m = self.__reverse.get(k) + 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) + c <<= 1 + + return r diff --git a/tests/test_report.py b/tests/test_report.py @@ -2,8 +2,8 @@ import unittest # local imports -from schiz import State -from schiz.error import ( +from shep import State +from shep.error import ( StateExists, StateInvalid, ) diff --git a/tests/test_state.py b/tests/test_state.py @@ -2,8 +2,8 @@ import unittest # local imports -from schiz import State -from schiz.error import ( +from shep import State +from shep.error import ( StateExists, StateInvalid, ) @@ -65,7 +65,7 @@ class TestState(unittest.TestCase): def test_alias_nopure(self): states = State(3) with self.assertRaises(ValueError): - states.alias('foo', 4) + states.alias('foo', 1) def test_alias_cover(self):