Skip to main content

mathparse is a library for solving mathematical equations contained in strings

Project description

mathparse

A secure, multilingual mathematical expression evaluator for Python

mathparse is a Python library that safely parses and evaluates mathematical expressions from strings, supporting both numeric operators and natural language words across 13+ languages. Unlike Python's dangerous eval() function, mathparse provides a secure, zero-dependency solution for evaluating user-provided mathematical expressions.

Why mathparse?

Security First - Never uses eval(), protecting against arbitrary code execution
Multilingual Support - Parse math in English, Spanish, French, German, Chinese, and 8+ more languages
Zero Dependencies - Pure Python implementation with no external requirements
Natural Language - Understands "fifty times twenty plus ten" alongside standard notation
Production Ready - Used in chatbots, calculators, voice assistants, and educational applications
Well Tested - Comprehensive test suite ensuring reliability

Quick Examples

from mathparse import mathparse

# Standard numeric expressions
mathparse.parse('50 * (85 / 100)')
>>> 42.5

# Natural language in English
mathparse.parse('one hundred times fifty four', language='ENG')
>>> 5400

# Mixed notation
mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
>>> 24

# Other languages (French, Spanish, German, Chinese, etc.)
mathparse.parse('cinq plus trois', language='FRE')
>>> 8

mathparse.parse('cinco más tres', language='ESP')
>>> 8

Use Cases

  • 🤖 Chatbots & Voice Assistants - Parse natural language math queries
  • 🧮 Calculator Applications - Build safe calculators that accept text input
  • 📚 Educational Software - Evaluate student-provided math expressions
  • 🌐 Multilingual Apps - Support math parsing in users' native languages
  • 🔐 Secure Code Evaluation - Replace dangerous eval() calls with safe parsing
  • 📊 Data Processing - Extract and calculate values from natural language text

Security: Why Not eval()?

Python's eval() function executes arbitrary code, creating severe security vulnerabilities:

# DANGEROUS - Never do this with user input!
eval("__import__('os').system('rm -rf /')")  # Deletes files
eval("__import__('requests').get('evil.com')")  # Network access

mathparse is the safe alternative:

Feature eval() mathparse
Mathematical expressions
Arbitrary code execution ⚠️ YES - DANGEROUS ❌ No
File system access ⚠️ YES - DANGEROUS ❌ No
Network access ⚠️ YES - DANGEROUS ❌ No
Import statements ⚠️ YES - DANGEROUS ❌ No
Security risk 🔴 CRITICAL 🟢 Safe
Dependencies 0 0
Natural language support ❌ No ✅ Yes
Multilingual ❌ No ✅ 13+ languages

mathparse uses postfix (Reverse Polish) notation internally, ensuring only valid mathematical operations are performed. See our security documentation for technical details.

Performance

  • Fast: Simple numeric expressions parse in microseconds
  • Efficient: Minimal memory footprint, suitable for high-volume applications
  • Scalable: Linear time complexity for expression evaluation

Language Support

The language parameter must be set in order to evaluate an equation that uses word operators. The language code should be a valid ISO 639-2 language code.

Installation

pip install mathparse

Documentation

See the full documentation at https://mathparse.chatterbot.us

Changelog

See release notes for changes.

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

mathparse-0.2.8.tar.gz (31.6 kB view details)

Uploaded Source

Built Distribution

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

mathparse-0.2.8-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file mathparse-0.2.8.tar.gz.

File metadata

  • Download URL: mathparse-0.2.8.tar.gz
  • Upload date:
  • Size: 31.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for mathparse-0.2.8.tar.gz
Algorithm Hash digest
SHA256 704a9d56e4b4f65e4cd99fcc38dd02bc1a79cbfda19b166d9032811f776b1903
MD5 f55c151d651010c5429f48174f3b0917
BLAKE2b-256 ec33e9b3e925c3e163281d3aff0f22a88b7ca55ef12f217df28b1f88dfcffff4

See more details on using hashes here.

File details

Details for the file mathparse-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: mathparse-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for mathparse-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 c20908efbac500f174fa75ffc497d9b39701edd13a56992ddeed31d07de7d605
MD5 f83a5d4146207415a2e6905f91191579
BLAKE2b-256 69d3108e9dcba5b82f7beece54449d233943fe032cc1ff47066054568fca4616

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