azrideus-mathparse 0.2.15__tar.gz → 0.2.18__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/PKG-INFO +125 -125
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/azrideus_mathparse.egg-info/PKG-INFO +125 -125
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/__init__.py +1 -1
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/evaluator.py +33 -8
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/setup.cfg +4 -4
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_binary_operations.py +80 -5
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_comparison_operations.py +138 -138
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_prefix_unary_operations.py +8 -7
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/LICENSE +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/README.md +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/azrideus_mathparse.egg-info/SOURCES.txt +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/azrideus_mathparse.egg-info/dependency_links.txt +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/azrideus_mathparse.egg-info/requires.txt +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/azrideus_mathparse.egg-info/top_level.txt +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/exceptions.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/extraction.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/limits.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/mathparse.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/mathwords.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/patterns.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/postfix.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/tokenizer.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/tokens.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/mathparse/word_tokens.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/pyproject.toml +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_bitshift_operations.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_bitwise_operations.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_complex_statements.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_hex_numbers.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_language_support.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_limits.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_replace_word_tokens.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_simplified_chinese.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_unary_operations.py +0 -0
- {azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_utils.py +0 -0
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: azrideus-mathparse
|
|
3
|
-
Version: 0.2.
|
|
4
|
-
Summary: A fork of mathparse (https://github.com/gunthercox/mathparse) for my own usage; a library for solving mathematical equations contained in strings
|
|
5
|
-
Author: Gunther Cox
|
|
6
|
-
License-Expression: MIT
|
|
7
|
-
Project-URL: Documentation, https://mathparse.chatterbot.us
|
|
8
|
-
Project-URL: Repository, https://github.com/gunthercox/mathparse
|
|
9
|
-
Project-URL: Changelog, https://github.com/gunthercox/mathparse/releases
|
|
10
|
-
Keywords: mathparse,mathematics,math,nlp,calculator,expression-parser,eval-alternative,safe-eval,multilingual,natural-language,postfix-notation,reverse-polish-notation,rpn,math-parser,expression-evaluator,secure-evaluation,language-processing,mathematical-expressions,formula-parser
|
|
11
|
-
Classifier: Development Status :: 4 - Beta
|
|
12
|
-
Classifier: Intended Audience :: Developers
|
|
13
|
-
Classifier: Operating System :: OS Independent
|
|
14
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
-
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
16
|
-
Classifier: Topic :: Text Processing :: General
|
|
17
|
-
Classifier: Topic :: Text Processing :: Linguistic
|
|
18
|
-
Classifier: Programming Language :: Python
|
|
19
|
-
Requires-Python: <3.15,>=3.10
|
|
20
|
-
Description-Content-Type: text/markdown
|
|
21
|
-
License-File: LICENSE
|
|
22
|
-
Provides-Extra: test
|
|
23
|
-
Requires-Dist: flake8; extra == "test"
|
|
24
|
-
Requires-Dist: sphinx<9.0,>=7.4; extra == "test"
|
|
25
|
-
Dynamic: license-file
|
|
26
|
-
|
|
27
|
-
# mathparse
|
|
28
|
-
|
|
29
|
-
**A secure, multilingual mathematical expression evaluator for Python**
|
|
30
|
-
|
|
31
|
-
`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.
|
|
32
|
-
|
|
33
|
-
## Why mathparse?
|
|
34
|
-
|
|
35
|
-
✅ **Security First** - Never uses `eval()`, protecting against arbitrary code execution
|
|
36
|
-
✅ **Multilingual Support** - Parse math in English, Spanish, French, German, Chinese, and 8+ more languages
|
|
37
|
-
✅ **Zero Dependencies** - Pure Python implementation with no external requirements
|
|
38
|
-
✅ **Natural Language** - Understands "fifty times twenty plus ten" alongside standard notation
|
|
39
|
-
✅ **Production Ready** - Used in chatbots, calculators, voice assistants, and educational applications
|
|
40
|
-
✅ **Well Tested** - Comprehensive test suite ensuring reliability
|
|
41
|
-
|
|
42
|
-
## Quick Examples
|
|
43
|
-
|
|
44
|
-
```python
|
|
45
|
-
from mathparse import mathparse
|
|
46
|
-
|
|
47
|
-
# Standard numeric expressions
|
|
48
|
-
mathparse.parse('50 * (85 / 100)')
|
|
49
|
-
>>> 42.5
|
|
50
|
-
|
|
51
|
-
# Natural language in English
|
|
52
|
-
mathparse.parse('one hundred times fifty four', language='ENG')
|
|
53
|
-
>>> 5400
|
|
54
|
-
|
|
55
|
-
# Mixed notation
|
|
56
|
-
mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
|
|
57
|
-
>>> 24
|
|
58
|
-
|
|
59
|
-
# Other languages (French, Spanish, German, Chinese, etc.)
|
|
60
|
-
mathparse.parse('cinq plus trois', language='FRE')
|
|
61
|
-
>>> 8
|
|
62
|
-
|
|
63
|
-
mathparse.parse('cinco más tres', language='ESP')
|
|
64
|
-
>>> 8
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
## Use Cases
|
|
68
|
-
|
|
69
|
-
- 🤖 **Chatbots & Voice Assistants** - Parse natural language math queries
|
|
70
|
-
- 🧮 **Calculator Applications** - Build safe calculators that accept text input
|
|
71
|
-
- 📚 **Educational Software** - Evaluate student-provided math expressions
|
|
72
|
-
- 🌐 **Multilingual Apps** - Support math parsing in users' native languages
|
|
73
|
-
- 🔐 **Secure Code Evaluation** - Replace dangerous `eval()` calls with safe parsing
|
|
74
|
-
- 📊 **Data Processing** - Extract and calculate values from natural language text
|
|
75
|
-
|
|
76
|
-
## Security: Why Not eval()?
|
|
77
|
-
|
|
78
|
-
Python's `eval()` function executes arbitrary code, creating severe security vulnerabilities:
|
|
79
|
-
|
|
80
|
-
```python
|
|
81
|
-
# DANGEROUS - Never do this with user input!
|
|
82
|
-
eval("__import__('os').system('rm -rf /')") # Deletes files
|
|
83
|
-
eval("__import__('requests').get('evil.com')") # Network access
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
**mathparse is the safe alternative:**
|
|
87
|
-
|
|
88
|
-
| Feature | eval() | mathparse |
|
|
89
|
-
|---------|--------|-----------|
|
|
90
|
-
| Mathematical expressions | ✅ | ✅ |
|
|
91
|
-
| Arbitrary code execution | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
92
|
-
| File system access | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
93
|
-
| Network access | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
94
|
-
| Import statements | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
95
|
-
| Security risk | 🔴 **CRITICAL** | 🟢 Safe |
|
|
96
|
-
| Dependencies | 0 | 0 |
|
|
97
|
-
| Natural language support | ❌ No | ✅ Yes |
|
|
98
|
-
| Multilingual | ❌ No | ✅ 13+ languages |
|
|
99
|
-
|
|
100
|
-
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.
|
|
101
|
-
|
|
102
|
-
## Performance
|
|
103
|
-
|
|
104
|
-
- **Fast**: Simple numeric expressions parse in microseconds
|
|
105
|
-
- **Efficient**: Minimal memory footprint, suitable for high-volume applications
|
|
106
|
-
- **Scalable**: Linear time complexity for expression evaluation
|
|
107
|
-
|
|
108
|
-
## Language Support
|
|
109
|
-
|
|
110
|
-
The language parameter must be set in order to evaluate an equation that uses word operators.
|
|
111
|
-
The language code should be a valid [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/code_list.php) language code.
|
|
112
|
-
|
|
113
|
-
## Installation
|
|
114
|
-
|
|
115
|
-
```bash
|
|
116
|
-
pip install mathparse
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
## Documentation
|
|
120
|
-
|
|
121
|
-
See the full documentation at https://mathparse.chatterbot.us
|
|
122
|
-
|
|
123
|
-
## Changelog
|
|
124
|
-
|
|
125
|
-
See [release notes](https://github.com/gunthercox/mathparse/releases) for changes.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: azrideus-mathparse
|
|
3
|
+
Version: 0.2.18
|
|
4
|
+
Summary: A fork of mathparse (https://github.com/gunthercox/mathparse) for my own usage; a library for solving mathematical equations contained in strings
|
|
5
|
+
Author: Gunther Cox
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Documentation, https://mathparse.chatterbot.us
|
|
8
|
+
Project-URL: Repository, https://github.com/gunthercox/mathparse
|
|
9
|
+
Project-URL: Changelog, https://github.com/gunthercox/mathparse/releases
|
|
10
|
+
Keywords: mathparse,mathematics,math,nlp,calculator,expression-parser,eval-alternative,safe-eval,multilingual,natural-language,postfix-notation,reverse-polish-notation,rpn,math-parser,expression-evaluator,secure-evaluation,language-processing,mathematical-expressions,formula-parser
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
16
|
+
Classifier: Topic :: Text Processing :: General
|
|
17
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
18
|
+
Classifier: Programming Language :: Python
|
|
19
|
+
Requires-Python: <3.15,>=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: flake8; extra == "test"
|
|
24
|
+
Requires-Dist: sphinx<9.0,>=7.4; extra == "test"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# mathparse
|
|
28
|
+
|
|
29
|
+
**A secure, multilingual mathematical expression evaluator for Python**
|
|
30
|
+
|
|
31
|
+
`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.
|
|
32
|
+
|
|
33
|
+
## Why mathparse?
|
|
34
|
+
|
|
35
|
+
✅ **Security First** - Never uses `eval()`, protecting against arbitrary code execution
|
|
36
|
+
✅ **Multilingual Support** - Parse math in English, Spanish, French, German, Chinese, and 8+ more languages
|
|
37
|
+
✅ **Zero Dependencies** - Pure Python implementation with no external requirements
|
|
38
|
+
✅ **Natural Language** - Understands "fifty times twenty plus ten" alongside standard notation
|
|
39
|
+
✅ **Production Ready** - Used in chatbots, calculators, voice assistants, and educational applications
|
|
40
|
+
✅ **Well Tested** - Comprehensive test suite ensuring reliability
|
|
41
|
+
|
|
42
|
+
## Quick Examples
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from mathparse import mathparse
|
|
46
|
+
|
|
47
|
+
# Standard numeric expressions
|
|
48
|
+
mathparse.parse('50 * (85 / 100)')
|
|
49
|
+
>>> 42.5
|
|
50
|
+
|
|
51
|
+
# Natural language in English
|
|
52
|
+
mathparse.parse('one hundred times fifty four', language='ENG')
|
|
53
|
+
>>> 5400
|
|
54
|
+
|
|
55
|
+
# Mixed notation
|
|
56
|
+
mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
|
|
57
|
+
>>> 24
|
|
58
|
+
|
|
59
|
+
# Other languages (French, Spanish, German, Chinese, etc.)
|
|
60
|
+
mathparse.parse('cinq plus trois', language='FRE')
|
|
61
|
+
>>> 8
|
|
62
|
+
|
|
63
|
+
mathparse.parse('cinco más tres', language='ESP')
|
|
64
|
+
>>> 8
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Use Cases
|
|
68
|
+
|
|
69
|
+
- 🤖 **Chatbots & Voice Assistants** - Parse natural language math queries
|
|
70
|
+
- 🧮 **Calculator Applications** - Build safe calculators that accept text input
|
|
71
|
+
- 📚 **Educational Software** - Evaluate student-provided math expressions
|
|
72
|
+
- 🌐 **Multilingual Apps** - Support math parsing in users' native languages
|
|
73
|
+
- 🔐 **Secure Code Evaluation** - Replace dangerous `eval()` calls with safe parsing
|
|
74
|
+
- 📊 **Data Processing** - Extract and calculate values from natural language text
|
|
75
|
+
|
|
76
|
+
## Security: Why Not eval()?
|
|
77
|
+
|
|
78
|
+
Python's `eval()` function executes arbitrary code, creating severe security vulnerabilities:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
# DANGEROUS - Never do this with user input!
|
|
82
|
+
eval("__import__('os').system('rm -rf /')") # Deletes files
|
|
83
|
+
eval("__import__('requests').get('evil.com')") # Network access
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**mathparse is the safe alternative:**
|
|
87
|
+
|
|
88
|
+
| Feature | eval() | mathparse |
|
|
89
|
+
|---------|--------|-----------|
|
|
90
|
+
| Mathematical expressions | ✅ | ✅ |
|
|
91
|
+
| Arbitrary code execution | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
92
|
+
| File system access | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
93
|
+
| Network access | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
94
|
+
| Import statements | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
95
|
+
| Security risk | 🔴 **CRITICAL** | 🟢 Safe |
|
|
96
|
+
| Dependencies | 0 | 0 |
|
|
97
|
+
| Natural language support | ❌ No | ✅ Yes |
|
|
98
|
+
| Multilingual | ❌ No | ✅ 13+ languages |
|
|
99
|
+
|
|
100
|
+
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.
|
|
101
|
+
|
|
102
|
+
## Performance
|
|
103
|
+
|
|
104
|
+
- **Fast**: Simple numeric expressions parse in microseconds
|
|
105
|
+
- **Efficient**: Minimal memory footprint, suitable for high-volume applications
|
|
106
|
+
- **Scalable**: Linear time complexity for expression evaluation
|
|
107
|
+
|
|
108
|
+
## Language Support
|
|
109
|
+
|
|
110
|
+
The language parameter must be set in order to evaluate an equation that uses word operators.
|
|
111
|
+
The language code should be a valid [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/code_list.php) language code.
|
|
112
|
+
|
|
113
|
+
## Installation
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
pip install mathparse
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Documentation
|
|
120
|
+
|
|
121
|
+
See the full documentation at https://mathparse.chatterbot.us
|
|
122
|
+
|
|
123
|
+
## Changelog
|
|
124
|
+
|
|
125
|
+
See [release notes](https://github.com/gunthercox/mathparse/releases) for changes.
|
{azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/azrideus_mathparse.egg-info/PKG-INFO
RENAMED
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: azrideus-mathparse
|
|
3
|
-
Version: 0.2.
|
|
4
|
-
Summary: A fork of mathparse (https://github.com/gunthercox/mathparse) for my own usage; a library for solving mathematical equations contained in strings
|
|
5
|
-
Author: Gunther Cox
|
|
6
|
-
License-Expression: MIT
|
|
7
|
-
Project-URL: Documentation, https://mathparse.chatterbot.us
|
|
8
|
-
Project-URL: Repository, https://github.com/gunthercox/mathparse
|
|
9
|
-
Project-URL: Changelog, https://github.com/gunthercox/mathparse/releases
|
|
10
|
-
Keywords: mathparse,mathematics,math,nlp,calculator,expression-parser,eval-alternative,safe-eval,multilingual,natural-language,postfix-notation,reverse-polish-notation,rpn,math-parser,expression-evaluator,secure-evaluation,language-processing,mathematical-expressions,formula-parser
|
|
11
|
-
Classifier: Development Status :: 4 - Beta
|
|
12
|
-
Classifier: Intended Audience :: Developers
|
|
13
|
-
Classifier: Operating System :: OS Independent
|
|
14
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
-
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
16
|
-
Classifier: Topic :: Text Processing :: General
|
|
17
|
-
Classifier: Topic :: Text Processing :: Linguistic
|
|
18
|
-
Classifier: Programming Language :: Python
|
|
19
|
-
Requires-Python: <3.15,>=3.10
|
|
20
|
-
Description-Content-Type: text/markdown
|
|
21
|
-
License-File: LICENSE
|
|
22
|
-
Provides-Extra: test
|
|
23
|
-
Requires-Dist: flake8; extra == "test"
|
|
24
|
-
Requires-Dist: sphinx<9.0,>=7.4; extra == "test"
|
|
25
|
-
Dynamic: license-file
|
|
26
|
-
|
|
27
|
-
# mathparse
|
|
28
|
-
|
|
29
|
-
**A secure, multilingual mathematical expression evaluator for Python**
|
|
30
|
-
|
|
31
|
-
`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.
|
|
32
|
-
|
|
33
|
-
## Why mathparse?
|
|
34
|
-
|
|
35
|
-
✅ **Security First** - Never uses `eval()`, protecting against arbitrary code execution
|
|
36
|
-
✅ **Multilingual Support** - Parse math in English, Spanish, French, German, Chinese, and 8+ more languages
|
|
37
|
-
✅ **Zero Dependencies** - Pure Python implementation with no external requirements
|
|
38
|
-
✅ **Natural Language** - Understands "fifty times twenty plus ten" alongside standard notation
|
|
39
|
-
✅ **Production Ready** - Used in chatbots, calculators, voice assistants, and educational applications
|
|
40
|
-
✅ **Well Tested** - Comprehensive test suite ensuring reliability
|
|
41
|
-
|
|
42
|
-
## Quick Examples
|
|
43
|
-
|
|
44
|
-
```python
|
|
45
|
-
from mathparse import mathparse
|
|
46
|
-
|
|
47
|
-
# Standard numeric expressions
|
|
48
|
-
mathparse.parse('50 * (85 / 100)')
|
|
49
|
-
>>> 42.5
|
|
50
|
-
|
|
51
|
-
# Natural language in English
|
|
52
|
-
mathparse.parse('one hundred times fifty four', language='ENG')
|
|
53
|
-
>>> 5400
|
|
54
|
-
|
|
55
|
-
# Mixed notation
|
|
56
|
-
mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
|
|
57
|
-
>>> 24
|
|
58
|
-
|
|
59
|
-
# Other languages (French, Spanish, German, Chinese, etc.)
|
|
60
|
-
mathparse.parse('cinq plus trois', language='FRE')
|
|
61
|
-
>>> 8
|
|
62
|
-
|
|
63
|
-
mathparse.parse('cinco más tres', language='ESP')
|
|
64
|
-
>>> 8
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
## Use Cases
|
|
68
|
-
|
|
69
|
-
- 🤖 **Chatbots & Voice Assistants** - Parse natural language math queries
|
|
70
|
-
- 🧮 **Calculator Applications** - Build safe calculators that accept text input
|
|
71
|
-
- 📚 **Educational Software** - Evaluate student-provided math expressions
|
|
72
|
-
- 🌐 **Multilingual Apps** - Support math parsing in users' native languages
|
|
73
|
-
- 🔐 **Secure Code Evaluation** - Replace dangerous `eval()` calls with safe parsing
|
|
74
|
-
- 📊 **Data Processing** - Extract and calculate values from natural language text
|
|
75
|
-
|
|
76
|
-
## Security: Why Not eval()?
|
|
77
|
-
|
|
78
|
-
Python's `eval()` function executes arbitrary code, creating severe security vulnerabilities:
|
|
79
|
-
|
|
80
|
-
```python
|
|
81
|
-
# DANGEROUS - Never do this with user input!
|
|
82
|
-
eval("__import__('os').system('rm -rf /')") # Deletes files
|
|
83
|
-
eval("__import__('requests').get('evil.com')") # Network access
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
**mathparse is the safe alternative:**
|
|
87
|
-
|
|
88
|
-
| Feature | eval() | mathparse |
|
|
89
|
-
|---------|--------|-----------|
|
|
90
|
-
| Mathematical expressions | ✅ | ✅ |
|
|
91
|
-
| Arbitrary code execution | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
92
|
-
| File system access | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
93
|
-
| Network access | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
94
|
-
| Import statements | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
95
|
-
| Security risk | 🔴 **CRITICAL** | 🟢 Safe |
|
|
96
|
-
| Dependencies | 0 | 0 |
|
|
97
|
-
| Natural language support | ❌ No | ✅ Yes |
|
|
98
|
-
| Multilingual | ❌ No | ✅ 13+ languages |
|
|
99
|
-
|
|
100
|
-
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.
|
|
101
|
-
|
|
102
|
-
## Performance
|
|
103
|
-
|
|
104
|
-
- **Fast**: Simple numeric expressions parse in microseconds
|
|
105
|
-
- **Efficient**: Minimal memory footprint, suitable for high-volume applications
|
|
106
|
-
- **Scalable**: Linear time complexity for expression evaluation
|
|
107
|
-
|
|
108
|
-
## Language Support
|
|
109
|
-
|
|
110
|
-
The language parameter must be set in order to evaluate an equation that uses word operators.
|
|
111
|
-
The language code should be a valid [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/code_list.php) language code.
|
|
112
|
-
|
|
113
|
-
## Installation
|
|
114
|
-
|
|
115
|
-
```bash
|
|
116
|
-
pip install mathparse
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
## Documentation
|
|
120
|
-
|
|
121
|
-
See the full documentation at https://mathparse.chatterbot.us
|
|
122
|
-
|
|
123
|
-
## Changelog
|
|
124
|
-
|
|
125
|
-
See [release notes](https://github.com/gunthercox/mathparse/releases) for changes.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: azrideus-mathparse
|
|
3
|
+
Version: 0.2.18
|
|
4
|
+
Summary: A fork of mathparse (https://github.com/gunthercox/mathparse) for my own usage; a library for solving mathematical equations contained in strings
|
|
5
|
+
Author: Gunther Cox
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Documentation, https://mathparse.chatterbot.us
|
|
8
|
+
Project-URL: Repository, https://github.com/gunthercox/mathparse
|
|
9
|
+
Project-URL: Changelog, https://github.com/gunthercox/mathparse/releases
|
|
10
|
+
Keywords: mathparse,mathematics,math,nlp,calculator,expression-parser,eval-alternative,safe-eval,multilingual,natural-language,postfix-notation,reverse-polish-notation,rpn,math-parser,expression-evaluator,secure-evaluation,language-processing,mathematical-expressions,formula-parser
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
16
|
+
Classifier: Topic :: Text Processing :: General
|
|
17
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
18
|
+
Classifier: Programming Language :: Python
|
|
19
|
+
Requires-Python: <3.15,>=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: flake8; extra == "test"
|
|
24
|
+
Requires-Dist: sphinx<9.0,>=7.4; extra == "test"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# mathparse
|
|
28
|
+
|
|
29
|
+
**A secure, multilingual mathematical expression evaluator for Python**
|
|
30
|
+
|
|
31
|
+
`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.
|
|
32
|
+
|
|
33
|
+
## Why mathparse?
|
|
34
|
+
|
|
35
|
+
✅ **Security First** - Never uses `eval()`, protecting against arbitrary code execution
|
|
36
|
+
✅ **Multilingual Support** - Parse math in English, Spanish, French, German, Chinese, and 8+ more languages
|
|
37
|
+
✅ **Zero Dependencies** - Pure Python implementation with no external requirements
|
|
38
|
+
✅ **Natural Language** - Understands "fifty times twenty plus ten" alongside standard notation
|
|
39
|
+
✅ **Production Ready** - Used in chatbots, calculators, voice assistants, and educational applications
|
|
40
|
+
✅ **Well Tested** - Comprehensive test suite ensuring reliability
|
|
41
|
+
|
|
42
|
+
## Quick Examples
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from mathparse import mathparse
|
|
46
|
+
|
|
47
|
+
# Standard numeric expressions
|
|
48
|
+
mathparse.parse('50 * (85 / 100)')
|
|
49
|
+
>>> 42.5
|
|
50
|
+
|
|
51
|
+
# Natural language in English
|
|
52
|
+
mathparse.parse('one hundred times fifty four', language='ENG')
|
|
53
|
+
>>> 5400
|
|
54
|
+
|
|
55
|
+
# Mixed notation
|
|
56
|
+
mathparse.parse('(seven * nine) + 8 - (45 plus two)', language='ENG')
|
|
57
|
+
>>> 24
|
|
58
|
+
|
|
59
|
+
# Other languages (French, Spanish, German, Chinese, etc.)
|
|
60
|
+
mathparse.parse('cinq plus trois', language='FRE')
|
|
61
|
+
>>> 8
|
|
62
|
+
|
|
63
|
+
mathparse.parse('cinco más tres', language='ESP')
|
|
64
|
+
>>> 8
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Use Cases
|
|
68
|
+
|
|
69
|
+
- 🤖 **Chatbots & Voice Assistants** - Parse natural language math queries
|
|
70
|
+
- 🧮 **Calculator Applications** - Build safe calculators that accept text input
|
|
71
|
+
- 📚 **Educational Software** - Evaluate student-provided math expressions
|
|
72
|
+
- 🌐 **Multilingual Apps** - Support math parsing in users' native languages
|
|
73
|
+
- 🔐 **Secure Code Evaluation** - Replace dangerous `eval()` calls with safe parsing
|
|
74
|
+
- 📊 **Data Processing** - Extract and calculate values from natural language text
|
|
75
|
+
|
|
76
|
+
## Security: Why Not eval()?
|
|
77
|
+
|
|
78
|
+
Python's `eval()` function executes arbitrary code, creating severe security vulnerabilities:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
# DANGEROUS - Never do this with user input!
|
|
82
|
+
eval("__import__('os').system('rm -rf /')") # Deletes files
|
|
83
|
+
eval("__import__('requests').get('evil.com')") # Network access
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**mathparse is the safe alternative:**
|
|
87
|
+
|
|
88
|
+
| Feature | eval() | mathparse |
|
|
89
|
+
|---------|--------|-----------|
|
|
90
|
+
| Mathematical expressions | ✅ | ✅ |
|
|
91
|
+
| Arbitrary code execution | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
92
|
+
| File system access | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
93
|
+
| Network access | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
94
|
+
| Import statements | ⚠️ **YES - DANGEROUS** | ❌ No |
|
|
95
|
+
| Security risk | 🔴 **CRITICAL** | 🟢 Safe |
|
|
96
|
+
| Dependencies | 0 | 0 |
|
|
97
|
+
| Natural language support | ❌ No | ✅ Yes |
|
|
98
|
+
| Multilingual | ❌ No | ✅ 13+ languages |
|
|
99
|
+
|
|
100
|
+
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.
|
|
101
|
+
|
|
102
|
+
## Performance
|
|
103
|
+
|
|
104
|
+
- **Fast**: Simple numeric expressions parse in microseconds
|
|
105
|
+
- **Efficient**: Minimal memory footprint, suitable for high-volume applications
|
|
106
|
+
- **Scalable**: Linear time complexity for expression evaluation
|
|
107
|
+
|
|
108
|
+
## Language Support
|
|
109
|
+
|
|
110
|
+
The language parameter must be set in order to evaluate an equation that uses word operators.
|
|
111
|
+
The language code should be a valid [ISO 639-2](https://www.loc.gov/standards/iso639-2/php/code_list.php) language code.
|
|
112
|
+
|
|
113
|
+
## Installation
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
pip install mathparse
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Documentation
|
|
120
|
+
|
|
121
|
+
See the full documentation at https://mathparse.chatterbot.us
|
|
122
|
+
|
|
123
|
+
## Changelog
|
|
124
|
+
|
|
125
|
+
See [release notes](https://github.com/gunthercox/mathparse/releases) for changes.
|
|
@@ -21,6 +21,22 @@ COMPARISON_FUNCTIONS = {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
|
|
24
|
+
def to_numbers(a, b) -> tuple:
|
|
25
|
+
"""
|
|
26
|
+
Convert two operands to numbers that can be used together.
|
|
27
|
+
Division returns a Decimal, which Python can not combine with a float,
|
|
28
|
+
so a float is converted to a Decimal when the other operand is one.
|
|
29
|
+
Example: (10 / 4) - 1.5 = Decimal('2.5') - Decimal('1.5')
|
|
30
|
+
"""
|
|
31
|
+
a = to_number(a)
|
|
32
|
+
b = to_number(b)
|
|
33
|
+
if isinstance(a, Decimal) and isinstance(b, float):
|
|
34
|
+
b = Decimal(str(b))
|
|
35
|
+
elif isinstance(b, Decimal) and isinstance(a, float):
|
|
36
|
+
a = Decimal(str(a))
|
|
37
|
+
return a, b
|
|
38
|
+
|
|
39
|
+
|
|
24
40
|
def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
25
41
|
"""
|
|
26
42
|
Given a list of evaluatable tokens in postfix format,
|
|
@@ -75,14 +91,16 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
75
91
|
total = mathwords.ArgumentList([to_number(a)])
|
|
76
92
|
total.append(to_number(b))
|
|
77
93
|
elif token == "+":
|
|
78
|
-
|
|
94
|
+
x, y = to_numbers(a, b)
|
|
95
|
+
total = x + y
|
|
79
96
|
elif token == "-":
|
|
80
|
-
|
|
97
|
+
x, y = to_numbers(a, b)
|
|
98
|
+
total = x - y
|
|
81
99
|
elif token == "*":
|
|
82
|
-
|
|
100
|
+
x, y = to_numbers(a, b)
|
|
101
|
+
total = x * y
|
|
83
102
|
elif token == "^":
|
|
84
|
-
base =
|
|
85
|
-
exponent = to_number(b)
|
|
103
|
+
base, exponent = to_numbers(a, b)
|
|
86
104
|
# Check the size of an integer power before computing it,
|
|
87
105
|
# since a huge power (e.g. 999999 ^ 999999999) would use all
|
|
88
106
|
# available memory. Float and Decimal powers instead raise an
|
|
@@ -106,9 +124,10 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
106
124
|
if to_number(b) == 0:
|
|
107
125
|
total = "undefined"
|
|
108
126
|
else:
|
|
109
|
-
|
|
127
|
+
x, y = to_numbers(a, b)
|
|
128
|
+
total = x % y
|
|
110
129
|
elif token in mathwords.COMPARISON_OPERATORS:
|
|
111
|
-
total = COMPARISON_FUNCTIONS[token](
|
|
130
|
+
total = COMPARISON_FUNCTIONS[token](*to_numbers(a, b))
|
|
112
131
|
elif token in mathwords.SHIFT_OPERATORS:
|
|
113
132
|
value = to_number(a)
|
|
114
133
|
count = to_number(b)
|
|
@@ -175,7 +194,13 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
|
|
|
175
194
|
# leading zeros (e.g., "01" has 2 digits, not 1)
|
|
176
195
|
digits = len(str(b))
|
|
177
196
|
divisor = 10**digits
|
|
178
|
-
|
|
197
|
+
# A Decimal keeps the exact value, so 0 . 1 + 0 . 2 = 0.3
|
|
198
|
+
# instead of 0.30000000000000004
|
|
199
|
+
fractional_part = Decimal(numeric_b) / divisor
|
|
200
|
+
# 'a' may be a float, such as the result of sqrt(4)
|
|
201
|
+
numeric_a, fractional_part = to_numbers(
|
|
202
|
+
numeric_a, fractional_part
|
|
203
|
+
)
|
|
179
204
|
|
|
180
205
|
# Handle negatives: -3 . 5 = -3.5, not -2.5
|
|
181
206
|
# Also -0 . 5 = -0.5 (check string since -0 == 0)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[egg_info]
|
|
2
|
-
tag_build =
|
|
3
|
-
tag_date = 0
|
|
4
|
-
|
|
1
|
+
[egg_info]
|
|
2
|
+
tag_build =
|
|
3
|
+
tag_date = 0
|
|
4
|
+
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from decimal import Decimal
|
|
1
2
|
from unittest import TestCase
|
|
2
3
|
from mathparse import mathparse, mathwords
|
|
3
4
|
|
|
@@ -92,7 +93,7 @@ class PositiveFloatTestCase(TestCase):
|
|
|
92
93
|
def test_addition(self):
|
|
93
94
|
result = mathparse.parse("0.6 + 0.5")
|
|
94
95
|
|
|
95
|
-
self.assertEqual(result, 1.1)
|
|
96
|
+
self.assertEqual(result, Decimal("1.1"))
|
|
96
97
|
|
|
97
98
|
def test_subtraction(self):
|
|
98
99
|
result = mathparse.parse("30.1 - 29.1")
|
|
@@ -102,7 +103,7 @@ class PositiveFloatTestCase(TestCase):
|
|
|
102
103
|
def test_multiplication(self):
|
|
103
104
|
result = mathparse.parse("0.9 * 0.9")
|
|
104
105
|
|
|
105
|
-
self.assertEqual(result, 0.81)
|
|
106
|
+
self.assertEqual(result, Decimal("0.81"))
|
|
106
107
|
|
|
107
108
|
def test_division(self):
|
|
108
109
|
result = mathparse.parse("0.6 / 0.2")
|
|
@@ -129,7 +130,7 @@ class WordBasedDecimalTestCase(TestCase):
|
|
|
129
130
|
"""
|
|
130
131
|
result = mathparse.parse("fifty three point four times seven", language="ENG")
|
|
131
132
|
|
|
132
|
-
self.assertEqual(result,
|
|
133
|
+
self.assertEqual(result, Decimal("373.8"))
|
|
133
134
|
|
|
134
135
|
def test_word_decimal_simple(self):
|
|
135
136
|
"""
|
|
@@ -137,7 +138,7 @@ class WordBasedDecimalTestCase(TestCase):
|
|
|
137
138
|
"""
|
|
138
139
|
result = mathparse.parse("five point two", language="ENG")
|
|
139
140
|
|
|
140
|
-
self.assertEqual(result, 5.2)
|
|
141
|
+
self.assertEqual(result, Decimal("5.2"))
|
|
141
142
|
|
|
142
143
|
def test_word_decimal_with_addition(self):
|
|
143
144
|
"""
|
|
@@ -145,7 +146,7 @@ class WordBasedDecimalTestCase(TestCase):
|
|
|
145
146
|
"""
|
|
146
147
|
result = mathparse.parse("three point five plus two point one", language="ENG")
|
|
147
148
|
|
|
148
|
-
self.assertEqual(result, 5.6)
|
|
149
|
+
self.assertEqual(result, Decimal("5.6"))
|
|
149
150
|
|
|
150
151
|
def test_word_decimal_with_division(self):
|
|
151
152
|
"""
|
|
@@ -296,3 +297,77 @@ class ConstantResultTestCase(TestCase):
|
|
|
296
297
|
def test_hyphenated_number_word_is_unaffected(self):
|
|
297
298
|
result = mathparse.parse('twenty-one plus one', language='ENG')
|
|
298
299
|
self.assertEqual(result, 22)
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
class MixedDecimalFloatTestCase(TestCase):
|
|
303
|
+
"""
|
|
304
|
+
Division returns a Decimal, which must still combine with floats.
|
|
305
|
+
"""
|
|
306
|
+
|
|
307
|
+
def test_division_minus_scientific_notation(self):
|
|
308
|
+
result = mathparse.parse("abs(170e6/32 - 8e6)/8e6")
|
|
309
|
+
|
|
310
|
+
self.assertEqual(result, Decimal("0.3359375"))
|
|
311
|
+
|
|
312
|
+
def test_division_plus_float(self):
|
|
313
|
+
result = mathparse.parse("10 / 4 + 0.5")
|
|
314
|
+
|
|
315
|
+
self.assertEqual(result, 3)
|
|
316
|
+
|
|
317
|
+
def test_division_minus_float(self):
|
|
318
|
+
result = mathparse.parse("10 / 4 - 1.5")
|
|
319
|
+
|
|
320
|
+
self.assertEqual(result, 1)
|
|
321
|
+
|
|
322
|
+
def test_float_minus_division(self):
|
|
323
|
+
result = mathparse.parse("1.5 - 10 / 4")
|
|
324
|
+
|
|
325
|
+
self.assertEqual(result, -1)
|
|
326
|
+
|
|
327
|
+
def test_division_times_scientific_notation(self):
|
|
328
|
+
result = mathparse.parse("10 / 4 * 2e1")
|
|
329
|
+
|
|
330
|
+
self.assertEqual(result, 50)
|
|
331
|
+
|
|
332
|
+
def test_division_modulo_float(self):
|
|
333
|
+
result = mathparse.parse("10 / 4 % 1.5")
|
|
334
|
+
|
|
335
|
+
self.assertEqual(result, 1)
|
|
336
|
+
|
|
337
|
+
def test_division_power_float(self):
|
|
338
|
+
result = mathparse.parse("(10 / 4) ^ 2e0")
|
|
339
|
+
|
|
340
|
+
self.assertEqual(result, Decimal("6.25"))
|
|
341
|
+
|
|
342
|
+
def test_division_compared_to_float(self):
|
|
343
|
+
result = mathparse.parse("10 / 4 == 2.5e0")
|
|
344
|
+
|
|
345
|
+
self.assertTrue(result)
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
class ExactFloatArithmeticTestCase(TestCase):
|
|
349
|
+
"""
|
|
350
|
+
Decimal literals are added, subtracted, multiplied and compared without
|
|
351
|
+
binary floating point error, the same way division already is.
|
|
352
|
+
"""
|
|
353
|
+
|
|
354
|
+
def test_float_addition(self):
|
|
355
|
+
self.assertEqual(mathparse.parse("0.1 + 0.2"), Decimal("0.3"))
|
|
356
|
+
|
|
357
|
+
def test_float_subtraction(self):
|
|
358
|
+
self.assertEqual(mathparse.parse("0.3 - 0.1"), Decimal("0.2"))
|
|
359
|
+
|
|
360
|
+
def test_float_multiplication(self):
|
|
361
|
+
self.assertEqual(mathparse.parse("1.1 * 3"), Decimal("3.3"))
|
|
362
|
+
|
|
363
|
+
def test_float_modulo(self):
|
|
364
|
+
self.assertEqual(mathparse.parse("1.1 % 0.5"), Decimal("0.1"))
|
|
365
|
+
|
|
366
|
+
def test_float_sum_compared_to_float(self):
|
|
367
|
+
self.assertTrue(mathparse.parse("0.1 + 0.2 == 0.3"))
|
|
368
|
+
|
|
369
|
+
def test_integer_arithmetic_stays_int(self):
|
|
370
|
+
result = mathparse.parse("4 * 4 + 2")
|
|
371
|
+
|
|
372
|
+
self.assertEqual(result, 18)
|
|
373
|
+
self.assertIsInstance(result, int)
|
|
@@ -1,138 +1,138 @@
|
|
|
1
|
-
from unittest import TestCase
|
|
2
|
-
from mathparse import mathparse
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class ComparisonTestCase(TestCase):
|
|
6
|
-
|
|
7
|
-
def test_less_than(self):
|
|
8
|
-
self.assertIs(mathparse.parse("3 < 5"), True)
|
|
9
|
-
self.assertIs(mathparse.parse("5 < 3"), False)
|
|
10
|
-
|
|
11
|
-
def test_greater_than(self):
|
|
12
|
-
self.assertIs(mathparse.parse("5 > 3"), True)
|
|
13
|
-
self.assertIs(mathparse.parse("3 > 5"), False)
|
|
14
|
-
|
|
15
|
-
def test_less_than_or_equal(self):
|
|
16
|
-
self.assertIs(mathparse.parse("3 <= 5"), True)
|
|
17
|
-
self.assertIs(mathparse.parse("5 <= 5"), True)
|
|
18
|
-
self.assertIs(mathparse.parse("6 <= 5"), False)
|
|
19
|
-
|
|
20
|
-
def test_greater_than_or_equal(self):
|
|
21
|
-
self.assertIs(mathparse.parse("5 >= 3"), True)
|
|
22
|
-
self.assertIs(mathparse.parse("5 >= 5"), True)
|
|
23
|
-
self.assertIs(mathparse.parse("4 >= 5"), False)
|
|
24
|
-
|
|
25
|
-
def test_equal(self):
|
|
26
|
-
self.assertIs(mathparse.parse("5 == 5"), True)
|
|
27
|
-
self.assertIs(mathparse.parse("5 == 4"), False)
|
|
28
|
-
|
|
29
|
-
def test_single_equals_is_alias_for_double_equals(self):
|
|
30
|
-
self.assertIs(mathparse.parse("2 + 2 = 4"), True)
|
|
31
|
-
self.assertIs(mathparse.parse("2 + 2 = 5"), False)
|
|
32
|
-
|
|
33
|
-
def test_not_equal(self):
|
|
34
|
-
self.assertIs(mathparse.parse("5 != 4"), True)
|
|
35
|
-
self.assertIs(mathparse.parse("5 != 5"), False)
|
|
36
|
-
|
|
37
|
-
def test_without_spaces(self):
|
|
38
|
-
self.assertIs(mathparse.parse("3<=5"), True)
|
|
39
|
-
self.assertIs(mathparse.parse("3>=5"), False)
|
|
40
|
-
self.assertIs(mathparse.parse("3==3"), True)
|
|
41
|
-
self.assertIs(mathparse.parse("3!=3"), False)
|
|
42
|
-
|
|
43
|
-
def test_tokenize_keeps_two_character_operators_together(self):
|
|
44
|
-
self.assertEqual(mathparse.tokenize("3<=5"), ["3", "<=", "5"])
|
|
45
|
-
self.assertEqual(mathparse.tokenize("3 >= 5"), ["3", ">=", "5"])
|
|
46
|
-
self.assertEqual(mathparse.tokenize("3 != 5"), ["3", "!=", "5"])
|
|
47
|
-
self.assertEqual(mathparse.tokenize("3 = 5"), ["3", "==", "5"])
|
|
48
|
-
|
|
49
|
-
def test_floats(self):
|
|
50
|
-
self.assertIs(mathparse.parse("2.5 < 2.75"), True)
|
|
51
|
-
self.assertIs(mathparse.parse("2.5 >= 2.75"), False)
|
|
52
|
-
|
|
53
|
-
def test_negative_numbers(self):
|
|
54
|
-
self.assertIs(mathparse.parse("-3 < -2"), True)
|
|
55
|
-
self.assertIs(mathparse.parse("3 <= -2"), False)
|
|
56
|
-
self.assertIs(mathparse.parse("-2 == -2"), True)
|
|
57
|
-
|
|
58
|
-
def test_lower_precedence_than_arithmetic(self):
|
|
59
|
-
# (1 + 2) <= 4, not 1 + (2 <= 4)
|
|
60
|
-
self.assertIs(mathparse.parse("1 + 2 <= 4"), True)
|
|
61
|
-
self.assertIs(mathparse.parse("2 * 3 > 5"), True)
|
|
62
|
-
self.assertIs(mathparse.parse("2 ^ 3 == 8"), True)
|
|
63
|
-
self.assertIs(mathparse.parse("10 % 4 != 2"), False)
|
|
64
|
-
|
|
65
|
-
def test_both_sides_are_evaluated(self):
|
|
66
|
-
self.assertIs(mathparse.parse("1 + 2 < 2 * 2"), True)
|
|
67
|
-
self.assertIs(mathparse.parse("10 / 2 >= 3 + 2"), True)
|
|
68
|
-
|
|
69
|
-
def test_parentheses(self):
|
|
70
|
-
self.assertIs(mathparse.parse("(3 < 5)"), True)
|
|
71
|
-
self.assertIs(mathparse.parse("(1 + 2) * 2 == 6"), True)
|
|
72
|
-
|
|
73
|
-
def test_unary_functions(self):
|
|
74
|
-
self.assertIs(mathparse.parse("sqrt 16 == 4"), True)
|
|
75
|
-
self.assertIs(mathparse.parse("sqrt 16 < 3"), False)
|
|
76
|
-
|
|
77
|
-
def test_constants(self):
|
|
78
|
-
self.assertIs(mathparse.parse("pi > 3"), True)
|
|
79
|
-
self.assertIs(mathparse.parse("e >= 3"), False)
|
|
80
|
-
|
|
81
|
-
def test_function_arguments(self):
|
|
82
|
-
self.assertIs(mathparse.parse("max(3, 7) == 7"), True)
|
|
83
|
-
self.assertIs(mathparse.parse("min(3, 7) > 3"), False)
|
|
84
|
-
|
|
85
|
-
def test_division_result(self):
|
|
86
|
-
self.assertIs(mathparse.parse("1 / 3 < 0.5"), True)
|
|
87
|
-
|
|
88
|
-
def test_result_can_be_used_in_arithmetic(self):
|
|
89
|
-
# True is 1 and False is 0
|
|
90
|
-
self.assertEqual(mathparse.parse("(3 < 5) + 1"), 2)
|
|
91
|
-
self.assertEqual(mathparse.parse("(3 > 5) + 1"), 1)
|
|
92
|
-
|
|
93
|
-
def test_chained_comparisons_are_left_associative(self):
|
|
94
|
-
# (1 < 2) < 3 is True < 3 is True
|
|
95
|
-
self.assertIs(mathparse.parse("1 < 2 < 3"), True)
|
|
96
|
-
# (3 < 2) == 0 is False == 0 is True
|
|
97
|
-
self.assertIs(mathparse.parse("3 < 2 == 0"), True)
|
|
98
|
-
|
|
99
|
-
def test_missing_operand(self):
|
|
100
|
-
with self.assertRaises(mathparse.PostfixTokenEvaluationException):
|
|
101
|
-
mathparse.parse("3 <=")
|
|
102
|
-
|
|
103
|
-
def test_is_symbol(self):
|
|
104
|
-
for operator in ["<", ">", "<=", ">=", "==", "!="]:
|
|
105
|
-
self.assertTrue(mathparse.is_symbol(operator))
|
|
106
|
-
self.assertTrue(mathparse.is_binary(operator))
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
class ComparisonWordsTestCase(TestCase):
|
|
110
|
-
|
|
111
|
-
def test_greater_than_words(self):
|
|
112
|
-
self.assertIs(mathparse.parse("five greater than three", language="ENG"), True)
|
|
113
|
-
|
|
114
|
-
def test_less_than_words(self):
|
|
115
|
-
self.assertIs(mathparse.parse("five less than three", language="ENG"), False)
|
|
116
|
-
|
|
117
|
-
def test_greater_than_or_equal_to_words(self):
|
|
118
|
-
result = mathparse.parse("five greater than or equal to five", language="ENG")
|
|
119
|
-
self.assertIs(result, True)
|
|
120
|
-
|
|
121
|
-
def test_less_than_or_equal_to_words(self):
|
|
122
|
-
result = mathparse.parse("six less than or equal to five", language="ENG")
|
|
123
|
-
self.assertIs(result, False)
|
|
124
|
-
|
|
125
|
-
def test_equals_words(self):
|
|
126
|
-
self.assertIs(mathparse.parse("two plus two equals four", language="ENG"), True)
|
|
127
|
-
self.assertIs(mathparse.parse("two plus two equal to five", language="ENG"), False)
|
|
128
|
-
|
|
129
|
-
def test_not_equal_to_words(self):
|
|
130
|
-
result = mathparse.parse("two plus two not equal to five", language="ENG")
|
|
131
|
-
self.assertIs(result, True)
|
|
132
|
-
|
|
133
|
-
def test_words_mixed_with_symbols(self):
|
|
134
|
-
self.assertIs(mathparse.parse("ten minus three >= 7", language="ENG"), True)
|
|
135
|
-
|
|
136
|
-
def test_extract_expression(self):
|
|
137
|
-
result = mathparse.extract_expression("Is it true that 3 + 4 >= 7?", language="ENG")
|
|
138
|
-
self.assertEqual(result, "3 + 4 >= 7")
|
|
1
|
+
from unittest import TestCase
|
|
2
|
+
from mathparse import mathparse
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class ComparisonTestCase(TestCase):
|
|
6
|
+
|
|
7
|
+
def test_less_than(self):
|
|
8
|
+
self.assertIs(mathparse.parse("3 < 5"), True)
|
|
9
|
+
self.assertIs(mathparse.parse("5 < 3"), False)
|
|
10
|
+
|
|
11
|
+
def test_greater_than(self):
|
|
12
|
+
self.assertIs(mathparse.parse("5 > 3"), True)
|
|
13
|
+
self.assertIs(mathparse.parse("3 > 5"), False)
|
|
14
|
+
|
|
15
|
+
def test_less_than_or_equal(self):
|
|
16
|
+
self.assertIs(mathparse.parse("3 <= 5"), True)
|
|
17
|
+
self.assertIs(mathparse.parse("5 <= 5"), True)
|
|
18
|
+
self.assertIs(mathparse.parse("6 <= 5"), False)
|
|
19
|
+
|
|
20
|
+
def test_greater_than_or_equal(self):
|
|
21
|
+
self.assertIs(mathparse.parse("5 >= 3"), True)
|
|
22
|
+
self.assertIs(mathparse.parse("5 >= 5"), True)
|
|
23
|
+
self.assertIs(mathparse.parse("4 >= 5"), False)
|
|
24
|
+
|
|
25
|
+
def test_equal(self):
|
|
26
|
+
self.assertIs(mathparse.parse("5 == 5"), True)
|
|
27
|
+
self.assertIs(mathparse.parse("5 == 4"), False)
|
|
28
|
+
|
|
29
|
+
def test_single_equals_is_alias_for_double_equals(self):
|
|
30
|
+
self.assertIs(mathparse.parse("2 + 2 = 4"), True)
|
|
31
|
+
self.assertIs(mathparse.parse("2 + 2 = 5"), False)
|
|
32
|
+
|
|
33
|
+
def test_not_equal(self):
|
|
34
|
+
self.assertIs(mathparse.parse("5 != 4"), True)
|
|
35
|
+
self.assertIs(mathparse.parse("5 != 5"), False)
|
|
36
|
+
|
|
37
|
+
def test_without_spaces(self):
|
|
38
|
+
self.assertIs(mathparse.parse("3<=5"), True)
|
|
39
|
+
self.assertIs(mathparse.parse("3>=5"), False)
|
|
40
|
+
self.assertIs(mathparse.parse("3==3"), True)
|
|
41
|
+
self.assertIs(mathparse.parse("3!=3"), False)
|
|
42
|
+
|
|
43
|
+
def test_tokenize_keeps_two_character_operators_together(self):
|
|
44
|
+
self.assertEqual(mathparse.tokenize("3<=5"), ["3", "<=", "5"])
|
|
45
|
+
self.assertEqual(mathparse.tokenize("3 >= 5"), ["3", ">=", "5"])
|
|
46
|
+
self.assertEqual(mathparse.tokenize("3 != 5"), ["3", "!=", "5"])
|
|
47
|
+
self.assertEqual(mathparse.tokenize("3 = 5"), ["3", "==", "5"])
|
|
48
|
+
|
|
49
|
+
def test_floats(self):
|
|
50
|
+
self.assertIs(mathparse.parse("2.5 < 2.75"), True)
|
|
51
|
+
self.assertIs(mathparse.parse("2.5 >= 2.75"), False)
|
|
52
|
+
|
|
53
|
+
def test_negative_numbers(self):
|
|
54
|
+
self.assertIs(mathparse.parse("-3 < -2"), True)
|
|
55
|
+
self.assertIs(mathparse.parse("3 <= -2"), False)
|
|
56
|
+
self.assertIs(mathparse.parse("-2 == -2"), True)
|
|
57
|
+
|
|
58
|
+
def test_lower_precedence_than_arithmetic(self):
|
|
59
|
+
# (1 + 2) <= 4, not 1 + (2 <= 4)
|
|
60
|
+
self.assertIs(mathparse.parse("1 + 2 <= 4"), True)
|
|
61
|
+
self.assertIs(mathparse.parse("2 * 3 > 5"), True)
|
|
62
|
+
self.assertIs(mathparse.parse("2 ^ 3 == 8"), True)
|
|
63
|
+
self.assertIs(mathparse.parse("10 % 4 != 2"), False)
|
|
64
|
+
|
|
65
|
+
def test_both_sides_are_evaluated(self):
|
|
66
|
+
self.assertIs(mathparse.parse("1 + 2 < 2 * 2"), True)
|
|
67
|
+
self.assertIs(mathparse.parse("10 / 2 >= 3 + 2"), True)
|
|
68
|
+
|
|
69
|
+
def test_parentheses(self):
|
|
70
|
+
self.assertIs(mathparse.parse("(3 < 5)"), True)
|
|
71
|
+
self.assertIs(mathparse.parse("(1 + 2) * 2 == 6"), True)
|
|
72
|
+
|
|
73
|
+
def test_unary_functions(self):
|
|
74
|
+
self.assertIs(mathparse.parse("sqrt 16 == 4"), True)
|
|
75
|
+
self.assertIs(mathparse.parse("sqrt 16 < 3"), False)
|
|
76
|
+
|
|
77
|
+
def test_constants(self):
|
|
78
|
+
self.assertIs(mathparse.parse("pi > 3"), True)
|
|
79
|
+
self.assertIs(mathparse.parse("e >= 3"), False)
|
|
80
|
+
|
|
81
|
+
def test_function_arguments(self):
|
|
82
|
+
self.assertIs(mathparse.parse("max(3, 7) == 7"), True)
|
|
83
|
+
self.assertIs(mathparse.parse("min(3, 7) > 3"), False)
|
|
84
|
+
|
|
85
|
+
def test_division_result(self):
|
|
86
|
+
self.assertIs(mathparse.parse("1 / 3 < 0.5"), True)
|
|
87
|
+
|
|
88
|
+
def test_result_can_be_used_in_arithmetic(self):
|
|
89
|
+
# True is 1 and False is 0
|
|
90
|
+
self.assertEqual(mathparse.parse("(3 < 5) + 1"), 2)
|
|
91
|
+
self.assertEqual(mathparse.parse("(3 > 5) + 1"), 1)
|
|
92
|
+
|
|
93
|
+
def test_chained_comparisons_are_left_associative(self):
|
|
94
|
+
# (1 < 2) < 3 is True < 3 is True
|
|
95
|
+
self.assertIs(mathparse.parse("1 < 2 < 3"), True)
|
|
96
|
+
# (3 < 2) == 0 is False == 0 is True
|
|
97
|
+
self.assertIs(mathparse.parse("3 < 2 == 0"), True)
|
|
98
|
+
|
|
99
|
+
def test_missing_operand(self):
|
|
100
|
+
with self.assertRaises(mathparse.PostfixTokenEvaluationException):
|
|
101
|
+
mathparse.parse("3 <=")
|
|
102
|
+
|
|
103
|
+
def test_is_symbol(self):
|
|
104
|
+
for operator in ["<", ">", "<=", ">=", "==", "!="]:
|
|
105
|
+
self.assertTrue(mathparse.is_symbol(operator))
|
|
106
|
+
self.assertTrue(mathparse.is_binary(operator))
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class ComparisonWordsTestCase(TestCase):
|
|
110
|
+
|
|
111
|
+
def test_greater_than_words(self):
|
|
112
|
+
self.assertIs(mathparse.parse("five greater than three", language="ENG"), True)
|
|
113
|
+
|
|
114
|
+
def test_less_than_words(self):
|
|
115
|
+
self.assertIs(mathparse.parse("five less than three", language="ENG"), False)
|
|
116
|
+
|
|
117
|
+
def test_greater_than_or_equal_to_words(self):
|
|
118
|
+
result = mathparse.parse("five greater than or equal to five", language="ENG")
|
|
119
|
+
self.assertIs(result, True)
|
|
120
|
+
|
|
121
|
+
def test_less_than_or_equal_to_words(self):
|
|
122
|
+
result = mathparse.parse("six less than or equal to five", language="ENG")
|
|
123
|
+
self.assertIs(result, False)
|
|
124
|
+
|
|
125
|
+
def test_equals_words(self):
|
|
126
|
+
self.assertIs(mathparse.parse("two plus two equals four", language="ENG"), True)
|
|
127
|
+
self.assertIs(mathparse.parse("two plus two equal to five", language="ENG"), False)
|
|
128
|
+
|
|
129
|
+
def test_not_equal_to_words(self):
|
|
130
|
+
result = mathparse.parse("two plus two not equal to five", language="ENG")
|
|
131
|
+
self.assertIs(result, True)
|
|
132
|
+
|
|
133
|
+
def test_words_mixed_with_symbols(self):
|
|
134
|
+
self.assertIs(mathparse.parse("ten minus three >= 7", language="ENG"), True)
|
|
135
|
+
|
|
136
|
+
def test_extract_expression(self):
|
|
137
|
+
result = mathparse.extract_expression("Is it true that 3 + 4 >= 7?", language="ENG")
|
|
138
|
+
self.assertEqual(result, "3 + 4 >= 7")
|
{azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/tests/test_prefix_unary_operations.py
RENAMED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
from decimal import Decimal
|
|
1
2
|
from unittest import TestCase
|
|
2
3
|
from mathparse import mathparse
|
|
3
4
|
|
|
@@ -279,48 +280,48 @@ class NegativeDecimalsLessThanOneTestCase(TestCase):
|
|
|
279
280
|
Test: -0.2 should equal -0.2
|
|
280
281
|
"""
|
|
281
282
|
result = mathparse.parse('-0.2')
|
|
282
|
-
self.assertEqual(result, -0.2)
|
|
283
|
+
self.assertEqual(result, Decimal("-0.2"))
|
|
283
284
|
|
|
284
285
|
def test_negative_zero_point_five(self):
|
|
285
286
|
"""
|
|
286
287
|
Test: -0.5 should equal -0.5
|
|
287
288
|
"""
|
|
288
289
|
result = mathparse.parse('-0.5')
|
|
289
|
-
self.assertEqual(result, -0.5)
|
|
290
|
+
self.assertEqual(result, Decimal("-0.5"))
|
|
290
291
|
|
|
291
292
|
def test_negative_zero_point_nine(self):
|
|
292
293
|
"""
|
|
293
294
|
Test: -0.9 should equal -0.9
|
|
294
295
|
"""
|
|
295
296
|
result = mathparse.parse('-0.9')
|
|
296
|
-
self.assertEqual(result, -0.9)
|
|
297
|
+
self.assertEqual(result, Decimal("-0.9"))
|
|
297
298
|
|
|
298
299
|
def test_negative_zero_point_one(self):
|
|
299
300
|
"""
|
|
300
301
|
Test: -0.1 should equal -0.1
|
|
301
302
|
"""
|
|
302
303
|
result = mathparse.parse('-0.1')
|
|
303
|
-
self.assertEqual(result, -0.1)
|
|
304
|
+
self.assertEqual(result, Decimal("-0.1"))
|
|
304
305
|
|
|
305
306
|
def test_negative_zero_point_ninety_nine(self):
|
|
306
307
|
"""
|
|
307
308
|
Test: -0.99 should equal -0.99"""
|
|
308
309
|
result = mathparse.parse('-0.99')
|
|
309
|
-
self.assertEqual(result, -0.99)
|
|
310
|
+
self.assertEqual(result, Decimal("-0.99"))
|
|
310
311
|
|
|
311
312
|
def test_negative_zero_point_zero_one(self):
|
|
312
313
|
"""
|
|
313
314
|
Test: -0.01 should equal -0.01
|
|
314
315
|
"""
|
|
315
316
|
result = mathparse.parse('-0.01')
|
|
316
|
-
self.assertEqual(result, -0.01)
|
|
317
|
+
self.assertEqual(result, Decimal("-0.01"))
|
|
317
318
|
|
|
318
319
|
def test_negative_zero_point_nine_nine_nine(self):
|
|
319
320
|
"""
|
|
320
321
|
Test: -0.999 should equal -0.999
|
|
321
322
|
"""
|
|
322
323
|
result = mathparse.parse('-0.999')
|
|
323
|
-
self.assertEqual(result, -0.999)
|
|
324
|
+
self.assertEqual(result, Decimal("-0.999"))
|
|
324
325
|
|
|
325
326
|
def test_negative_decimal_in_addition(self):
|
|
326
327
|
"""
|
|
File without changes
|
|
File without changes
|
{azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/azrideus_mathparse.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
{azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/azrideus_mathparse.egg-info/requires.txt
RENAMED
|
File without changes
|
{azrideus_mathparse-0.2.15 → azrideus_mathparse-0.2.18}/azrideus_mathparse.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|