Skip to content

Commit 3ead402

Browse files
committed
refactor: reorganize module directories
1 parent d241b31 commit 3ead402

File tree

7 files changed

+85
-68
lines changed

7 files changed

+85
-68
lines changed

docs/source/modules/index.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ Modules
44
Database
55
--------
66

7-
.. automodule:: json_as_db.Database
7+
.. automodule:: json_as_db.core.database
8+
:members:
9+
:undoc-members:
10+
:show-inheritance:
11+
12+
13+
Matcher
14+
--------
15+
16+
.. automodule:: json_as_db.core.matcher
817
:members:
918
:undoc-members:
1019
:show-inheritance:

src/json_as_db/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .Condition import Condition, Conditions, Key
2-
from .Database import Database
1+
from .core.database import Database
2+
from .core.matcher import Condition, Conditions, Key
33

44
__version__ = '0.2.4'
55

File renamed without changes.

src/json_as_db/core/__init__.py

Whitespace-only changes.

src/json_as_db/Database.py renamed to src/json_as_db/core/database.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from datetime import datetime
77
from typing import Any, Union, List, Callable
88

9-
from ._constants import package_name
10-
from ._utils import override_dict, from_maybe_list, return_maybe
9+
from ..constants import package_name
10+
from .._utils import override_dict, from_maybe_list, return_maybe
1111

1212
__all__ = [
1313
'Database'

src/json_as_db/Condition.py renamed to src/json_as_db/core/matcher.py

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,10 @@
1-
from enum import Enum
2-
from typing import Any, List, Callable, Union
3-
4-
class Operator(Enum):
5-
LESS_THAN = '<'
6-
LESS_EQUAL = '<='
7-
EQUAL = '=='
8-
NOT_EQUAL = '!='
9-
GREATER_THAN = '>'
10-
GREATER_EQUAL = '>='
11-
AND = '&'
12-
OR = '|'
13-
14-
15-
def _compare(a: Any, b: Any, op: Operator):
16-
if op is Operator.LESS_THAN:
17-
return a < b
18-
elif op is Operator.LESS_EQUAL:
19-
return a <= b
20-
elif op is Operator.EQUAL:
21-
return a == b
22-
elif op is Operator.NOT_EQUAL:
23-
return a != b
24-
elif op is Operator.GREATER_THAN:
25-
return a > b
26-
elif op is Operator.GREATER_EQUAL:
27-
return a >= b
28-
elif op is Operator.AND:
29-
if a != None and b != None:
30-
return a & b
31-
return False
32-
elif op is Operator.OR:
33-
if a != None and b != None:
34-
return a | b
35-
return False
36-
else:
37-
raise NotImplementedError()
38-
39-
40-
def _invert_operator(op: Operator):
41-
if op is Operator.LESS_THAN: # <
42-
return Operator.GREATER_EQUAL # ~(<) is (>=)
43-
elif op is Operator.LESS_EQUAL: # <=
44-
return Operator.GREATER_THAN # ~(<=) is (>)
45-
elif op is Operator.EQUAL: # ==
46-
return Operator.NOT_EQUAL # ~(==) is (!=)
47-
elif op is Operator.NOT_EQUAL: # !=
48-
return Operator.EQUAL # ~(!=) is (==)
49-
elif op is Operator.GREATER_THAN: # >
50-
return Operator.LESS_EQUAL # ~(>) is (<=)
51-
elif op is Operator.GREATER_EQUAL: # >=
52-
return Operator.LESS_THAN # ~(>=) is (<)
53-
elif op is Operator.AND: # and
54-
return Operator.OR # ~(and) is (or)
55-
elif op is Operator.OR: # or
56-
return Operator.AND # ~(or) is (and)
57-
else:
58-
raise NotImplementedError()
1+
from typing import Any, Union
2+
3+
from .operator import (
4+
Operator,
5+
compare,
6+
invert_operator
7+
)
598

609

6110
class Comparator:
@@ -89,7 +38,7 @@ def copy(self) -> 'Condition':
8938

9039
def __invert__(self) -> 'Condition':
9140
copy = self.copy()
92-
copy.operator = _invert_operator(self.operator)
41+
copy.operator = invert_operator(self.operator)
9342
return copy
9443

9544
def __and__(self, other: 'Condition') -> 'Conditions':
@@ -108,7 +57,7 @@ def evaluate(self, item: dict) -> bool:
10857
key = str(self.key)
10958
if not key in item:
11059
return None
111-
return _compare(item[key], self.value, self.operator)
60+
return compare(item[key], self.value, self.operator)
11261

11362

11463
class Conditions(Comparator):
@@ -128,7 +77,7 @@ def __init__(
12877

12978
def evaluate(self, other: dict) -> bool:
13079
l, r, op = self._left, self._right, self._oper
131-
return _compare(l(other), r(other), op)
80+
return compare(l(other), r(other), op)
13281

13382
def copy(self) -> 'Conditions':
13483
return Conditions(
@@ -142,7 +91,7 @@ def __invert__(self) -> 'Conditions':
14291
For example, when you call `~(a & b)` it means `not (a and b)`.
14392
"""
14493
inst = self.copy()
145-
inst._oper = _invert_operator(inst._oper)
94+
inst._oper = invert_operator(inst._oper)
14695
return inst
14796

14897
def invert(self) -> 'Conditions':
@@ -153,7 +102,7 @@ def invert(self) -> 'Conditions':
153102
return Conditions(
154103
lvalue=~self._left,
155104
rvalue=~self._right,
156-
operator=_invert_operator(self._oper),
105+
operator=invert_operator(self._oper),
157106
)
158107

159108
def __and__(self, other: Union[Condition, 'Conditions']) -> 'Conditions':

src/json_as_db/core/operator.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from enum import Enum
2+
from typing import Any
3+
4+
5+
class Operator(Enum):
6+
LESS_THAN = '<'
7+
LESS_EQUAL = '<='
8+
EQUAL = '=='
9+
NOT_EQUAL = '!='
10+
GREATER_THAN = '>'
11+
GREATER_EQUAL = '>='
12+
AND = '&'
13+
OR = '|'
14+
15+
16+
def compare(a: Any, b: Any, op: Operator):
17+
if op is Operator.LESS_THAN:
18+
return a < b
19+
elif op is Operator.LESS_EQUAL:
20+
return a <= b
21+
elif op is Operator.EQUAL:
22+
return a == b
23+
elif op is Operator.NOT_EQUAL:
24+
return a != b
25+
elif op is Operator.GREATER_THAN:
26+
return a > b
27+
elif op is Operator.GREATER_EQUAL:
28+
return a >= b
29+
elif op is Operator.AND:
30+
if a != None and b != None:
31+
return a & b
32+
return False
33+
elif op is Operator.OR:
34+
if a != None and b != None:
35+
return a | b
36+
return False
37+
else:
38+
raise NotImplementedError()
39+
40+
41+
def invert_operator(op: Operator):
42+
if op is Operator.LESS_THAN: # <
43+
return Operator.GREATER_EQUAL # ~(<) is (>=)
44+
elif op is Operator.LESS_EQUAL: # <=
45+
return Operator.GREATER_THAN # ~(<=) is (>)
46+
elif op is Operator.EQUAL: # ==
47+
return Operator.NOT_EQUAL # ~(==) is (!=)
48+
elif op is Operator.NOT_EQUAL: # !=
49+
return Operator.EQUAL # ~(!=) is (==)
50+
elif op is Operator.GREATER_THAN: # >
51+
return Operator.LESS_EQUAL # ~(>) is (<=)
52+
elif op is Operator.GREATER_EQUAL: # >=
53+
return Operator.LESS_THAN # ~(>=) is (<)
54+
elif op is Operator.AND: # and
55+
return Operator.OR # ~(and) is (or)
56+
elif op is Operator.OR: # or
57+
return Operator.AND # ~(or) is (and)
58+
else:
59+
raise NotImplementedError()

0 commit comments

Comments
 (0)