Browse Source

Eval

pull/35/head
Jure Šorn 5 years ago
parent
commit
fc67ec2ac8
1 changed files with 3 additions and 3 deletions
  1. 6
      README.md

6
README.md

@ -1405,7 +1405,7 @@ import ast
from ast import Num, BinOp, UnaryOp from ast import Num, BinOp, UnaryOp
import operator as op import operator as op
legal_operators = {ast.Add: op.add,
LEGAL_OPERATORS = {ast.Add: op.add,
ast.Sub: op.sub, ast.Sub: op.sub,
ast.Mult: op.mul, ast.Mult: op.mul,
ast.Div: op.truediv, ast.Div: op.truediv,
@ -1424,9 +1424,9 @@ def eval_node(node):
if node_type not in [BinOp, UnaryOp]: if node_type not in [BinOp, UnaryOp]:
raise TypeError(node) raise TypeError(node)
operator_type = type(node.op) operator_type = type(node.op)
if operator_type not in legal_operators:
if operator_type not in LEGAL_OPERATORS:
raise TypeError(f'Illegal operator {node.op}') raise TypeError(f'Illegal operator {node.op}')
operator = legal_operators[operator_type]
operator = LEGAL_OPERATORS[operator_type]
if node_type == BinOp: if node_type == BinOp:
left, right = eval_node(node.left), eval_node(node.right) left, right = eval_node(node.left), eval_node(node.right)
return operator(left, right) return operator(left, right)

Loading…
Cancel
Save