Registry / data / py-expression-eval

py-expression-eval

JSON →
library0.3.14pypypi✓ verified 88d ago

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.

pip install py-expression-eval
INSTALL
IMPORT
SIG · PY-EXPRESSION-EVAL
P
py-expression-eval
datapythonv0.3.14
Install
1.6s avg
Import
7ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.3.14 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.003s · 17.9MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
✓ from py_expression_eval import Parser

This quickstart demonstrates how to initialize the parser, evaluate expressions with variables, use built-in functions, and leverage conditional operators. The `parse()` method returns an expression object which then can be evaluated with an optional dictionary of variables.

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}'")
Debug
Known issues
breakingIn 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.
fix
Test your existing expressions thoroughly after upgrading to 0.3.14. If issues arise, consider custom operator overriding or adjusting expressions.
affects: 0.3.14 and later
gotchaNew 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.
fix
Upgrade to version 0.3.10 or newer to use these operators. Alternatively, implement custom functions or operators for older versions.
affects: <0.3.10
gotchaSupport 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.
fix
Ensure you are on version 0.3.7 or newer for reliable `**` operator behavior. In older versions, use `pow(base, exponent)` if available, or stick to `^` (if defined differently in that version).
affects: <0.3.7
gotchaString 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.
fix
Upgrade to version 0.3.11 or newer for flexible string literal quoting. In older versions, ensure consistency with the default quote type (typically single quotes).
affects: <0.3.11
Errors
Common errors & fixes
NameError: name 'Parser' is not defined
The `Parser` class was not imported before use.
fix
Add `from py_expression_eval import Parser` at the top of your Python file.
py_expression_eval.parser.BadExpression: 'Undefined operator: in'
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.
fix
Upgrade 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: '
The expression string contains a syntax error, an unsupported character, or is malformed (e.g., unbalanced parentheses, unexpected operator placement).
fix
Review 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'
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()`.
fix
Ensure you first parse the expression: `parsed_expression = parser.parse(expression_string)` then call `parsed_expression.evaluate(variables)`.
Upgrade
Version history
0.3.14latest on PyPI · released Aug 26, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Resources