Skip to main content

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.

  1. 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.
  2. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

callableExpressionParser-1.0.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

callableExpressionParser-1.0.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file callableExpressionParser-1.0.1.tar.gz.

File metadata

File hashes

Hashes for callableExpressionParser-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a9804e04b222203ecf805599444d65f70ac39fa23dbba55682bb8ad72c7d5002
MD5 e27ff42fc1947642432f31cf0fbb579e
BLAKE2b-256 04d75755bc04b3e9cc2d5a23ccd8d785172c720d536089ef41f50bbbd20f98e3

See more details on using hashes here.

File details

Details for the file callableExpressionParser-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for callableExpressionParser-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ab96b86cbb94fe098340f395d42cc01edaa091ce69d98494c1e5859f2861bcd0
MD5 00b3772c147480eec38322d10ff70c88
BLAKE2b-256 3ed8ed8cffde49d8435e858df23754baa66b447d46a7c5626edb4e31d8e1dbcd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page