pip install pyexpr
- Simple evaluation
assert pyexpr.evaluate('1 + 2 * (3 - 4) **2') == 3
- With functions
assert pyexpr.evaluate('1 + 2 * (3 - 4) ** sqrt(4)', builtins=dict(sqrt=math.sqrt)) == 3
- With variables
assert pyexpr.evaluate('1 + 2 * (3 - 4) ** X', variables=dict(X=4)) == 3
You can find more examples in test.py