From 6a9a63de17c17190b1e987b195b5cbf92cd1cec8 Mon Sep 17 00:00:00 2001 From: Diogo Silva Date: Wed, 4 Dec 2019 08:30:33 -0300 Subject: [PATCH] initial commit --- .gitignore | 2 + README.md | 0 py_matching_pattern/__init__.py | 83 +++++++++++++++++++++++++++++++++ setup.py | 9 ++++ tests/__init__.py | 1 + tests/test_core.py | 82 ++++++++++++++++++++++++++++++++ 6 files changed, 177 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 py_matching_pattern/__init__.py create mode 100644 setup.py create mode 100644 tests/__init__.py create mode 100644 tests/test_core.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a6a362 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +dist +__pycache__ diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/py_matching_pattern/__init__.py b/py_matching_pattern/__init__.py new file mode 100644 index 0000000..17d4aca --- /dev/null +++ b/py_matching_pattern/__init__.py @@ -0,0 +1,83 @@ + +from threading import Lock +from copy import deepcopy +from uuid import uuid4 + +class InvalidKeySize(Exception): + pass + +class InvalidKeyCount(Exception): + pass + +class KeyNotFound(Exception): + pass + +class PatternMatchStore: + + default =None + + def __init__(self,keysize=1): + if keysize < 1: + raise InvalidKeySize + + self.__db = {} + self.__stage = {} + self.__keysize=keysize + self.__lock = Lock() + + self.default=uuid4() + + def put(self,keys=[],value=None): + if(len(keys) 0: + return self.__get(keys=self.__default_filled(keys=keys,n=n)) + + break + return None diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..b97bedb --- /dev/null +++ b/setup.py @@ -0,0 +1,9 @@ +from setuptools import setup +import os + +setup( + name="py_matching_pattern", + version=os.getenv("VERSION"), + packages=["py_matching_pattern"], + url='https://github.com/diogok/py_matching_pattern', +) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..875ac51 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +#pass diff --git a/tests/test_core.py b/tests/test_core.py new file mode 100644 index 0000000..e3f209e --- /dev/null +++ b/tests/test_core.py @@ -0,0 +1,82 @@ +import unittest +import py_matching_pattern as core + +class CoreTest(unittest.TestCase): + + def test_basic(self): + mm = core.PatternMatchStore(keysize=3) + _=mm.default + + mm.put(keys=["a","b","c"],value=1) + mm.put(keys=["a","b","b"],value=2) + mm.put(keys=["a","b",_],value=3) + mm.put(keys=["a",_,_],value=4) + mm.put(keys=["a",None,"c"],value=5) + + mm.commit() + + self.assertEqual(1,mm.get(keys=["a","b","c"])) + self.assertEqual(2,mm.get(keys=["a","b","b"])) + self.assertEqual(3,mm.get(keys=["a","b","d"])) + self.assertEqual(4,mm.get(keys=["a","c","d"])) + self.assertEqual(5,mm.get(keys=["a",None,"c"])) + + def test_basic_unordered(self): + mm = core.PatternMatchStore(keysize=3) + d=mm.default + + mm.put(keys=["a","b",d],value=3) + mm.put(keys=["a",'b',"b"],value=2) + mm.put(keys=["a",d,d],value=4) + mm.put(keys=["a","b","c"],value=1) + + mm.commit() + + self.assertEqual(1,mm.get(keys=["a","b","c"])) + self.assertEqual(2,mm.get(keys=["a","b","b"])) + self.assertEqual(3,mm.get(keys=["a","b","d"])) + self.assertEqual(4,mm.get(keys=["a","c","d"])) + + def test_basic_clean(self): + mm = core.PatternMatchStore(keysize=3) + + mm.put(keys=["a","b","c"],value=1) + mm.commit() + self.assertEqual(1,mm.get(keys=["a","b","c"])) + + mm.clean() + mm.commit() + with self.assertRaises(core.KeyNotFound): + mm.get(keys=["a","b","c"]) + + def test_uncommited(self): + mm = core.PatternMatchStore(keysize=3) + + mm.put(keys=["a","b","c"],value=1) + with self.assertRaises(core.KeyNotFound): + mm.get(keys=["a","b","c"]) + + def test_uncommited_clean(self): + mm = core.PatternMatchStore(keysize=3) + + mm.put(keys=["a","b","c"],value=1) + mm.commit() + self.assertEqual(1,mm.get(keys=["a","b","c"])) + + mm.clean() + self.assertEqual(1,mm.get(keys=["a","b","c"])) + + def test_validate_all_leaf_have_default(self): + pass + + def test_validate_conflicts(self): + pass + + def test_invalid_keycount(self): + pass + + def test_invalid_keysize(self): + pass + + def test_value_not_found(self): + pass \ No newline at end of file