azrideus-mathparse 0.2.11__tar.gz → 0.2.12__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.
Files changed (22) hide show
  1. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/PKG-INFO +1 -1
  2. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/PKG-INFO +1 -1
  3. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/SOURCES.txt +1 -0
  4. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/mathparse/__init__.py +1 -1
  5. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/mathparse/mathparse.py +225 -158
  6. azrideus_mathparse-0.2.12/tests/test_limits.py +70 -0
  7. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/LICENSE +0 -0
  8. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/README.md +0 -0
  9. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/dependency_links.txt +0 -0
  10. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/requires.txt +0 -0
  11. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/azrideus_mathparse.egg-info/top_level.txt +0 -0
  12. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/mathparse/mathwords.py +0 -0
  13. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/pyproject.toml +0 -0
  14. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/setup.cfg +0 -0
  15. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/tests/test_binary_operations.py +0 -0
  16. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/tests/test_complex_statements.py +0 -0
  17. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/tests/test_language_support.py +0 -0
  18. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/tests/test_prefix_unary_operations.py +0 -0
  19. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/tests/test_replace_word_tokens.py +0 -0
  20. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/tests/test_simplified_chinese.py +0 -0
  21. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/tests/test_unary_operations.py +0 -0
  22. {azrideus_mathparse-0.2.11 → azrideus_mathparse-0.2.12}/tests/test_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: azrideus-mathparse
3
- Version: 0.2.11
3
+ Version: 0.2.12
4
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
5
  Author: Gunther Cox
6
6
  License-Expression: MIT
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: azrideus-mathparse
3
- Version: 0.2.11
3
+ Version: 0.2.12
4
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
5
  Author: Gunther Cox
6
6
  License-Expression: MIT
@@ -12,6 +12,7 @@ mathparse/mathwords.py
12
12
  tests/test_binary_operations.py
13
13
  tests/test_complex_statements.py
14
14
  tests/test_language_support.py
15
+ tests/test_limits.py
15
16
  tests/test_prefix_unary_operations.py
16
17
  tests/test_replace_word_tokens.py
17
18
  tests/test_simplified_chinese.py
@@ -2,4 +2,4 @@
2
2
  mathparse is a library for solving mathematical equations contained in strings
3
3
  """
4
4
 
5
- __version__ = "0.2.11"
5
+ __version__ = "0.2.12"
@@ -1,25 +1,63 @@
1
1
  """
2
2
  Methods for evaluating mathematical equations in strings.
3
3
  """
4
- from decimal import Decimal
4
+
5
+ from decimal import Decimal, DecimalException
5
6
  from typing import Union
6
7
  from . import mathwords
8
+ import math
7
9
  import re
8
10
 
9
11
 
12
+ # Limits that protect against expressions which would take too long or use
13
+ # too much memory to evaluate, such as "999999 ** 999999999". Either limit can
14
+ # be changed, or set to None to disable it.
15
+
16
+ # The maximum number of characters in an expression passed to parse()
17
+ MAX_EXPRESSION_LENGTH = 1000
18
+
19
+ # The maximum number of digits in any integer computed during evaluation
20
+ MAX_INTEGER_DIGITS = 50
21
+
22
+
10
23
  class PostfixTokenEvaluationException(Exception):
11
24
  """
12
25
  Exception to be raised when an expression cannot be evaluated.
13
26
  """
27
+
14
28
  pass
15
29
 
16
30
 
31
+ class ExpressionTooLargeException(PostfixTokenEvaluationException):
32
+ """
33
+ Exception to be raised when an expression, or a number computed while
34
+ evaluating it, exceeds the configured size limits.
35
+ """
36
+
37
+ pass
38
+
39
+
40
+ def check_integer_digits(log10_value: float, token: str) -> None:
41
+ """
42
+ Raise an ExpressionTooLargeException if an integer whose base 10
43
+ logarithm is (approximately) log10_value has more than MAX_INTEGER_DIGITS
44
+ digits.
45
+ """
46
+ # An integer n has floor(log10(n)) + 1 digits
47
+ if MAX_INTEGER_DIGITS is not None and log10_value >= MAX_INTEGER_DIGITS:
48
+ raise ExpressionTooLargeException(
49
+ 'The result of "{}" would have more than {} digits'.format(
50
+ token, MAX_INTEGER_DIGITS
51
+ )
52
+ )
53
+
54
+
17
55
  def is_int(string: str) -> bool:
18
56
  """
19
57
  Return true if string is an integer.
20
58
  """
21
59
  # Allow leading minus sign
22
- if string[0] == '-':
60
+ if string[0] == "-":
23
61
  # Must have at least one digit after the minus
24
62
  return len(string) > 1 and string[1:].isdigit()
25
63
  else:
@@ -34,7 +72,7 @@ def is_float(string: str) -> bool:
34
72
  try:
35
73
  float(string)
36
74
  # Exponent format numbers such as "25e2" are also floats
37
- return '.' in string or 'e' in string
75
+ return "." in string or "e" in string
38
76
  except ValueError:
39
77
  return False
40
78
 
@@ -66,10 +104,13 @@ def is_symbol(string: str) -> bool:
66
104
  Return true if the string is a mathematical symbol.
67
105
  """
68
106
  return (
69
- is_int(string) or is_float(string) or
70
- is_constant(string) or is_unary(string) or
71
- is_binary(string) or
72
- (string == '(') or (string == ')')
107
+ is_int(string)
108
+ or is_float(string)
109
+ or is_constant(string)
110
+ or is_unary(string)
111
+ or is_binary(string)
112
+ or (string == "(")
113
+ or (string == ")")
73
114
  )
74
115
 
75
116
 
@@ -105,7 +146,7 @@ def to_number(val) -> Union[int, float, str, Decimal]:
105
146
 
106
147
  # Numbers in exponent format, such as "2.5e3", "1e-6" or ".5E+2"
107
148
  SCIENTIFIC_NOTATION_PATTERN = re.compile(
108
- r'(?<![\w.])(\d+\.?\d*|\.\d+)e([+-]?\d+)(?![\w.])'
149
+ r"(?<![\w.])(\d+\.?\d*|\.\d+)e([+-]?\d+)(?![\w.])"
109
150
  )
110
151
 
111
152
 
@@ -115,9 +156,10 @@ def normalize_scientific_notation(string: str) -> str:
115
156
  that they are kept as a single token, rather than being split apart by the
116
157
  '.' and '+' binary operators. For example "2.5e+3" becomes "25e2".
117
158
  """
159
+
118
160
  def replace(match):
119
161
  _, digits, exponent = Decimal(match.group(0)).as_tuple()
120
- return '{}e{}'.format(''.join(str(d) for d in digits), exponent)
162
+ return "{}e{}".format("".join(str(d) for d in digits), exponent)
121
163
 
122
164
  return SCIENTIFIC_NOTATION_PATTERN.sub(replace, string)
123
165
 
@@ -143,9 +185,9 @@ def create_unicode_word_boundary_pattern(word: str) -> str:
143
185
  '(?:^|(?<=[\\s+\\-*/^()]))दो(?:$|(?=[\\s+\\-*/^()]))'
144
186
  """
145
187
  # Check if word contains only ASCII alphanumeric characters
146
- if word.isascii() and word.replace('-', '').replace("'", '').isalnum():
188
+ if word.isascii() and word.replace("-", "").replace("'", "").isalnum():
147
189
  # For ASCII words, use standard word boundaries
148
- return r'\b' + re.escape(word) + r'\b'
190
+ return r"\b" + re.escape(word) + r"\b"
149
191
  else:
150
192
  # For non-ASCII (Unicode) words, use space/operator boundaries
151
193
  # This pattern matches the word when it's:
@@ -153,8 +195,8 @@ def create_unicode_word_boundary_pattern(word: str) -> str:
153
195
  # - At the end of string OR followed by whitespace/operator
154
196
  escaped_word = re.escape(word)
155
197
  # Boundary characters: whitespace, operators, parentheses
156
- boundary = r'[\s+\-*/^()]'
157
- return f'(?:^|(?<={boundary})){escaped_word}(?:$|(?={boundary}))'
198
+ boundary = r"[\s+\-*/^()]"
199
+ return f"(?:^|(?<={boundary})){escaped_word}(?:$|(?={boundary}))"
158
200
 
159
201
 
160
202
  def find_word_groups(string: str, words: list) -> list:
@@ -163,29 +205,24 @@ def find_word_groups(string: str, words: list) -> list:
163
205
  The words parameter should be the list of words to check for
164
206
  such as "hundred".
165
207
  """
166
- scale_pattern = '|'.join(words)
208
+ scale_pattern = "|".join(words)
167
209
  # For example:
168
210
  # (?:(?:\d+)\s+(?:hundred|thousand)*\s*)+(?:\d+|hundred|thousand)+
169
211
  regex = re.compile(
170
- r'(?:(?:\d+)\s+(?:' +
171
- scale_pattern +
172
- r')*\s*)+(?:\d+|' +
173
- scale_pattern + r')+'
212
+ r"(?:(?:\d+)\s+(?:" + scale_pattern + r")*\s*)+(?:\d+|" + scale_pattern + r")+"
174
213
  )
175
214
  result = regex.findall(string)
176
215
  return result
177
216
 
178
217
 
179
- def replace_word_tokens_simplified_chinese(
180
- string, stopwords: set[str] = None
181
- ) -> str:
218
+ def replace_word_tokens_simplified_chinese(string, stopwords: set[str] = None) -> str:
182
219
  """
183
220
  simplified Chinese version:
184
221
  Given a string and an ISO 639-2 language code,
185
222
  return the string with the words replaced with
186
223
  an operational equivalent.
187
224
  """
188
- words = mathwords.word_groups_for_language('CHI')
225
+ words = mathwords.word_groups_for_language("CHI")
189
226
 
190
227
  # Handle special Chinese power construction patterns FIRST
191
228
  # In Chinese, '的...次方' forms a power construction where:
@@ -194,8 +231,8 @@ def replace_word_tokens_simplified_chinese(
194
231
  # - '次幂' is similar to '次方'
195
232
  # Example: 二的四次方 = 2's 4th power = 2 ^ 4
196
233
  # We need to remove '次方' and '次幂' BEFORE they get replaced as operators
197
- string = string.replace('次方', ' ')
198
- string = string.replace('次幂', ' ')
234
+ string = string.replace("次方", " ")
235
+ string = string.replace("次幂", " ")
199
236
 
200
237
  # Collect all operators (binary, prefix unary, postfix unary)
201
238
  # and process them by length (longest first) to handle cases where
@@ -204,14 +241,14 @@ def replace_word_tokens_simplified_chinese(
204
241
  all_operators = {}
205
242
 
206
243
  # Add binary operators
207
- all_operators.update(words['binary_operators'])
244
+ all_operators.update(words["binary_operators"])
208
245
 
209
246
  # Add prefix unary operators
210
- all_operators.update(words['prefix_unary_operators'])
247
+ all_operators.update(words["prefix_unary_operators"])
211
248
 
212
249
  # Add postfix unary operators
213
- if 'postfix_unary_operators' in words:
214
- all_operators.update(words['postfix_unary_operators'])
250
+ if "postfix_unary_operators" in words:
251
+ all_operators.update(words["postfix_unary_operators"])
215
252
 
216
253
  # Sort all operators by length (longest first)
217
254
  sorted_operators = sorted(all_operators.keys(), key=len, reverse=True)
@@ -219,36 +256,34 @@ def replace_word_tokens_simplified_chinese(
219
256
  for operator in sorted_operators:
220
257
  if operator in string:
221
258
  # 中文没有分隔符,后面需要靠分隔符分割式子,每次识别一个符号都将其分开来
222
- string = string.replace(
223
- operator, ' ' + all_operators[operator] + ' '
224
- )
259
+ string = string.replace(operator, " " + all_operators[operator] + " ")
225
260
 
226
261
  # chinese_scales用list的原因是为了保持从大到小的顺序,亿、万、千...
227
262
  # Sort scales by their numeric value (largest first) to ensure correct
228
263
  # parsing. For example, '千' (1000) should be found before '百' (100)
229
- digits = set(words['numbers'].keys())
264
+ digits = set(words["numbers"].keys())
230
265
  scales = sorted(
231
- words['scales'].keys(), key=lambda x: words['scales'][x], reverse=True
266
+ words["scales"].keys(), key=lambda x: words["scales"][x], reverse=True
232
267
  )
233
- digits_scales = words['numbers'].copy()
234
- digits_scales.update(words['scales'])
268
+ digits_scales = words["numbers"].copy()
269
+ digits_scales.update(words["scales"])
235
270
 
236
271
  # 九千八百万九千八百——> 98009800
237
272
  def chinese_string_to_num(str):
238
- if str == '':
273
+ if str == "":
239
274
  return 0
240
275
 
241
276
  if str in digits:
242
277
  return digits_scales[str]
243
278
 
244
- digits_scales.update(words['scales'])
279
+ digits_scales.update(words["scales"])
245
280
 
246
281
  for scale in scales:
247
282
  index = str.find(scale)
248
283
  if index >= 0:
249
284
  t1 = chinese_string_to_num(str[:index])
250
285
  t2 = digits_scales[scale]
251
- t3 = chinese_string_to_num(str[index + 1:])
286
+ t3 = chinese_string_to_num(str[index + 1 :])
252
287
  return t1 * t2 + t3
253
288
  else:
254
289
  return digits_scales[str]
@@ -266,7 +301,7 @@ def replace_word_tokens_simplified_chinese(
266
301
  if start < end:
267
302
  num = str(chinese_string_to_num(string[start:end]))
268
303
  # 需要加多一个分隔符
269
- num_str = str(num) + ' '
304
+ num_str = str(num) + " "
270
305
  string = string[:start] + num_str + string[end:]
271
306
  index = start + len(num_str)
272
307
  start = end = index
@@ -277,7 +312,7 @@ def replace_word_tokens_simplified_chinese(
277
312
  if index >= len(string):
278
313
  if start < end:
279
314
  num = str(chinese_string_to_num(string[start:end]))
280
- num_str = str(num) + ' '
315
+ num_str = str(num) + " "
281
316
  string = string[:start] + num_str + string[end:]
282
317
 
283
318
  break
@@ -285,9 +320,7 @@ def replace_word_tokens_simplified_chinese(
285
320
  return string
286
321
 
287
322
 
288
- def replace_word_tokens(
289
- string: str, language: str, stopwords: set[str] = None
290
- ) -> str:
323
+ def replace_word_tokens(string: str, language: str, stopwords: set[str] = None) -> str:
291
324
  """
292
325
  Replace word-based mathematical terms with their symbolic equivalents.
293
326
 
@@ -312,15 +345,15 @@ def replace_word_tokens(
312
345
  words = mathwords.word_groups_for_language(language)
313
346
 
314
347
  # Process operators by length (longest first) to handle compound operators
315
- binary_operators = words['binary_operators']
348
+ binary_operators = words["binary_operators"]
316
349
  sorted_operators = sorted(binary_operators.keys(), key=len, reverse=True)
317
350
  for operator in sorted_operators:
318
351
  if operator in string:
319
352
  string = string.replace(operator, binary_operators[operator])
320
353
 
321
354
  # Handle prefix unary operators (like "square root of")
322
- if 'prefix_unary_operators' in words:
323
- prefix_unary_operators = words['prefix_unary_operators']
355
+ if "prefix_unary_operators" in words:
356
+ prefix_unary_operators = words["prefix_unary_operators"]
324
357
  # Sort by length to handle compound operators where a shorter token
325
358
  # is a substring of a longer token
326
359
  sorted_prefix_operators = sorted(
@@ -328,13 +361,11 @@ def replace_word_tokens(
328
361
  )
329
362
  for operator in sorted_prefix_operators:
330
363
  if operator in string:
331
- string = string.replace(
332
- operator, prefix_unary_operators[operator]
333
- )
364
+ string = string.replace(operator, prefix_unary_operators[operator])
334
365
 
335
366
  # Handle postfix unary operators (like "squared", "cubed")
336
- if 'postfix_unary_operators' in words:
337
- postfix_unary_operators = words['postfix_unary_operators']
367
+ if "postfix_unary_operators" in words:
368
+ postfix_unary_operators = words["postfix_unary_operators"]
338
369
  # Sort by length to handle compound operators where a shorter token
339
370
  # is a substring of a longer token
340
371
  sorted_postfix_operators = sorted(
@@ -343,38 +374,42 @@ def replace_word_tokens(
343
374
  for operator in sorted_postfix_operators:
344
375
  if operator in string:
345
376
  # Captures the number/operand before the unary operator
346
- pattern = r'(\w+)\s+' + re.escape(operator)
347
- replacement = r'(\1 ' + postfix_unary_operators[operator] + ')'
377
+ pattern = r"(\w+)\s+" + re.escape(operator)
378
+ replacement = r"(\1 " + postfix_unary_operators[operator] + ")"
348
379
  string = re.sub(pattern, replacement, string)
349
380
 
350
381
  # Handle compound numbers:
351
382
  # (e.g., "twenty one" -> "(20 + 1)", "fifty four" -> "(50 + 4)")
352
- numbers = words['numbers']
383
+ numbers = words["numbers"]
353
384
 
354
385
  # Create regex patterns for compound numbers
355
386
  # Pattern matches: (tens_word) (units_word) where tens_word is
356
387
  # 20,30,40,...,90 and units_word is 1-9
357
- tens_words = {word: value for word, value in numbers.items() if value in [
358
- 20, 30, 40, 50, 60, 70, 80, 90
359
- ]}
360
- units_words = {word: value for word, value in numbers.items() if value in [
361
- 1, 2, 3, 4, 5, 6, 7, 8, 9
362
- ]}
388
+ tens_words = {
389
+ word: value
390
+ for word, value in numbers.items()
391
+ if value in [20, 30, 40, 50, 60, 70, 80, 90]
392
+ }
393
+ units_words = {
394
+ word: value
395
+ for word, value in numbers.items()
396
+ if value in [1, 2, 3, 4, 5, 6, 7, 8, 9]
397
+ }
363
398
 
364
399
  # Preprocess hyphenated compound numbers
365
400
  # (e.g., "fifty-four" -> "fifty four")
366
401
  for tens_word in tens_words.keys():
367
402
  for units_word in units_words.keys():
368
- hyphenated_pattern = tens_word + r'-' + units_word
369
- space_separated = tens_word + ' ' + units_word
403
+ hyphenated_pattern = tens_word + r"-" + units_word
404
+ space_separated = tens_word + " " + units_word
370
405
  if re.search(hyphenated_pattern, string):
371
406
  string = re.sub(hyphenated_pattern, space_separated, string)
372
407
 
373
408
  # Replace compound numbers first (before individual number replacement)
374
409
  for tens_word, tens_value in tens_words.items():
375
410
  for units_word, units_value in units_words.items():
376
- compound_pattern = tens_word + r'\s+' + units_word
377
- compound_replacement = f'({tens_value} + {units_value})'
411
+ compound_pattern = tens_word + r"\s+" + units_word
412
+ compound_replacement = f"({tens_value} + {units_value})"
378
413
  if re.search(compound_pattern, string):
379
414
  string = re.sub(compound_pattern, compound_replacement, string)
380
415
 
@@ -394,16 +429,16 @@ def replace_word_tokens(
394
429
  for word in string.split():
395
430
  if word not in stopwords:
396
431
  filtered_words.append(word)
397
- string = ' '.join(filtered_words)
432
+ string = " ".join(filtered_words)
398
433
 
399
434
  # Replace scaling multipliers with numeric values
400
- scales = words['scales']
401
- end_index_characters = mathwords.BINARY_OPERATORS | {'('}
435
+ scales = words["scales"]
436
+ end_index_characters = mathwords.BINARY_OPERATORS | {"("}
402
437
 
403
438
  word_matches = find_word_groups(string, frozenset(scales.keys()))
404
439
 
405
440
  for match in word_matches:
406
- string = string.replace(match, '(' + match + ')')
441
+ string = string.replace(match, "(" + match + ")")
407
442
 
408
443
  for scale in frozenset(scales.keys()):
409
444
  for _ in range(0, string.count(scale)):
@@ -413,24 +448,21 @@ def replace_word_tokens(
413
448
  while is_int(string[start_index - 1]) and start_index > 0:
414
449
  start_index -= 1
415
450
 
416
- end_index = string.find(' ', start_index) + 1
417
- end_index = string.find(' ', end_index) + 1
451
+ end_index = string.find(" ", start_index) + 1
452
+ end_index = string.find(" ", end_index) + 1
418
453
 
419
- add = ' + '
454
+ add = " + "
420
455
  if string[end_index] in end_index_characters:
421
- add = ''
456
+ add = ""
422
457
 
423
- string = string[:start_index] + '(' + string[start_index:]
424
- string = string.replace(
425
- scale, '* ' + str(scales[scale]) + ')' + add,
426
- 1
427
- )
458
+ string = string[:start_index] + "(" + string[start_index:]
459
+ string = string.replace(scale, "* " + str(scales[scale]) + ")" + add, 1)
428
460
 
429
- string = string.replace(') (', ') + (')
461
+ string = string.replace(") (", ") + (")
430
462
 
431
463
  # Remove extra parentheses around all scale expressions to simplify output
432
464
  # Convert ((number * scale)) to (number * scale) for all scales
433
- string = re.sub(r'\(\((\d+\s*\*\s*\d+)\)\)', r'(\1)', string)
465
+ string = re.sub(r"\(\((\d+\s*\*\s*\d+)\)\)", r"(\1)", string)
434
466
 
435
467
  return string
436
468
 
@@ -451,10 +483,10 @@ def preprocess_unary_operators(tokens: list) -> list:
451
483
  processed_tokens = []
452
484
 
453
485
  # A following minus sign should be treated as unary (negative)
454
- unary_contexts = mathwords.BINARY_OPERATORS | {'('}
486
+ unary_contexts = mathwords.BINARY_OPERATORS | {"("}
455
487
 
456
488
  for i, token in enumerate(tokens):
457
- if token == '-':
489
+ if token == "-":
458
490
  # Check if this minus should be treated as unary
459
491
  is_unary_minus = False
460
492
 
@@ -470,7 +502,7 @@ def preprocess_unary_operators(tokens: list) -> list:
470
502
 
471
503
  if is_unary_minus:
472
504
  # Convert the unary minus to 'neg' function
473
- processed_tokens.append('neg')
505
+ processed_tokens.append("neg")
474
506
  else:
475
507
  # Keep as binary minus
476
508
  processed_tokens.append(token)
@@ -485,17 +517,17 @@ def to_postfix(tokens: list) -> list:
485
517
  Convert a list of evaluatable tokens to postfix format.
486
518
  """
487
519
  precedence = {
488
- '.': 5,
489
- '^': 4,
490
- '/': 3,
491
- '*': 3,
492
- '%': 3,
493
- '+': 2,
494
- '-': 2,
520
+ ".": 5,
521
+ "^": 4,
522
+ "/": 3,
523
+ "*": 3,
524
+ "%": 3,
525
+ "+": 2,
526
+ "-": 2,
495
527
  # Commas separate function arguments, so every other operator within
496
528
  # an argument is evaluated first
497
- ',': 1,
498
- '(': 0
529
+ ",": 1,
530
+ "(": 0,
499
531
  }
500
532
 
501
533
  # Unary functions have a higher precedence than binary operators
@@ -503,7 +535,7 @@ def to_postfix(tokens: list) -> list:
503
535
 
504
536
  # Right-associative operators use strict inequality when popping,
505
537
  # so that e.g. 2^3^2 evaluates as 2^(3^2) = 512, not (2^3)^2 = 64
506
- right_associative = {'^'}
538
+ right_associative = {"^"}
507
539
 
508
540
  postfix = []
509
541
  opstack = []
@@ -517,11 +549,11 @@ def to_postfix(tokens: list) -> list:
517
549
  postfix.append(token)
518
550
  elif is_unary(token):
519
551
  opstack.append(token)
520
- elif token == '(':
552
+ elif token == "(":
521
553
  opstack.append(token)
522
- elif token == ')':
554
+ elif token == ")":
523
555
  top_token = opstack.pop()
524
- while top_token != '(':
556
+ while top_token != "(":
525
557
  postfix.append(top_token)
526
558
  top_token = opstack.pop()
527
559
  elif is_binary(token):
@@ -534,22 +566,24 @@ def to_postfix(tokens: list) -> list:
534
566
  # A decimal point directly after a number is part of that number
535
567
  # (e.g. "round 2.5" is round(2.5), not round(2) . 5), so it must
536
568
  # not cause a preceding unary function to be applied early
537
- is_decimal_part = (
538
- token == '.' and index > 0 and tokens[index - 1] != ')'
539
- )
569
+ is_decimal_part = token == "." and index > 0 and tokens[index - 1] != ")"
540
570
 
541
571
  while (opstack != []) and (
542
572
  (
543
- opstack[-1] in precedence and token in precedence and (
544
- precedence[opstack[-1]] > precedence[token] or (
545
- is_left_assoc and
546
- precedence[opstack[-1]] == precedence[token]
573
+ opstack[-1] in precedence
574
+ and token in precedence
575
+ and (
576
+ precedence[opstack[-1]] > precedence[token]
577
+ or (
578
+ is_left_assoc
579
+ and precedence[opstack[-1]] == precedence[token]
547
580
  )
548
581
  )
549
- ) or
550
- (
551
- is_unary(opstack[-1]) and not is_decimal_part and
552
- unary_precedence >= precedence[token]
582
+ )
583
+ or (
584
+ is_unary(opstack[-1])
585
+ and not is_decimal_part
586
+ and unary_precedence >= precedence[token]
553
587
  )
554
588
  ):
555
589
  postfix.append(opstack.pop())
@@ -583,8 +617,12 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
583
617
  # Convert token (string) to number for unary function evaluation
584
618
  a = to_number(a)
585
619
  function = mathwords.UNARY_FUNCTIONS[token]
620
+ # Check the size of n! (using log(n!) = lgamma(n + 1)) before
621
+ # computing it, since a large factorial is slow to calculate
622
+ if function is math.factorial and isinstance(a, int) and a > 1:
623
+ check_integer_digits(math.lgamma(a + 1) / math.log(10), token)
586
624
  if isinstance(a, mathwords.ArgumentList) and not getattr(
587
- function, 'variadic', False
625
+ function, "variadic", False
588
626
  ):
589
627
  raise PostfixTokenEvaluationException(
590
628
  'Function "{}" takes a single argument'.format(token)
@@ -592,24 +630,22 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
592
630
  total = function(a)
593
631
  elif len(stack) == 1:
594
632
  raise PostfixTokenEvaluationException(
595
- 'Insufficient values in expression for operator "{}"'.format(
596
- token
597
- )
633
+ 'Insufficient values in expression for operator "{}"'.format(token)
598
634
  )
599
635
  elif len(stack) > 1:
600
636
  b = stack.pop()
601
637
  a = stack.pop()
602
638
 
603
- if token != ',' and (
604
- isinstance(a, mathwords.ArgumentList) or
605
- isinstance(b, mathwords.ArgumentList)
639
+ if token != "," and (
640
+ isinstance(a, mathwords.ArgumentList)
641
+ or isinstance(b, mathwords.ArgumentList)
606
642
  ):
607
643
  raise PostfixTokenEvaluationException(
608
- 'Comma separated values can only be used as function '
609
- 'arguments, such as max(3, 7)'
644
+ "Comma separated values can only be used as function "
645
+ "arguments, such as max(3, 7)"
610
646
  )
611
647
 
612
- if token == ',':
648
+ if token == ",":
613
649
  # Collect comma separated function arguments into a list
614
650
  # Example: 3 , 7 , 5 = [3, 7, 5]
615
651
  if isinstance(a, mathwords.ArgumentList):
@@ -617,30 +653,40 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
617
653
  else:
618
654
  total = mathwords.ArgumentList([to_number(a)])
619
655
  total.append(to_number(b))
620
- elif token == '+':
656
+ elif token == "+":
621
657
  total = to_number(a) + to_number(b)
622
- elif token == '-':
658
+ elif token == "-":
623
659
  total = to_number(a) - to_number(b)
624
- elif token == '*':
660
+ elif token == "*":
625
661
  total = to_number(a) * to_number(b)
626
- elif token == '^':
627
- total = to_number(a) ** to_number(b)
628
- elif token == '/':
662
+ elif token == "^":
663
+ base = to_number(a)
664
+ exponent = to_number(b)
665
+ # Check the size of an integer power before computing it,
666
+ # since a huge power (e.g. 999999 ^ 999999999) would use all
667
+ # available memory. Float and Decimal powers instead raise an
668
+ # overflow error quickly.
669
+ if (
670
+ isinstance(base, int)
671
+ and isinstance(exponent, int)
672
+ and exponent > 0
673
+ and abs(base) > 1
674
+ ):
675
+ check_integer_digits(exponent * math.log10(abs(base)), token)
676
+ total = base**exponent
677
+ elif token == "/":
629
678
  # Convert tokens such as constants (e.g. "pi") to numbers
630
679
  # before passing them to Decimal
631
680
  if to_number(b) == 0:
632
- total = 'undefined'
681
+ total = "undefined"
633
682
  else:
634
- total = (
635
- Decimal(str(to_number(a))) /
636
- Decimal(str(to_number(b)))
637
- )
638
- elif token == '%':
683
+ total = Decimal(str(to_number(a))) / Decimal(str(to_number(b)))
684
+ elif token == "%":
639
685
  if to_number(b) == 0:
640
- total = 'undefined'
686
+ total = "undefined"
641
687
  else:
642
688
  total = to_number(a) % to_number(b)
643
- elif token == '.':
689
+ elif token == ".":
644
690
  # Treat decimal points as a binary operator that combines the
645
691
  # integer and fractional part of two numbers
646
692
  # Example: 53 . 25 = 53.25, -3 . 5 = -3.5
@@ -654,7 +700,7 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
654
700
  total = Decimal(numeric_a)
655
701
  else:
656
702
  # Check if 'a' has a negative sign (handles -0 case)
657
- is_negative = str(a).startswith('-')
703
+ is_negative = str(a).startswith("-")
658
704
 
659
705
  # Convert a to number for calculation
660
706
  numeric_a = to_number(a)
@@ -662,7 +708,7 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
662
708
  # Count the digits in the original string b to preserve
663
709
  # leading zeros (e.g., "01" has 2 digits, not 1)
664
710
  digits = len(str(b))
665
- divisor = 10 ** digits
711
+ divisor = 10**digits
666
712
  fractional_part = numeric_b / divisor
667
713
 
668
714
  # Handle negatives: -3 . 5 = -3.5, not -2.5
@@ -676,21 +722,27 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
676
722
  'Unknown token "{}"'.format(token)
677
723
  )
678
724
 
725
+ # Limit the size of every computed integer, which also prevents
726
+ # repeated operations such as multiplication from growing a number
727
+ # without bound
728
+ if isinstance(total, int) and total:
729
+ check_integer_digits((total.bit_length() - 1) * math.log10(2), token)
730
+
679
731
  if total is not None:
680
732
  stack.append(total)
681
733
 
682
734
  # If the stack is empty the tokens could not be evaluated
683
735
  if not stack:
684
736
  raise PostfixTokenEvaluationException(
685
- 'The postfix expression resulted in an empty stack'
737
+ "The postfix expression resulted in an empty stack"
686
738
  )
687
739
 
688
740
  result = stack.pop()
689
741
 
690
742
  if isinstance(result, mathwords.ArgumentList):
691
743
  raise PostfixTokenEvaluationException(
692
- 'Comma separated values can only be used as function arguments, '
693
- 'such as max(3, 7)'
744
+ "Comma separated values can only be used as function arguments, "
745
+ "such as max(3, 7)"
694
746
  )
695
747
 
696
748
  # Convert final result from string (such as "4" or a constant like "pi")
@@ -701,7 +753,7 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
701
753
  return result
702
754
 
703
755
 
704
- def tokenize(string: str, language: str = None, escape: str = '___') -> list:
756
+ def tokenize(string: str, language: str = None, escape: str = "___") -> list:
705
757
  """
706
758
  Convert a string into a list of mathematical tokens for processing.
707
759
 
@@ -728,7 +780,7 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
728
780
  # Ignore punctuation
729
781
  if len(string) and not string[-1].isalnum():
730
782
  character = string[-1]
731
- string = string[:-1] + ' ' + character
783
+ string = string[:-1] + " " + character
732
784
 
733
785
  # If language is specified, normalize compound operators by removing
734
786
  # spaces between their characters. This handles cases like '乘 以'
@@ -747,18 +799,18 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
747
799
  if len(phrase) > 1:
748
800
  # Create pattern with optional spaces between each character
749
801
  # For example, '乘以' could appear as '乘 以' or '乘 以'
750
- spaced_phrase = ' '.join(phrase)
802
+ spaced_phrase = " ".join(phrase)
751
803
  # Replace spaced version with non-spaced version
752
804
  string = string.replace(spaced_phrase, phrase)
753
805
 
754
806
  # Python style power operator is an alias for '^'. This must be replaced
755
807
  # before '*' is split apart as a binary operator below.
756
- string = string.replace('**', '^')
808
+ string = string.replace("**", "^")
757
809
 
758
810
  # Binary operators must have space around them to be tokenized properly
759
811
  # Special handling for minus sign: preserve leading negatives
760
812
  for operator in mathwords.BINARY_OPERATORS:
761
- if operator == '-':
813
+ if operator == "-":
762
814
  # For minus sign, use a pattern that only spaces it when it's
763
815
  # clearly a binary operator (after digits or closing parenthesis).
764
816
  # This preserves leading negatives like "-3" and distinguishes
@@ -769,30 +821,30 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
769
821
  # operator)
770
822
  # Only add spaces around minus when preceded by a digit or closing
771
823
  # parenthesis
772
- string = re.sub(r'([\d)])\s*-\s*', r'\1 - ', string)
824
+ string = re.sub(r"([\d)])\s*-\s*", r"\1 - ", string)
773
825
  # Separate a leading minus from a following constant or function
774
826
  # name (e.g. "-pi" or "-cos 0") so that it becomes a unary minus.
775
827
  # A minus directly after a word character (as in "twenty-one") is
776
828
  # left alone.
777
- string = re.sub(r'(?<![\w.])-(?=[^\W\d_])', '- ', string)
829
+ string = re.sub(r"(?<![\w.])-(?=[^\W\d_])", "- ", string)
778
830
  else:
779
- string = string.replace(operator, f' {operator} ')
831
+ string = string.replace(operator, f" {operator} ")
780
832
 
781
833
  # Parenthesis must have space around them to be tokenized properly
782
- string = string.replace('(', ' ( ')
783
- string = string.replace(')', ' ) ')
834
+ string = string.replace("(", " ( ")
835
+ string = string.replace(")", " ) ")
784
836
 
785
837
  if language:
786
838
  words = mathwords.words_for_language(language)
787
839
 
788
840
  for phrase in words:
789
- escaped_phrase = phrase.replace(' ', escape)
841
+ escaped_phrase = phrase.replace(" ", escape)
790
842
  string = string.replace(phrase, escaped_phrase)
791
843
 
792
844
  tokens = string.split()
793
845
 
794
846
  for index, token in enumerate(tokens):
795
- tokens[index] = token.replace(escape, ' ')
847
+ tokens[index] = token.replace(escape, " ")
796
848
 
797
849
  return tokens
798
850
 
@@ -857,12 +909,17 @@ def parse(
857
909
  - Supports unary functions: sqrt, log
858
910
  - Each expression must use terms from a single language
859
911
  - Division by zero returns 'undefined' instead of raising an exception
912
+ - Raises ExpressionTooLargeException if the expression is longer than
913
+ MAX_EXPRESSION_LENGTH, or a result would be too large to compute
860
914
  """
915
+ if MAX_EXPRESSION_LENGTH is not None and len(string) > MAX_EXPRESSION_LENGTH:
916
+ raise ExpressionTooLargeException(
917
+ "The expression is longer than {} characters".format(MAX_EXPRESSION_LENGTH)
918
+ )
919
+
861
920
  if language:
862
- if language == 'CHI':
863
- string = replace_word_tokens_simplified_chinese(
864
- string, stopwords
865
- )
921
+ if language == "CHI":
922
+ string = replace_word_tokens_simplified_chinese(string, stopwords)
866
923
  else:
867
924
  string = replace_word_tokens(string, language, stopwords)
868
925
 
@@ -870,7 +927,14 @@ def parse(
870
927
  tokens = preprocess_unary_operators(tokens)
871
928
  postfix = to_postfix(tokens)
872
929
 
873
- return evaluate_postfix(postfix)
930
+ try:
931
+ return evaluate_postfix(postfix)
932
+ except (OverflowError, DecimalException) as error:
933
+ # Float and Decimal operations raise these when a result is too large
934
+ # (e.g. 10.0 ^ 400 or exp(1000))
935
+ raise ExpressionTooLargeException(
936
+ "The result is too large to compute: {}".format(error)
937
+ ) from error
874
938
 
875
939
 
876
940
  def extract_expression(dirty_string: str, language: str) -> str:
@@ -923,7 +987,7 @@ def extract_expression(dirty_string: str, language: str) -> str:
923
987
  # operator. Binary operators (except '(') are only valid at the
924
988
  # start if they are unary (such as a leading minus for a negative
925
989
  # number)
926
- if part in mathwords.BINARY_OPERATORS and part != '(':
990
+ if part in mathwords.BINARY_OPERATORS and part != "(":
927
991
  # For a binary operator to be the start of an expression, it
928
992
  # must be:
929
993
  # 1. At the very beginning (position 0) - could be unary minus
@@ -944,9 +1008,12 @@ def extract_expression(dirty_string: str, language: str) -> str:
944
1008
  if all_prev_non_math and i + 1 < len(tokens):
945
1009
  next_token = tokens[i + 1]
946
1010
  if (
947
- is_int(next_token) or is_float(next_token) or
948
- is_constant(next_token) or is_unary(next_token) or
949
- next_token == '(' or is_word(next_token, language)
1011
+ is_int(next_token)
1012
+ or is_float(next_token)
1013
+ or is_constant(next_token)
1014
+ or is_unary(next_token)
1015
+ or next_token == "("
1016
+ or is_word(next_token, language)
950
1017
  ):
951
1018
  start_index = i
952
1019
  break
@@ -962,10 +1029,10 @@ def extract_expression(dirty_string: str, language: str) -> str:
962
1029
  else:
963
1030
  end_index -= 1
964
1031
 
965
- result = ' '.join(tokens[start_index:end_index])
1032
+ result = " ".join(tokens[start_index:end_index])
966
1033
 
967
1034
  # Remove spaces around decimal points for cleaner output
968
1035
  # Replace " . " with "." to convert "-100 . 5" to "-100.5"
969
- result = result.replace(' . ', '.')
1036
+ result = result.replace(" . ", ".")
970
1037
 
971
1038
  return result
@@ -0,0 +1,70 @@
1
+ from unittest import TestCase
2
+ from mathparse import mathparse
3
+
4
+
5
+ class ExpressionLimitTestCase(TestCase):
6
+
7
+ def setUp(self):
8
+ self.max_expression_length = mathparse.MAX_EXPRESSION_LENGTH
9
+ self.max_integer_digits = mathparse.MAX_INTEGER_DIGITS
10
+
11
+ def tearDown(self):
12
+ mathparse.MAX_EXPRESSION_LENGTH = self.max_expression_length
13
+ mathparse.MAX_INTEGER_DIGITS = self.max_integer_digits
14
+
15
+ def test_huge_power(self):
16
+ with self.assertRaises(mathparse.ExpressionTooLargeException):
17
+ mathparse.parse('999999**999999999')
18
+
19
+ def test_huge_power_tower(self):
20
+ with self.assertRaises(mathparse.ExpressionTooLargeException):
21
+ mathparse.parse('9 ^ 9 ^ 9')
22
+
23
+ def test_power_at_limit(self):
24
+ # 10 ^ 999 has exactly 1000 digits
25
+ self.assertEqual(mathparse.parse('10 ^ 999'), 10 ** 999)
26
+
27
+ def test_power_over_limit(self):
28
+ with self.assertRaises(mathparse.ExpressionTooLargeException):
29
+ mathparse.parse('10 ^ 1000')
30
+
31
+ def test_power_of_one_is_not_limited(self):
32
+ self.assertEqual(mathparse.parse('1 ^ 999999999'), 1)
33
+
34
+ def test_huge_factorial(self):
35
+ with self.assertRaises(mathparse.ExpressionTooLargeException):
36
+ mathparse.parse('factorial 10000000')
37
+
38
+ def test_repeated_multiplication(self):
39
+ with self.assertRaises(mathparse.ExpressionTooLargeException):
40
+ mathparse.parse('(2 ^ 3000) * (2 ^ 3000)')
41
+
42
+ def test_float_overflow(self):
43
+ with self.assertRaises(mathparse.ExpressionTooLargeException):
44
+ mathparse.parse('1.5 ** 999999999')
45
+
46
+ def test_function_overflow(self):
47
+ with self.assertRaises(mathparse.ExpressionTooLargeException):
48
+ mathparse.parse('exp(1000)')
49
+
50
+ def test_long_expression(self):
51
+ with self.assertRaises(mathparse.ExpressionTooLargeException):
52
+ mathparse.parse('1' + ' + 1' * 500)
53
+
54
+ def test_is_postfix_evaluation_exception(self):
55
+ # Existing handlers for evaluation errors also catch size errors
56
+ with self.assertRaises(mathparse.PostfixTokenEvaluationException):
57
+ mathparse.parse('999999 ^ 999999999')
58
+
59
+ def test_custom_integer_digit_limit(self):
60
+ mathparse.MAX_INTEGER_DIGITS = 10
61
+ with self.assertRaises(mathparse.ExpressionTooLargeException):
62
+ mathparse.parse('10 ^ 10')
63
+
64
+ def test_disabled_integer_digit_limit(self):
65
+ mathparse.MAX_INTEGER_DIGITS = None
66
+ self.assertEqual(mathparse.parse('2 ^ 5000'), 2 ** 5000)
67
+
68
+ def test_disabled_expression_length_limit(self):
69
+ mathparse.MAX_EXPRESSION_LENGTH = None
70
+ self.assertEqual(mathparse.parse('1' + ' + 1' * 500), 501)