expr.py
A safe and simple math evaluator for Python, built with rply.
View on PyPI
Expr.py is a simple but safe math expression evaluator made for Python.
It can evaluate pretty advanced math concepts without crashing your computer.
Made using rply
Features
- Fully object oriented
- Completely typed for intellisense
- Protection against DoS attacks
- Customizable and extendable
- Follows order of operations
- Floating point precision
Getting started
You should install expr.py using pip:
$ pip install -U expr.py
Here is a simple program to get started:
import expr
if __name__ == '__main__':
expression = '6 + 5 * 2'
print(expr.evaluate(expression)) # 16
What does expr.py support?
Basic operations
The following operations are supported by expr.py:
+(addition)-(subtraction)*(multiplication)/(division)//(floor division)%(modulo)^(exponentation)!(factorial)
Variables
The most basic way of defining variables is by
passing in the variables kwarg into the evaluator.
expr.evaluate('2x', variables={'x': 2}) # 4
You can also let the input define variables:
expr.evaluate('x = 5')
expr.evaluate('6 + x') # 11
There are by default, 2 predefined constants. (pi and e)
Functions [WIP]
You can define functions through the builtins kwarg:
def f(x):
return x + 1
expr.evaluate('f(5)', builtins={'f': f}) # 6
You can also define functions via input:
expr.evaluate('f(x) = 2x')
expr.evaluate('f(3)') # 6
There are a few builtin functions:
sqrtcbrtloglog10lnradsincostanasinacosatan
Grouping
This concept is pretty simple, anything in parentheses will be evaluated before anything outside of them.
expr.evaluate('5 * 6 + 2') # 32
expr.evaluate('5 * (6 + 2)') # 40
States
You can create different states so that each can store their own variables and functions independently from others.
To do this, use expr.create_state:
state = expr.create_state()
print(state.evaluate('0.1 + 0.2')) # 0.3
Note: All parameters belong in create_state rather than in evaluate for states.
Again, variables and functions are independent from each other:
state1 = expr.create_state()
state1.evaluate('x = 1')
state2 = expr.create_state()
state2.evaluate('x') # error (x is not defined)
state1.evaluate('x') # 1
Changelog
v0.2
This update mainly brings bug fixes from v0.1.
What's new?
- You can now pass in custom classes into
Parser.evaluate - Constants are now precise to around 30 places.
- New constants (
phi,tau)
More precise builtin functions
v0.2 changes the way some builtin functions are processed for boosts on both performance and precision.
sqrtnow usesDecimal.sqrtlog10now usesDecimal.log10lnnow usesDecimal.lncbrtnow usesinput ** expr.one_thirdsinnow usesexpr.sincosnow usesexpr.cos
Bug fixes
- Fixed unary minus interfering with implicit multiplication.
- in v0.1:
5-3=-15 - in v0.2:
5-3=2
- in v0.1:
Miscellaneous
- Many functions now have positional-only arguments for slight performance boosts
- This drops support for Python 3.7
- Messages retrieved from
ParsingError.friendlyare now much more descriptive.
v0.3
What's new?
- Unary plus is now supported (E.g.
+5) - Scientific notation is now supported (E.g.
4E-2)- To reduce conflics, 'E' must be captialized.
This means that2e9would evaluate to2 * e * 9, for example.
- To reduce conflics, 'E' must be captialized.
- The
clskwarg is now supported inexpr.evaluate
Bug fixes
- Catch
OverflowErrorin theexpr.Overflowparsing error. - Fix invalid typings with
Callable
Metadata
Release files for expr.py 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| expr.py-0.3.0.tar.gz | 11.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| expr.py-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.5 kB
Release files / expr.py-0.3.0.tar.gz
| Download URL | expr.py-0.3.0.tar.gz |
|---|---|
| Size | 11.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b7de20de494791afb9e9b1f11d5455c93399fe8a6b32359dbd766aa232ea1c66
|
|
BLAKE2b-256 checksum How to use checksums |
1f4c7cb08a69aa9233cdb4975ba462a1faf09c0852c1e3b40213b0ef79e4c33b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
|
Release files / expr.py-0.3.0-py3-none-any.whl
| Download URL | expr.py-0.3.0-py3-none-any.whl |
|---|---|
| Size | 10.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e45314288f5b955c0984dc377febc88cbc0bdc9b66124daf8ff5f9db731fdcae
|
|
BLAKE2b-256 checksum How to use checksums |
f3cd4b461263626f4fea4f365dd27693b7c1ec26de67d1f4820d5e06659f196d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
|