Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- def eval_str(formula, varname, value):
- """
- Evaluate the formula for a given value.
- The only allowed functions to evaluate are contained in
- the python math module.
- Arguments:
- formula : string to be evaluated via eval(formula)
- varname : argument to be substituted
- value : value to substitute with varname
- Returns:
- float, evaluated formula
- """
- #Obtain list of allowable math functions
- allowable_func = {}
- for fn in dir(math):
- if "_" not in fn:
- allowable_func[fn] = getattr(math, fn)
- return eval(formula, allowable_func, {varname: value})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement