Skip to main content

Package for Python that evaluates math expressions from strings

Project description

Mathematical Expressions for Python

tests status

What is Mathex?

Mathematical Expressions (or Mathex for short) is a package for Python that evaluates mathematical expressions from strings at runtime according to Mathex Specification. It aims to provide fast, easy, customizable and, most importantly, safe evaluation with no dependencies.

The package is compatible with Python 3.7+.

How to use?

Using Mathex is super easy - just import, initialize and evaluate. That's it.

from mathex import Mathex, default_flags

# Use `Config` class and `DefaultFlags` to get default settings.
# For what settings are available, check out documentation.
config = Mathex(default_flags)

# Config class contains your settings along with custom
# variables and functions you inserted.
x = 1.5
config.add_constant("x", x)

# These variables and functions are then available for users
# to use in expressions.
input = "2x + 5"

# Mathex returns error and result of evaluation as a tuple.
result, error = config.evaluate(input)

# If error is None, evaluation completed without errors
if not error:
    print(f"{input} is {result}")  # Outputs `2x + 5 is 8`

Don't forget to install Mathex using pip:

python -m pip install mathex

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

mathex-0.2.0.tar.gz (8.3 kB view hashes)

Uploaded Source

Built Distribution

mathex-0.2.0-py3-none-any.whl (10.8 kB view hashes)

Uploaded Python 3

Supported by

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