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}]

Print the Binary Tree Expression

After an expression tree is created using createExpressionTree() you can call the display() function on the root node of the tree to see the Binary Tree printed out.
Shoutout to this Stack Overflow Answer for the code!

from callableExpressionParser import createExpressionTree
expression = '[a]+2*[b]'
root = createExpressionTree(expression=expression)
root.display()
#  +_
# /  \
# a  *
#   / \
#   2 b

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.2.tar.gz (7.8 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.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for callableExpressionParser-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e8b685ee54ef89ce14b037aafd32013ee535bde7df92922f8790bc7a5f889d82
MD5 b6ea4867944d4d2295494ce21b900ae7
BLAKE2b-256 3f8b1efe75c7551e68504c162bb923f81ba5f55c7f7feb9e7da5a6b1b456c404

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for callableExpressionParser-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 51fae76016f3c35d0ab67da742aee33c2c355a774bb4c82aa63f60bad3933562
MD5 5e9e1c5d7408d03c5533770e57f935a8
BLAKE2b-256 db5d61942e57acb9c867a631d6a5dc5cc9e8240733b41831cdc3b8954dad4713

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