|
1 | 1 | # mathparse |
2 | 2 |
|
3 | | -The `mathparse` library is a Python module designed to evaluate mathematical equations contained in strings. |
| 3 | +**A secure, multilingual mathematical expression evaluator for Python** |
4 | 4 |
|
5 | | -Here are a few examples: |
| 5 | +`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. |
| 6 | + |
| 7 | +## Why mathparse? |
| 8 | + |
| 9 | +✅ **Security First** - Never uses `eval()`, protecting against arbitrary code execution |
| 10 | +✅ **Multilingual Support** - Parse math in English, Spanish, French, German, Chinese, and 8+ more languages |
| 11 | +✅ **Zero Dependencies** - Pure Python implementation with no external requirements |
| 12 | +✅ **Natural Language** - Understands "fifty times twenty plus ten" alongside standard notation |
| 13 | +✅ **Production Ready** - Used in chatbots, calculators, voice assistants, and educational applications |
| 14 | +✅ **Well Tested** - Comprehensive test suite ensuring reliability |
| 15 | + |
| 16 | +## Quick Examples |
6 | 17 |
|
7 | 18 | ```python |
8 | 19 | from mathparse import mathparse |
9 | 20 |
|
| 21 | +# Standard numeric expressions |
10 | 22 | mathparse.parse('50 * (85 / 100)') |
11 | 23 | >>> 42.5 |
12 | 24 |
|
| 25 | +# Natural language in English |
13 | 26 | mathparse.parse('one hundred times fifty four', language='ENG') |
14 | 27 | >>> 5400 |
15 | 28 |
|
| 29 | +# Mixed notation |
16 | 30 | mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG') |
17 | 31 | >>> 24 |
| 32 | + |
| 33 | +# Other languages (French, Spanish, German, Chinese, etc.) |
| 34 | +mathparse.parse('cinq plus trois', language='FRE') |
| 35 | +>>> 8 |
| 36 | + |
| 37 | +mathparse.parse('cinco más tres', language='ESP') |
| 38 | +>>> 8 |
| 39 | +``` |
| 40 | + |
| 41 | +## Use Cases |
| 42 | + |
| 43 | +- 🤖 **Chatbots & Voice Assistants** - Parse natural language math queries |
| 44 | +- 🧮 **Calculator Applications** - Build safe calculators that accept text input |
| 45 | +- 📚 **Educational Software** - Evaluate student-provided math expressions |
| 46 | +- 🌐 **Multilingual Apps** - Support math parsing in users' native languages |
| 47 | +- 🔐 **Secure Code Evaluation** - Replace dangerous `eval()` calls with safe parsing |
| 48 | +- 📊 **Data Processing** - Extract and calculate values from natural language text |
| 49 | + |
| 50 | +## Security: Why Not eval()? |
| 51 | + |
| 52 | +Python's `eval()` function executes arbitrary code, creating severe security vulnerabilities: |
| 53 | + |
| 54 | +```python |
| 55 | +# DANGEROUS - Never do this with user input! |
| 56 | +eval("__import__('os').system('rm -rf /')") # Deletes files |
| 57 | +eval("__import__('requests').get('evil.com')") # Network access |
18 | 58 | ``` |
19 | 59 |
|
20 | | -## Security |
| 60 | +**mathparse is the safe alternative:** |
| 61 | + |
| 62 | +| Feature | eval() | mathparse | |
| 63 | +|---------|--------|-----------| |
| 64 | +| Mathematical expressions | ✅ | ✅ | |
| 65 | +| Arbitrary code execution | ⚠️ **YES - DANGEROUS** | ❌ No | |
| 66 | +| File system access | ⚠️ **YES - DANGEROUS** | ❌ No | |
| 67 | +| Network access | ⚠️ **YES - DANGEROUS** | ❌ No | |
| 68 | +| Import statements | ⚠️ **YES - DANGEROUS** | ❌ No | |
| 69 | +| Security risk | 🔴 **CRITICAL** | 🟢 Safe | |
| 70 | +| Dependencies | 0 | 0 | |
| 71 | +| Natural language support | ❌ No | ✅ Yes | |
| 72 | +| Multilingual | ❌ No | ✅ 13+ languages | |
| 73 | + |
| 74 | +mathparse uses [postfix (Reverse Polish) notation](https://mathparse.chatterbot.us/postfix/) internally, ensuring only valid mathematical operations are performed. See our [security documentation](https://mathparse.chatterbot.us/postfix/) for technical details. |
21 | 75 |
|
22 | | -Mathparse does not employ the use of Python's [`eval` function](https://docs.python.org/3/library/functions.html#eval) when evaluating provided mathematical expressions. This is a measure to prevent arbitrary code execution vulnerabilities. See https://mathparse.chatterbot.us/postfix/ for additional details. |
| 76 | +## Performance |
23 | 77 |
|
24 | | -Mathparse is a standalone Python package and requires zero dependencies to function. |
| 78 | +- **Fast**: Simple numeric expressions parse in microseconds |
| 79 | +- **Efficient**: Minimal memory footprint, suitable for high-volume applications |
| 80 | +- **Scalable**: Linear time complexity for expression evaluation |
25 | 81 |
|
26 | 82 | ## Language Support |
27 | 83 |
|
|
0 commit comments