Skip to main content
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.

Release files for tinypost 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tinypost 0.0.3
File Size Uploaded
tinypost-0.0.3.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tinypost 0.0.3
File Interpreter ABI Platform
tinypost-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 16.4 kB

Release files / tinypost-0.0.3.tar.gz

Download URL tinypost-0.0.3.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b07997ddb14385476550fa464552ebe9b187cc12614adf10d3a2bc8c3b5af74a
BLAKE2b-256 checksum
How to use checksums
89a8567739f803ae8f08b0cf5367e7a85400eeade92a081af5c81e0e19f74d88
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / tinypost-0.0.3-py3-none-any.whl

Download URL tinypost-0.0.3-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
705b360f33552bdd25d92400c3b1d98103ad4628e33c1374ccc79157713f58e7
BLAKE2b-256 checksum
How to use checksums
2701fe2d66d70d910d09f63e77085d10b9864007fd3b2e4cf7fe862bc9a40588
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page