Skip to main content

A library for parsing math strings

Project description

MathExpr

MathExpr is a Python library for parsing and evaluating mathematical expressions.

Table of Contents

Getting Started

To get started with this project, please follow these steps:

Prerequisites

  • Python 3.8+
  • pip 20.0+

Installation

To install the library, run the following command:

pip install mathexpr

Features

MathExpr provides the following features:

  • Parsing mathematical expressions
  • Evaluating mathematical expressions
  • Evaluating mathematical expressions with variables

Usage

To use MathExpr, import the MathParse class and use the parse and evaluate methods. Simple usage example:

from mathexpr import MathExpr

math_string = "(2 + 3) * 4^3"
result = MathExpr.evaluate(math_string)
print(result)

>>> 320.0

Usage with variables:

from mathexpr import MathExpr
math_string = "x + y"
result = MathExpr.evaluate(math_string, {"x": 2, "y": 3})
print(result)

>>> 5

Debug the AST:

from mathexpr import MathExpr, print_ast

math_string = "(2 + 3) * 4^3"
ast = MathExpr.parse(math_string)
print_ast(ast)

>>> TokenType.MUL
>>> вњвЂвЂTokenType.ADD
>>> в”‚  вњвЂвЂNumNode(value=2.0)
>>> в”‚  в•°вЂвЂNumNode(value=3.0)
>>> в•°вЂвЂTokenType.POW
>>> в”‚  вњвЂвЂNumNode(value=4.0)
>>> в”‚  в•°вЂвЂNumNode(value=3.0)

Contributing

If you would like to contribute to this project, please fork the repository and create a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

mathexpr-1.0.3.tar.gz (2.9 kB view details)

Uploaded Source

File details

Details for the file mathexpr-1.0.3.tar.gz.

File metadata

  • Download URL: mathexpr-1.0.3.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for mathexpr-1.0.3.tar.gz
Algorithm Hash digest
SHA256 54e883186f0409644e4fffab3a150187c969bd14cf53f2c29f2ebcbfb407007b
MD5 141fc4df29e90d4cdcd9bf73b5632a21
BLAKE2b-256 ea634209efd896fc1744093ef0a4bed31aadbe11516a5554f5844b5135e80f84

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