Math Expression Parser that works with [ + - * / ^ // ! ] operators, constants [ pi ], functions [ exp, log, ln ], strings ["testString"], and [variables] out of the box and can be expanded to cover more use cases!
Project description
mathExpressionParser
Math Expression Parser that works with [ + - * / ^ // ! ] operators, constants [ pi ], functions [ exp, log, ln ], strings ["testString"], and [variables] out of the box and can be expanded to cover more use cases!
Motivation
A lot of the math expression parsers I found online didn't quite cover all the use cases I needed as well as were hard to expand upon. With that, I created my own!
How To Install
pip install callableExpressionParser
How To Use
Regardless of the language, you'll primarily use 2 lines of code in order.
- createExpressionTree - This function takes the list of nodes from extractNodes or an expression string and parses its nodes and creates a binary tree. Each parent node is an operator or a function, and each child node is a number, constant, or token group. This function returns the base node of the tree which you can call exec() on to prepare the tree.
- The result of the base Node's exec() function is a function that takes in an optional dictionary argument. If any variables are defined in the expression, the variables will be replaced with the dictionary key-value where the key is the variable's name.
What's great about this approach is say you have a table represented as a list of dictionaries. If you'd want to add a calculated column that is some expression based on other row values, you can create the new column easily.
Example in python
from callableExpressionParser import createExpressionTree
tbl = [{'a': 1, 'b': 2},{'a': 3, 'b': 4},{'a': 5, 'b': 6}]
expression = '[a]+2*[b]'
func = createExpressionTree(expression=expression).exec()
tbl = [{**r, 'c': func(**r)} for r in tbl]
print(tbl) # [{'a': 1, 'b': 2, 'c': 5.0},{'a': 3, 'b': 4, 'c': 11.0},{'a': 5, 'b': 6, 'c': 17.0}]
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file callableExpressionParser-1.0.1.tar.gz.
File metadata
- Download URL: callableExpressionParser-1.0.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9804e04b222203ecf805599444d65f70ac39fa23dbba55682bb8ad72c7d5002
|
|
| MD5 |
e27ff42fc1947642432f31cf0fbb579e
|
|
| BLAKE2b-256 |
04d75755bc04b3e9cc2d5a23ccd8d785172c720d536089ef41f50bbbd20f98e3
|
File details
Details for the file callableExpressionParser-1.0.1-py3-none-any.whl.
File metadata
- Download URL: callableExpressionParser-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab96b86cbb94fe098340f395d42cc01edaa091ce69d98494c1e5859f2861bcd0
|
|
| MD5 |
00b3772c147480eec38322d10ff70c88
|
|
| BLAKE2b-256 |
3ed8ed8cffde49d8435e858df23754baa66b447d46a7c5626edb4e31d8e1dbcd
|