Skip to content

Commit c104ecf

Browse files
committed
refactor: pull method up into interface
1 parent 3d81299 commit c104ecf

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/json_as_db/Condition.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from enum import Enum
22
from typing import Any, List, Callable, Union
33

4-
54
class Operator(Enum):
65
LESS_THAN = '<'
76
LESS_EQUAL = '<='
@@ -59,7 +58,15 @@ def _invert_operator(op: Operator):
5958
raise NotImplementedError()
6059

6160

62-
class Condition:
61+
class Comparator:
62+
def __call__(self, item: dict) -> bool:
63+
return self.evaluate(item)
64+
65+
def evaluate(self, other: dict) -> bool:
66+
pass
67+
68+
69+
class Condition(Comparator):
6370
key: str
6471
value: Any
6572
operator: Operator
@@ -77,9 +84,6 @@ def __repr__(self) -> str:
7784
]
7885
return f"Condition({', '.join(s)})"
7986

80-
def __call__(self, item: dict) -> bool:
81-
return self._evaluate_by_key(item)
82-
8387
def copy(self) -> 'Condition':
8488
return Condition(key=self.key, value=self.value, operator=self.operator)
8589

@@ -100,17 +104,14 @@ def __or__(self, other: 'Condition') -> 'Conditions':
100104
def __ror__(self, other: 'Condition') -> 'Conditions':
101105
return Conditions(lvalue=other, rvalue=self, operator=Operator.OR)
102106

103-
def _evaluate_by_key(self, item: dict) -> bool:
107+
def evaluate(self, item: dict) -> bool:
104108
key = str(self.key)
105109
if not key in item:
106110
return None
107-
return self.evaluate(item[key])
108-
109-
def evaluate(self, other: Any) -> bool:
110-
return _compare(other, self.value, self.operator)
111+
return _compare(item[key], self.value, self.operator)
111112

112113

113-
class Conditions:
114+
class Conditions(Comparator):
114115
_left: Union[Condition, 'Conditions']
115116
_right: Union[Condition, 'Conditions']
116117
_oper: Operator
@@ -129,9 +130,6 @@ def evaluate(self, other: dict) -> bool:
129130
l, r, op = self._left, self._right, self._oper
130131
return _compare(l(other), r(other), op)
131132

132-
def __call__(self, item: dict) -> bool:
133-
return self.evaluate(item)
134-
135133
def copy(self) -> 'Conditions':
136134
return Conditions(
137135
lvalue=self._left,

0 commit comments

Comments
 (0)