Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. import math
  2. def eval_str(formula, varname, value):
  3. """
  4. Evaluate the formula for a given value.
  5. The only allowed functions to evaluate are contained in
  6. the python math module.
  7.  
  8. Arguments:
  9. formula : string to be evaluated via eval(formula)
  10. varname : argument to be substituted
  11. value : value to substitute with varname
  12. Returns:
  13. float, evaluated formula
  14. """
  15.  
  16. #Obtain list of allowable math functions
  17. allowable_func = {}
  18. for fn in dir(math):
  19. if "_" not in fn:
  20. allowable_func[fn] = getattr(math, fn)
  21.  
  22. return eval(formula, allowable_func, {varname: value})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement