Skip to main content

A package that evaluates a mathematical expression given as a string.

Project description

Math Evaluator

A package that evaluates a mathematical expression given as a string.

By default it accepts

  • operands
    • ints and
    • floats
  • operators
    • unary +-,
    • binary +-*/ and
    • parantheses ()

Getting Started

Getting your first application running is super easy.

pip install math_evaluator
"""
A small CLI application that endlessly takes user input and evaluates it as a mathematical expression
"""
from math_evaluator import calc 

while True:
    # this will raise a SyntaxError if the expression is syntactically incorrect
    # normally you should always catch that Exception
    expression = input(">>> ")
    print(calc(expression))

Customization

The above usage is the simplest and suffices most of the time. Anyways, Math Evaluator is built to be customized by you. It internally uses the Python parser however, so it's abilities cannot be changed fundamentally. For example operator assoziativity or how parentheses work cannot be altered.

This package consists of two main modules .implicit and .explicit. However, most applications will only need to use one of them.

.implicit

Checks if the expression is a valid mathematical expression and then evals it. This makes it totally safe to use - even when exposed to potencially malicious users.

.implicit example

We might want to extend the calculator to allow powers (** is the corresponding operator). To do that we can reach into math_evaluator.implicit and add ast operations to the valid operations

import ast
from math_evaluator.implicit import calc, valid_ops

valid_ops.add(ast.Pow)

assert calc("4**3") == 4**3

You can not only change the valid operations but also the allowed operands. We could for example add complex numbers

from math_evaluator.implicit import calc, allowed_types

allowed_types.add(complex)

assert calc("3*5j") == 15j

.explicit

Explicitly evaluates the expression. This allows you to modify the semantics.

.explicit example

In many other programming languages 5 ^ 4 is the same as 5*5*5*5 = 625. In Python however, ^ is the bitwise xor operator and 5 ^ 4 evaluates to 1. Now we will redefine ^ to mean the same as ** by reaching into the .explicit module:

import ast
import operator
from math_evaluator.explicit import calc, op_map

op_map[ast.BitXor] = operator.__pow__

assert calc("5^4") == 625

math_parser exports .explicit.calc by default.

Further customization

If you want to change the fundamental syntax or semantics, you can file an issue. Then I will try my best at weighing cost vs gain. If I deem your feature request as "unworthy", that should not stop you from forking this project and extending it on your own. I am available for any questions you might have.

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

math_evaluator-0.0.3.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

math_evaluator-0.0.3-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: math_evaluator-0.0.3.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.5

File hashes

Hashes for math_evaluator-0.0.3.tar.gz
Algorithm Hash digest
SHA256 135c354c61a3be297176067961671f696ac3bc1626df3055094ab32e496b7bc0
MD5 1c3adf72a4938489a2952be532a3db94
BLAKE2b-256 7440c77c398e005f8f37551e4c2c03219917209df4054490d768a9e2753ebb06

See more details on using hashes here.

File details

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

File metadata

  • Download URL: math_evaluator-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.5

File hashes

Hashes for math_evaluator-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 040804047aa8981ad85e9031d4656dafdea1790c05b6c7221c1bfc4b8877cc97
MD5 8cd2233a9fcc37728b593c4c356adee5
BLAKE2b-256 83ab57220ce7e3478748516ebd0501fe582e150100e0f18b46f02503c23a35ef

See more details on using hashes here.

Supported by

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