Skip to main content

Parsematic: a basic math parser written in python

Project description

Parsematic

This is a basic math parser that can parse mathematical expressions.

Features

Parses a variety of mathematical expressions, including:

  • Integers (0, 1, 5, -7), Floats (3.14159, 2.7, 0.5)

  • Basic arithmetic operations (+, -, \*, /, \*\*, //, %)

  • Constants PI, TAU, NAN, E, INF

  • Comparison operators (==, !=, <, >, <=, >=)

  • Parentheses for grouping

  • Mathematical functions (sin, cos, tan, abs, sqrt, log, fact, gcd, lcm, xor, int, float, min, max)

Usage

First, install it with:

pip install parsematic

Import the MathParser class:

from math_parser import MathParser

Create a MathParser instance:

parser = MathParser()

Use the parse() method to parse and evaluate an expression:

result = parser.parse("2 + 3 * (4 - 1)")
print(result) # Output: 11

Or, use it from the command line

python -m parsematic "2 + 3 * (4 - 1)"

Supported Operators:

  • Arithmetic operators: +, -, \_, / (true division), // (floor division), \*\* (exponentiation), % (modulo)
  • Comparison operators: ==, !=, <, >, <=, >=
  • Supported Functions sin, cos, tan, abs, sqrt, log, fact (factorial), gcd (greatest common divisor), lcm (least common multiple), xor (bitwise exclusive or), int, float (type conversions), min, max

Error Handling

The parser will raise exceptions for invalid syntax or unsupported operations.

Additional Notes

The parser currently does support constants or user-defined functions (although you have to modify MathFunc.funcs). (but not variables within the parser itself)

Contributing

Pull requests are welcome!

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

parsematic-1.0.0.tar.gz (5.9 kB view hashes)

Uploaded Source

Built Distribution

parsematic-1.0.0-py3-none-any.whl (6.7 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