Skip to main content

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.

Metadata

Release files for azrideus-mathparse 0.2.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for azrideus-mathparse 0.2.19
File Size Uploaded Provenance Actions
azrideus_mathparse-0.2.19.tar.gz 42.6 kB Without provenance Details for azrideus_mathparse-0.2.19.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for azrideus-mathparse 0.2.19
File Interpreter ABI Platform Actions
azrideus_mathparse-0.2.19-py3-none-any.whl Python 3 none any Details for azrideus_mathparse-0.2.19-py3-none-any.whl

Total release size: 76.3 kB

Release files / azrideus_mathparse-0.2.19.tar.gz

Download URL azrideus_mathparse-0.2.19.tar.gz
Size 42.6 kB
Tags Source
SHA-256 checksum
How to use checksums
dee98280608bf988619ef29aca852a64eeed96f3c1880fef1bed750c3f71c6b1
BLAKE2b-256 checksum
How to use checksums
29a9166d90d2607119d0056a9ff2a4e4ad9bba99922eb268475f2ba8de46f923
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / azrideus_mathparse-0.2.19-py3-none-any.whl

Download URL azrideus_mathparse-0.2.19-py3-none-any.whl
Size 33.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
985d9ca9511de0fb02dd5d047577511c298bb35daee183b6009f366855fa98c6
BLAKE2b-256 checksum
How to use checksums
83f3d599fd38cf983803b086640eff93ae0bb994acac875a709455214e73c7f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for azrideus-mathparse 0.2.19

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page