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 details)

Uploaded Source

Built Distribution

parsematic-1.0.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file parsematic-1.0.0.tar.gz.

File metadata

  • Download URL: parsematic-1.0.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for parsematic-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8a0f7532f2e443230b47b6be60d4a21974da4b351ae3650520a29414f1f337f2
MD5 1740cfd9bca3ba6c64101524f34cec3c
BLAKE2b-256 56a3e371667e7e86907df79beda8ea57258ff22e39095f26b654a81493630d2b

See more details on using hashes here.

File details

Details for the file parsematic-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: parsematic-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for parsematic-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe2f2eb8e6f3b2985169c12319740f1554c884f382f978ca3e0c95082307217b
MD5 051cefe9785bf4ffe71b9b39c621f1e5
BLAKE2b-256 3d2ad665f12cfa67b739985d1676d14ff7972f0257cecbf6b54daf86abe4b224

See more details on using hashes here.

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