11from enum import Enum
22from typing import Any , List , Callable , Union
33
4-
54class 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