make arithmetic expression case insensitive
internally, we use downcased names
This commit is contained in:
+4
-2
@@ -211,10 +211,12 @@ _ALLOWED_CMPOPS = {
|
||||
####################################
|
||||
class SafeExpression:
|
||||
def __init__(self, expression: str):
|
||||
self.expression = expression
|
||||
self.tree = ast.parse(expression, mode="eval")
|
||||
self.expression = expression.lower()
|
||||
self.tree = ast.parse(self.expression, mode="eval")
|
||||
|
||||
def evaluate(self, variables: Mapping[str, int | float]) -> int | float | bool:
|
||||
# normalize to downcased variable names
|
||||
variables = {k.lower(): v for k,v in variables.items()}
|
||||
return self._eval(self.tree.body, variables)
|
||||
|
||||
def _eval(self, node: ast.AST, variables: Mapping[str, int | float]):
|
||||
|
||||
Reference in New Issue
Block a user