Tool for safe (or less safe) evaluation of strings as math expressions
Project description
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
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
math_eval-0.1.2.tar.gz
(22.8 kB
view hashes)
Built Distribution
math_eval-0.1.2-py3-none-any.whl
(22.3 kB
view hashes)
Close
Hashes for math_eval-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf94d11ba69ad90e8c0ef54da168e16d364c3c3eb4e1570ec8f53da567ef1428 |
|
MD5 | 2e73724086d06b5a0e082940386d2fd0 |
|
BLAKE2b-256 | 0f7276cb89222a0eb86133bf71d029bce5e660e23b24a915716887e8d80baf17 |