Py-Expression-Eval
Py-expression-eval is a lightweight Python library designed for evaluating mathematical and logical expressions from strings. It supports variables, custom functions, and a wide range of operators, making it suitable for dynamic calculation needs. The current version is 0.3.14. Releases are typically infrequent, focused on bug fixes and minor feature enhancements.
Common errors
-
NameError: name 'Parser' is not defined
cause The `Parser` class was not imported before use.fixAdd `from py_expression_eval import Parser` at the top of your Python file. -
py_expression_eval.parser.BadExpression: 'Undefined operator: in'
cause You are attempting to use the `in` operator (or other new operators like `NOT`, `XOR`, `?:`) with an older version of `py-expression-eval` that does not support it.fixUpgrade your `py-expression-eval` library to version 0.3.10 or newer by running `pip install --upgrade py-expression-eval`. -
py_expression_eval.parser.BadExpression: 'Unexpected token: '
cause The expression string contains a syntax error, an unsupported character, or is malformed (e.g., unbalanced parentheses, unexpected operator placement).fixReview your expression string for typos, correct syntax, and ensure all operators and functions are valid for your `py-expression-eval` version. Use single quotes for string literals if you are on an older version (<0.3.11). -
AttributeError: 'str' object has no attribute 'evaluate'
cause You are calling `.evaluate()` directly on the string expression instead of first parsing it. The `evaluate` method belongs to the expression object returned by `parser.parse()`.fixEnsure you first parse the expression: `parsed_expression = parser.parse(expression_string)` then call `parsed_expression.evaluate(variables)`.
Warnings
- breaking In version 0.3.14, `math.pow` was swapped with `self.pow` internally. While usually backward compatible, custom implementations or reliance on specific `math.pow` behavior (e.g., for very large numbers or specific edge cases) might exhibit subtle differences.
- gotcha New operators like `IN`, `NOT`, `XOR`, and conditional (`? :`) were added in version 0.3.10. Attempting to use these operators in versions prior to 0.3.10 will result in a `BadExpression: 'Undefined operator'` error.
- gotcha Support for the `**` (power) operator was fixed and improved around version 0.3.7. Using `**` in much older versions might lead to incorrect parsing or evaluation, or it might not have been supported at all.
- gotcha String literal quote types became configurable from version 0.3.11. Prior to this, the parser might have been more restrictive (e.g., only allowing single quotes). Using double quotes in older versions could result in `BadExpression` errors.
Install
-
pip install py-expression-eval
Imports
- Parser
from py_expression_eval import Parser
Quickstart
from py_expression_eval import Parser
# Initialize the parser
parser = Parser()
# Evaluate a simple expression with variables
expression1 = "2 * x + y"
variables1 = {"x": 3, "y": 5}
result1 = parser.parse(expression1).evaluate(variables1)
print(f"'{expression1}' with {variables1} evaluates to: {result1}")
# Evaluate an expression with built-in functions
expression2 = "sqrt(16) + abs(-10)"
result2 = parser.parse(expression2).evaluate()
print(f"'{expression2}' evaluates to: {result2}")
# Evaluate an expression with a conditional operator (requires v0.3.10+ for 'in')
expression3 = "x in [1, 2, 3] ? 'present' : 'absent'"
variables3 = {"x": 2}
result3 = parser.parse(expression3).evaluate(variables3)
print(f"'{expression3}' with {variables3} evaluates to: '{result3}'")