Skip to main content

Leval

License Coverage CI PyPI

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 depth, time and expression size limits, 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

  1. initializes an evaluation universe, which defines the functions, variables and operations available
  2. 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.

Security

leval walks the AST itself and never uses getattr, subscripting, or calls to anything other than the functions you explicitly register by name.

The evaluator is only as safe as what you put into the universe, so keep the following in mind:

  • You own the functions and values. Any function you register can be called with attacker-controlled arguments, and operators (==, <, in, arithmetic, …) invoke the corresponding dunder methods on the values you provide. Do not register anything with side effects (open, eval, importers, ORM objects, …) or pass in objects whose comparisons/arithmetic do something dangerous.
  • max_time is cooperative, not preemptive. It is only checked between AST node visits, so a single slow function call or a single expensive operation runs to completion regardless of the limit. Enforce hard timeouts out of process if you need them.
  • Exceptions are not all wrapped. Errors from operators and registered functions (e.g. ZeroDivisionError, TypeError, ValueError) propagate as their native types, not only as leval.excs.EvaluatorError. Catch broadly around evaluation.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

leval-1.4.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

leval-1.4.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file leval-1.4.0.tar.gz.

File metadata

  • Download URL: leval-1.4.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for leval-1.4.0.tar.gz
Algorithm Hash digest
SHA256 859083230a3a1f334d58bd266a4109e7504c88be26eacffe2e77ae291f1f35d0
MD5 cfc60acb12211e5099e7f23d51598df1
BLAKE2b-256 1e9c6308a7b248eba9b45755b55a1de22ef5e16b18e40397e0ab9a5f360d32b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for leval-1.4.0.tar.gz:

Publisher: ci.yml on valohai/leval

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file leval-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: leval-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for leval-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 51a4614732673daf64f9e274c2b7b7e2bb8d85d978d3e6262cad4cf67c297fab
MD5 37ed8349914aa3c7f69b1ff11438f2ab
BLAKE2b-256 21d6b7eebc0f0f2b8b7ae80e9a23b6b2f6d72ed4702557a47a6f681aad320df2

See more details on using hashes here.

Provenance

The following attestation bundles were made for leval-1.4.0-py3-none-any.whl:

Publisher: ci.yml on valohai/leval

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 files

1.3.0

2 files

1.2.0

2 files

1.1.1

2 files

1.1.0

2 files

1.0.0

2 files

0.2.1

2 files

0.2.0

1 file

0.1.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page