You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
973 B

  1. """Parse arbitrary math equations in a safe way.
  2. Gratefully copied from https://stackoverflow.com/a/9558001
  3. """
  4. import ast
  5. import operator as op
  6. # supported operators
  7. operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul, ast.Div: op.truediv, ast.Pow: op.pow, ast.BitXor: op.xor, ast.USub: op.neg}
  8. def compute(expr):
  9. """Parse a mathematical expression and return the answer.
  10. >>> compute('2^6')
  11. 4
  12. >>> compute('2**6')
  13. 64
  14. >>> compute('1 + 2*3**(4^5) / (6 + -7)')
  15. -5.0
  16. """
  17. return _eval(ast.parse(expr, mode='eval').body)
  18. def _eval(node):
  19. if isinstance(node, ast.Num): # <number>
  20. return node.n
  21. elif isinstance(node, ast.BinOp): # <left> <operator> <right>
  22. return operators[type(node.op)](_eval(node.left), _eval(node.right))
  23. elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1
  24. return operators[type(node.op)](_eval(node.operand))
  25. else:
  26. raise TypeError(node)