Skip to main content

math_eval

Safe evaluation of strings as math expressions

Features

  • Written entirely in Python.
  • Read strings representing math expressions or functions of any number of variables, and return a scalar or function as appropriate.
  • Supports all logical and arithmetic operators, as well as arbitrary functions of one or two variables.
  • Easy to add new functions of one or two variables.
  • safe_compute adds an extra layer of safety by creating functions that refuse non-numeric inputs.
  • Equation class that can be extended independently of compute() and safe_compute().

How to use

  • Install Python 3.6 or newer.

  • Install

    # or PyPI
    pip install math_eval
    
  • Use in situations where you need safe evaluation of strings as math expressions

    >>> from math_eval import compute, safe_compute
    >>> fun = safe_compute("x*y")
    >>> fun('a', 3)
    Traceback (most recent call last):
    ...
    ValueError: Functions produced by compute() with safe = True do not accept non-numeric arguments.
    >>> fun(3.4, 3)
    10.2
    >>> fun2 = compute("str(z) + x*y")
    >>> fun2('a', 3, 3.5)
    '3.5aaa'
    >>> print(fun2.__doc__)
    str(z) + x*y
    Args are positional-only in the order ['x', 'y', 'z'].
    >>> compute("(3*4.5**2 >= 17) | 1/-5 == 3")
    True
    

WARNING safe_compute() is not safe if arbitrary ufunctions are added, and compute() is probably unsafe in countless ways I haven't thought of as it is.

For instance, if the chr and eval ufunctions were introduced, even safe_compute() could be used to evaluate artibrary Python expressions by translating integers into strings, and evaluating those strings with the normal Python interpreter.

I (Mark J. Olson) am not knowledgeable about computer security, so think carefully about possible workarounds for safe_compute's safety features before deploying it in a safety-sensitive setting.

Contributing

Be sure to read the [contribution guidelines]

(https://github.com/molsonkiko/math_eval/blob/main/CONTRIBUTING.md).

More information

TODO: add something to show test coverage of code (Coverage URL does not exist)

Coverage

Release files for math-eval 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for math-eval 0.2.4
File Size Uploaded
math_eval-0.2.4.tar.gz 23.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for math-eval 0.2.4
File Interpreter ABI Platform
math_eval-0.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 46.2 kB

Release files / math_eval-0.2.4.tar.gz

Download URL math_eval-0.2.4.tar.gz
Size 23.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5b5103e70708b81298710f8fd761ac4c6f047aa5e7a7766c11920b6b7cd74f36
BLAKE2b-256 checksum
How to use checksums
35698c9ad234b1e2e49b165414a1e6acda6d6cd01086864c0b9543e7729e9c74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

Release files / math_eval-0.2.4-py3-none-any.whl

Download URL math_eval-0.2.4-py3-none-any.whl
Size 22.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7924a9fbd0959d6c606d4194a4751b26bdf135dfb54ccd61db565305ea72f1c
BLAKE2b-256 checksum
How to use checksums
40e4ff37b8508debd0199144ac38d161ff9a02f38e46f393c75647afaaba78ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.2.4 This release

2 release files

0.2.3

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page