Limited evaluator
Project description
Leval
A limited expression evaluator
A little more suited for dynamic usage than ast.literal_eval()
while remaining as safe as the functions you pass in.
Under the hood, it uses the ast
module to parse the expression,
then walks the AST in Python to evaluate the result. You can
also specify a depth limit for the complexity of the expression,
as well as a time limit for the evaluation.
Example usage
Simple API
For many use cases, the simple_eval()
function is sufficient.
You can specify a depth limit and a time limit, and optional mappings
of variables and functions.
The values
mapping can also be keyed by a tuple of strings, which
is what attribute accesses are folded to.
Operations are generally limited to numbers only in the simple API.
from leval.simple import simple_eval
assert simple_eval('1 + 2') == 3
assert simple_eval('x < -80 or x > 125 or x == 85', values={'x': 85})
assert simple_eval('abs(x) > 80', values={'x': -85}, functions={'abs': abs})
assert simple_eval('x.y.z + 8', values={('x', 'y', 'z'): 34}) == 42
Advanced API
Under the hood, simple_eval
simply
- initializes an evaluation universe, which defines the functions, variables and operations available
- creates an Evaluator to evaluate the expression with the given universe
Both of these classes are designed to be easily subclassable. There are examples
in the test_leval.py
file.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file leval-1.3.0.tar.gz
.
File metadata
- Download URL: leval-1.3.0.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ebee62d7178fa1bea8c7300a0af34690a7049a05bc6dcf4f0209dd221ff6e51 |
|
MD5 | 196dbf276fc327e15e2059d9d58873bc |
|
BLAKE2b-256 | 52793ae360244a3c36fb6f27693be431991ca5a59afb8e37815cb7c44367e13e |
File details
Details for the file leval-1.3.0-py3-none-any.whl
.
File metadata
- Download URL: leval-1.3.0-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f22b89cabd00919806d4bff57d1c2003083bf5200e88335cd329eef708ad22d |
|
MD5 | 3490000fbd2a1770c4eb8091742c7b1d |
|
BLAKE2b-256 | acfecf1d9b559830a92c7cd2dafa0d156df9e1ee2f07cf286dca7bd9abd603b1 |