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

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-0.1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

expreval-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for expreval-0.1.0.tar.gz
Algorithm Hash digest
SHA256 24dc943b1665dfaa8da4958e9627f772e8f9c6db69a9f2742f857d4d03078564
MD5 155ea551c23bf4b231b768610e3c18db
BLAKE2b-256 0fd1f574cbf2592d13d30fb0d45d262d92fba98102da1e0e9cb614c36bd42330

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for expreval-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a1081128e1da6a17358108617e0aef372b8f8dafbdb8815262c166902e532e3
MD5 59de2ff7109549e4af4d8d28ffb4480d
BLAKE2b-256 5b1ab7ab7972657878fefeeca956b1d3d1748d63ab803c9a539bd2490bb18429

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