Skip to main content

A fun project for parsing arithmetic expressions

Project description

MathPy

A small parser for arithmetic expressions

The goal is to parse expressions like (1+2)*3 and evaluate them.

It also parses an identifiers like x+2. The resulting object then needs values for those to evaluate itself.

CLI

The package can be executed as script. It will evaluate an expression and accepts any number of key-value-pairs to use for variables.

~$ python3 -m expr_parser x^2+y x=2 y=1
5

Basic Usage

The main API provides 3 functions (each takes an expression):

  • parse as the name says, parses the expression an returns a syntax tree
  • evaluate creates and immidiently evaluates the resulting syntax tree. No unknown can be used!
  • function takes an expression containing the unknown x and returns a function
>>> from expr_parser import *
>>> evaluate("(1+2)*3")
9
>>> f = function("x^2")
>>> f(4)
16

Advanced Usage

The Parser object

The basic API is just a shortcut for instancing a Parser object and calling its methods.

A Parser object contains the information required for parsing an expression:

  • What brackets are used
  • Which operators are used

These can be controled with:

  • Parser.add_brackets(opening, closing)
  • Parser.add_operator(operator)

The static method default() instances an object and populates it with the default brackets and operators. This method is used to provide the basic API's parser.

Custom Operators

An operator object requires the following parameters:

  • symbol: sthe operator's string representation
  • priority: integer for determening the operation's execution order
  • binary: method for using the operator between two values x and y
  • unary: method for using the operator in front of a single value x

The Operator class provides the @Operator.handle_callables decorator to extend the binary method to work with functions as well as numbers.

from expr_parser.operators.base import Operator

Mod = Operator(
  symbol="%",
  priority=20
  binary=Operator.handle_callables(lambda x, y: x % y)
)

The basic math operators +, -, *, /, ^ are implemented in expr_parser.operators.default.

In expr_parser.operators.dice is another example for a custom operator. It's the dice operator known from tabletop-roleplays, where 2d6 means roll a 6-sided die 2-times.

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

gammelmath-0.0.1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

gammelmath-0.0.1-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file gammelmath-0.0.1.tar.gz.

File metadata

  • Download URL: gammelmath-0.0.1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.6

File hashes

Hashes for gammelmath-0.0.1.tar.gz
Algorithm Hash digest
SHA256 8ba7ceb9619c453f6edc47f00ef0d6516a31933a1ad986afd6032585e4bc3b34
MD5 afc0cb6bcefaa0f5cdee62a473dc38a9
BLAKE2b-256 aa013c6535fffe64ae58fc169c62e74467fbd3d37cbee1b3a009f4089cb9d374

See more details on using hashes here.

File details

Details for the file gammelmath-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: gammelmath-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.6

File hashes

Hashes for gammelmath-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f08451ba044b88092377e307994f9c65fb94e316034f4fe8dfd13ebd24d959cb
MD5 75ecc602d3ca139915c778c6e4e257ed
BLAKE2b-256 754cdc37fd91934839bc6b6c1ceab50b48d96f3be834dea0b04f01446d1b6150

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