Skip to main content

A safe and simple math expression evaluator for Python.

Project description

expr.py-inf

A safe and simple math evaluator for Python, built with rply.
View on PyPI

Downloads

Expr.py-inf is Expr.py But modified to remove the safe limits if anyone ever wanted to The Safe limits are also now configable

expr.evaluate("1+1", max_safe_number_input=2147483347, max_exponent_input=2147483347, max_factorial_input=2147483347)

Expr.py is a simple but safe math expression evaluator made for Python.
It can evaluate pretty advanced math concepts without crashing your computer.

Made using rply

Features

  • Fully object oriented
  • Completely typed for intellisense
  • Protection against DoS attacks
  • Customizable and extendable
  • Follows order of operations
  • Floating point precision

Getting started

You should install expr.py using pip:

$ pip install -U expr.py-inf

Here is a simple program to get started:

import expr

if __name__ == '__main__':
    expression = '6 + 5 * 2' 
    print(expr.evaluate(expression))  # 16

What does expr.py support?

Basic operations

The following operations are supported by expr.py:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • // (floor division)
  • % (modulo)
  • ^ (exponentation)
  • ! (factorial)

Variables

The most basic way of defining variables is by passing in the variables kwarg into the evaluator.

expr.evaluate('2x', variables={'x': 2})  # 4

You can also let the input define variables:

expr.evaluate('x = 5')
expr.evaluate('6 + x')  # 11

There are by default, 2 predefined constants. (pi and e)

Functions [WIP]

You can define functions through the builtins kwarg:

def f(x):
    return x + 1

expr.evaluate('f(5)', builtins={'f': f})  # 6

You can also define functions via input:

expr.evaluate('f(x) = 2x')
expr.evaluate('f(3)')  # 6

There are a few builtin functions:

  • sqrt
  • cbrt
  • log
  • log10
  • ln
  • rad
  • sin
  • cos
  • tan
  • asin
  • acos
  • atan

Grouping

This concept is pretty simple, anything in parentheses will be evaluated before anything outside of them.

expr.evaluate('5 * 6 + 2')  # 32
expr.evaluate('5 * (6 + 2)')  # 40

States

You can create different states so that each can store their own variables and functions independently from others.

To do this, use expr.create_state:

state = expr.create_state()
print(state.evaluate('0.1 + 0.2'))  # 0.3 

Note: All parameters belong in create_state rather than in evaluate for states.

Again, variables and functions are independent from each other:

state1 = expr.create_state()
state1.evaluate('x = 1')

state2 = expr.create_state()
state2.evaluate('x')  # error (x is not defined)

state1.evaluate('x')  # 1

Changelog

v0.2

This update mainly brings bug fixes from v0.1.

What's new?

  • You can now pass in custom classes into Parser.evaluate
  • Constants are now precise to around 30 places.
  • New constants (phi, tau)
More precise builtin functions

v0.2 changes the way some builtin functions are processed for boosts on both performance and precision.

  • sqrt now uses Decimal.sqrt
  • log10 now uses Decimal.log10
  • ln now uses Decimal.ln
  • cbrt now uses input ** expr.one_third
  • sin now uses expr.sin
  • cos now uses expr.cos

Bug fixes

  • Fixed unary minus interfering with implicit multiplication.
    • in v0.1: 5-3 = -15
    • in v0.2: 5-3 = 2

Miscellaneous

  • Many functions now have positional-only arguments for slight performance boosts
    • This drops support for Python 3.7
  • Messages retrieved from ParsingError.friendly are now much more descriptive.

v0.3

What's new?

  • Unary plus is now supported (E.g. +5)
  • Scientific notation is now supported (E.g. 4E-2)
    • To reduce conflics, 'E' must be captialized.
      This means that 2e9 would evaluate to 2 * e * 9, for example.
  • The cls kwarg is now supported in expr.evaluate

Bug fixes

  • Catch OverflowError in the expr.Overflow parsing error.
  • Fix invalid typings with Callable

v0.4

  • Removed The Safe Limits for expr.py!

What's new?

  • Increased Safe limits to Infinity
  • Added some new arguments to the evaluate() function

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

expr_py_inf-0.4.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

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

expr_py_inf-0.4.1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file expr_py_inf-0.4.1.tar.gz.

File metadata

  • Download URL: expr_py_inf-0.4.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for expr_py_inf-0.4.1.tar.gz
Algorithm Hash digest
SHA256 e624ac7c33bc90e087f1ae63c2172be05050146e1b65ac024be2d011c0c3a310
MD5 a0b69a0d811ca01d835398886eb06ac6
BLAKE2b-256 5dd1bd0ce7b89dc3f22ec7fdd7df2917511bdceb3376a1ddcf792315f39f9d56

See more details on using hashes here.

File details

Details for the file expr_py_inf-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: expr_py_inf-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for expr_py_inf-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8e934cb7db1c911cb5e148779fb680064451dc57c9250cdb2d1bfcaf0fb0fff3
MD5 7c15f150363a89d125705aa4c587371d
BLAKE2b-256 fe10511a9ed091c7f3f1fa809d31925ca8d2f8a51bbf95fbae66f6e73c546683

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