frappy.lib.mathparser: add relational operators
This commit is contained in:
@@ -37,6 +37,13 @@ class MathParser:
|
|||||||
ast.Div: op.truediv,
|
ast.Div: op.truediv,
|
||||||
ast.Pow: op.pow,
|
ast.Pow: op.pow,
|
||||||
ast.FloorDiv: op.floordiv,
|
ast.FloorDiv: op.floordiv,
|
||||||
|
ast.Lt: op.lt,
|
||||||
|
ast.Gt: op.gt,
|
||||||
|
ast.LtE: op.le,
|
||||||
|
ast.GtE: op.ge,
|
||||||
|
ast.Eq: op.eq,
|
||||||
|
ast.NotEq: op.ne,
|
||||||
|
ast.Not: op.not_,
|
||||||
ast.USub: op.neg,
|
ast.USub: op.neg,
|
||||||
ast.UAdd: lambda a:a}
|
ast.UAdd: lambda a:a}
|
||||||
|
|
||||||
@@ -74,6 +81,15 @@ class MathParser:
|
|||||||
if isinstance(node, ast.BinOp): # evaluate binary operations
|
if isinstance(node, ast.BinOp): # evaluate binary operations
|
||||||
method = self._operators2method[type(node.op)]
|
method = self._operators2method[type(node.op)]
|
||||||
return method( self.eval_(node.left), self.eval_(node.right))
|
return method( self.eval_(node.left), self.eval_(node.right))
|
||||||
|
if isinstance(node, ast.Compare): # evaluate binary operations
|
||||||
|
left = self.eval_(node.left)
|
||||||
|
for oper, value in zip(node.ops, node.comparators):
|
||||||
|
method = self._operators2method[type(oper)]
|
||||||
|
right = self.eval_(value)
|
||||||
|
if not method(left, right):
|
||||||
|
return False
|
||||||
|
left = right
|
||||||
|
return True
|
||||||
if isinstance(node, ast.UnaryOp): # handle operators
|
if isinstance(node, ast.UnaryOp): # handle operators
|
||||||
method = self._operators2method[type(node.op)]
|
method = self._operators2method[type(node.op)]
|
||||||
return method( self.eval_(node.operand) )
|
return method( self.eval_(node.operand) )
|
||||||
|
|||||||
Reference in New Issue
Block a user