Skip to main content

Limited evaluator

Project description

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 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

  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.

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

leval-1.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

leval-1.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: leval-1.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for leval-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f089e72d8b3b959a0c6155fc9cfb067a995bcc1dd854d9c3c5f63cb4a76cfaf2
MD5 baee9390c618855d08463828abced77c
BLAKE2b-256 74e4b86660079f0a5e2f5314e044bf9dfb1a9f0c5c939d5c373ce9d923c2bd0b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: leval-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for leval-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e9a36a78da79ae4b426640d5090d54c4d325b39aac35c29010efdecea5a46e90
MD5 f496a4e6128feb36bca49be269fd30cf
BLAKE2b-256 6fa5dae2b76f0d4e0eddd2ada611440d079aed6ffff820b25b4ae5eed09e8922

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page