strmath
A module for evaluating math expressions without eval(). Currently the module supports only simple math operations (eg. multiplication, subtraction, division, %) but in the future there will be functions support.
Installation
The module is available for installation from PyPI
$ pip install strmath
Basic Usage
from strmath import evaluate
result = evaluate("(90 + 2) // 4")
print(result)
Accuracy
As tests show, the library is 100% accurate with python native evaluation:
+----------------+--------+---------+-----+-----------+-------------+
| | Python | StrMath | PEE | Mathparse | InfixParser |
+----------------+--------+---------+-----+-----------+-------------+
| Failures | 0 | 0 | 61 | 150 | 91 |
| Failures (%) | 0% | 0% | 30% | 75% | 45% |
+----------------+--------+---------+-----+-----------+-------------+
In the test above, 198 randomly generated samples were submitted to Python eval() and several other parsing libraries, including strmath.
As you can see, the library has 0 failures and almost same speed with native python. You can see test implementation here.
Features
Currently Supported
- basic math operations (+, -, /, //, *, **, %) eg.
"2 + 5 * 2" --> 12 - float operations
- braces eg.
"(2 + 5) * 2" --> 20 - functions from
mathmodule and some builtins
Planned for Future
- custom functions registration
- correct
-operator as neg (eg.50+-30) - mathematical constants (i.e. PI)
Principle
- Tokenize expression (split it into operators and numbers)
- Apply operations order
- Build evaluation binary tree
- Evaluate the tree
License
This repository is licensed under MIT license. See LICENSE for details.
Metadata
Release files for strmath 1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| strmath-1.1.tar.gz | 6.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| strmath-1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.7 kB
Release files / strmath-1.1.tar.gz
| Download URL | strmath-1.1.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a4435842e18475798353675c9c431d849b770fde7d86e68801bda96c07ba20e5
|
|
BLAKE2b-256 checksum How to use checksums |
14f4fb15906eb1ba0de5c57da6a408641c6144cc0589d1a5469ad505b7a770ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.14 CPython/3.10.5 Linux/5.15.0-1014-azure
|
Release files / strmath-1.1-py3-none-any.whl
| Download URL | strmath-1.1-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aa6d231db0d9bb6814d0dc83589a855dedf03020054d45b48c8511cc1143e661
|
|
BLAKE2b-256 checksum How to use checksums |
132456578747a28770bb04911f5a33a5306c25b4853bed4d75d9887d5b1c7c30
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.14 CPython/3.10.5 Linux/5.15.0-1014-azure
|