import operator
class Expression:
def __eq__(self, other):
return Operator("==", other)
def __lt__(self, other):
return Operator("<", other)
def __le__(self, other):
return Operator("<=", other)
def __gt__(self, other):
return Operator(">", other)
def __ge__(self, other):
return Operator(">=", other)
class Operator:
def __init__(self, operator_, rhs):
self._operator = operator_
self._rhs = rhs
self._operator_map = {
"==": operator.eq,
"<": operator.lt,
"<=": operator.le,
">": operator.gt,
">=": operator.ge
}
@property
def value(self):
return self._rhs
@property
def operator(self):
return self._operator_map[self._operator]
def pick_range(data, left_exp, right_exp):
lvalue = left_exp.value
rvalue = right_exp.value
loperator = left_exp.operator
roperator = right_exp.operator
return [item for item in data if loperator(item, lvalue) and roperator(item, rvalue)]
exp = Expression()
data = [1, 2, 3, 4, 5, 6, 7]
print(pick_range(data, 1 < exp, exp < 6))
def pick_range1(data, exp):
value = exp.value
operator = exp.operator
return [item for item in data if operator(item, value)]
exp = Expression()
data = [1, 2, 3, 4, 5, 6, 7]
print(pick_range1(data, 1 < exp))
|