Skip to main content

An infix-to-postfix expression parser for evaluating math expressions.

Project description

Tinypost logo

Tinypost

Tinypost is a tiny infix-to-postfix expression parser and interpreter for math expressions. Quickly evaluate math expressions quickly with just a few lines of code. Tinypost supports many of the functions in the python math module.

Features

  • No external dependencies
  • Fast performance
  • Abides by standard operator precedence
  • Implements many mathematical functions in the math module
  • Easily bind custom variables and functions
  • MIT License
  • Local stacks
  • No recursion

Install

Install easily using the following command.

pip install tinypost

Simple Example

The following code evaluates an expression at runtime.

import tinypost as tp
print(tp.eval_expr("3 + 12 / 4")) # Prints 6.0

Binding Variables

Setting and updating your own custom variables is simple.

import tinypost as tp

x = 5.5
tp.add_var("x", x)
print(tp.eval_expr("(x + 4) / 2"))) # Prints 4.75

x = 3.7
tp.update_var("x", x)
print(tp.eval_expr("(x + 4) / 2"))) # Prints 3.85

long_var_name = 5.5
tp.add_var("long_var_name", long_var_name)
print(tp.eval_expr("long_var_name * 3 / 6.23"))) # Prints 2.648...

Variable names must start with letter and can only contain letters, underscores, and numbers. You can also use multiple variables in a single expression.

Optimizing Variable Usage

If you find yourself evalulating the same expression over and over again but just updating the value of the variable, you can try the following:

import tinypost as tp

x = 3
tp.update_var("x", x)

expr = tp.compile_expr("x^2 + 4")

for i in range(3):
	x = i
	tp.update_var("x", i)
	print(tp.eval_compiled(expr), end=" ") # Prints 4 5 8

This will run faster than if you were to call tp.eval_expr() for each iteration of the loop.

Mathematical Functions

Tinypost support many mathematical functions, including abs, sqrt, sin, cos, tan, ln, acos, asin, atan, atan2, exp, log, sinh, cosh, tanh, max, and pow. When calling a function, make sure to put a pair of parentheses around its arguments, and if it has multiple arguments, separate each argument with a comma.

import tinypost as tp
expr = "max(sin(3 + 4 / 3.5), 0.6)"
print(tp.eval_expr(expr)) # Prints 0.6

If a function you would like to use is not supported by Tinypost or you would like to bind your own functions, you can do the following.

import tinypost as tp

def my_weird_sum(a, b):
    weird_num = 2.3551
    return (a / weird_num) ** 2 + b

# Functions cannot have optional parameters, they
# must have a constant number of arguments.
# Functions must return a singular int or float.

tp.add_func("my_weird_sum", my_weird_sum)
expr = "my_weird_sum(3, 9.2) + 3"
print(tp.eval_expr(expr)) # Prints 13.822...

Also, the constants pi and e are available for your use.

How This Works

Tinypost uses Dijkstra's Shunting Yard Algorithm to convert infix expressions to postfix expressions (Tinypost uses right postfix notation). Afterwards, Tinypost uses a postfix expressions evaluation algorithm to evaluate the compiled expression. Both algorithms make extensive use of stacks, meaning that popping and pushing to the stack is an O(1) process. The entire algorithm runs in about O(n) time because it has to loop through every token in the expression.

Notes

Tinypost raises python SyntaxErrors if it finds an error in an expresion you passed in. Variables, functions, and modules prefixed by an _ are meant for internal use only. Using these incorrectly could result in unpredictable behavior.

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

tinypost-0.0.3.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

tinypost-0.0.3-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file tinypost-0.0.3.tar.gz.

File metadata

  • Download URL: tinypost-0.0.3.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10

File hashes

Hashes for tinypost-0.0.3.tar.gz
Algorithm Hash digest
SHA256 b07997ddb14385476550fa464552ebe9b187cc12614adf10d3a2bc8c3b5af74a
MD5 0b969ec125a2b407ce7c2bb0420b8951
BLAKE2b-256 89a8567739f803ae8f08b0cf5367e7a85400eeade92a081af5c81e0e19f74d88

See more details on using hashes here.

File details

Details for the file tinypost-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: tinypost-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10

File hashes

Hashes for tinypost-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 705b360f33552bdd25d92400c3b1d98103ad4628e33c1374ccc79157713f58e7
MD5 40427c7674b00bbf226d054958c87798
BLAKE2b-256 2701fe2d66d70d910d09f63e77085d10b9864007fd3b2e4cf7fe862bc9a40588

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