Skip to main content

Minimal, robust Python library for any math expressions

Project description

expreval logo

Expreval, A Minimal, robust Python library for any math expressions


coverage
Changelog

Minimal. Single function. No dependencies. You give a string with a numeric expression; it returns a float.

Install

pip install expreval

Quick use

from expreval import evaluate
evaluate("sin(pi/2) + log(e)")  # 2.0

CLI:

expreval "sin(pi/4)**2"

What works now

  • Numbers: int / float literals
  • Operators: + - * / % ** and unary + -
  • Grouping: ( )
  • Names: pi, e, any function from the standard math module (sin, cos, sqrt, log, ...)
  • Simple function calls with positional arguments only

That is all - for now.

Safety (current scope)

Only a hand‑written walk over Python's ast for a very small subset. No attribute access, no imports, no keywords, no assignments, no lambdas, no comprehensions. If it's not listed above it should raise an error.

API

evaluate(expression: str) -> float

Returns a float (even if you pass an int literal). Raises standard Python exceptions (NameError, TypeError, etc.) for invalid input.

Examples

evaluate("2*(3+4)-5/2")        # 11.5
evaluate("sin(pi/6)**2")       # 0.249999... (floating point)
evaluate("sqrt(2)**2")         # 2.000000...

Why

Original itch: certain very large numeric expressions (or results) caused numexpr to raise errors in our workflow. For simple scalar math that shouldn't fail, we just needed a tiny, predictable evaluator that:

  • Has zero heavy dependencies
  • Doesn't optimize or chunk arrays (so no surprise shape / size limits)
  • Always returns a plain Python float for valid math
  • Is easy to audit (a short AST walk) and extend later if truly needed

So expreval exists to reliably handle those "big result" cases where bringing in numexpr (and hitting its internal limits) was overkill. If you only need quick scalar math, this keeps it boring and dependable.

Contributor Guide

uv venv
uv sync

# make changes and then run these 

# to create coverage logo and changelogs
.\scripts\update.sh

# (Optional)to bump version for release
uv run scripts\bump_version.py <X.Y.Z>

License

MIT. See LICENSE.

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

expreval-1.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

expreval-1.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: expreval-1.1.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for expreval-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ce4bcc462973e888349ac8e37976d7c0d65deab6a71254821077962c11da696e
MD5 215f55d2b54197976d69d03fbbb4c782
BLAKE2b-256 99dd1be1d763d8bd0fa06e89419d2728993d347be3b8945f00edadabb0b7c2ba

See more details on using hashes here.

File details

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

File metadata

  • Download URL: expreval-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for expreval-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2e5d58f6683e1876980fc663b3abb65a5064c88b0d7b258f861536323bfa01b
MD5 1bd7b7775a4df15634d8051e92101c07
BLAKE2b-256 f8924813c5e18a6cf9606857b39da66432afcae33179fd3c1b91018b5c77b85e

See more details on using hashes here.

Supported by

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