azrideus-mathparse 0.2.9__tar.gz → 0.2.10__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.9 → azrideus_mathparse-0.2.10}/PKG-INFO +1 -1
  2. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/azrideus_mathparse.egg-info/PKG-INFO +1 -1
  3. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/mathparse/__init__.py +1 -1
  4. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/mathparse/mathparse.py +34 -1
  5. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/mathparse/mathwords.py +1779 -1777
  6. azrideus_mathparse-0.2.10/tests/test_binary_operations.py +257 -0
  7. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/tests/test_unary_operations.py +5 -3
  8. azrideus_mathparse-0.2.9/tests/test_binary_operations.py +0 -174
  9. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/LICENSE +0 -0
  10. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/README.md +0 -0
  11. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/azrideus_mathparse.egg-info/SOURCES.txt +0 -0
  12. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/azrideus_mathparse.egg-info/dependency_links.txt +0 -0
  13. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/azrideus_mathparse.egg-info/requires.txt +0 -0
  14. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/azrideus_mathparse.egg-info/top_level.txt +0 -0
  15. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/pyproject.toml +0 -0
  16. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/setup.cfg +0 -0
  17. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/tests/test_complex_statements.py +0 -0
  18. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/tests/test_language_support.py +0 -0
  19. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/tests/test_prefix_unary_operations.py +0 -0
  20. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/tests/test_replace_word_tokens.py +0 -0
  21. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/tests/test_simplified_chinese.py +0 -0
  22. {azrideus_mathparse-0.2.9 → azrideus_mathparse-0.2.10}/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.9
3
+ Version: 0.2.10
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.9
3
+ Version: 0.2.10
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
@@ -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.9'
5
+ __version__ = "0.2.10"
@@ -33,7 +33,8 @@ def is_float(string: str) -> bool:
33
33
  """
34
34
  try:
35
35
  float(string)
36
- return '.' in string
36
+ # Exponent format numbers such as "25e2" are also floats
37
+ return '.' in string or 'e' in string
37
38
  except ValueError:
38
39
  return False
39
40
 
@@ -102,6 +103,25 @@ def to_number(val) -> Union[int, float, str, Decimal]:
102
103
  return val
103
104
 
104
105
 
106
+ # Numbers in exponent format, such as "2.5e3", "1e-6" or ".5E+2"
107
+ SCIENTIFIC_NOTATION_PATTERN = re.compile(
108
+ r'(?<![\w.])(\d+\.?\d*|\.\d+)e([+-]?\d+)(?![\w.])'
109
+ )
110
+
111
+
112
+ def normalize_scientific_notation(string: str) -> str:
113
+ """
114
+ Rewrite exponent format numbers without decimal points or plus signs so
115
+ that they are kept as a single token, rather than being split apart by the
116
+ '.' and '+' binary operators. For example "2.5e+3" becomes "25e2".
117
+ """
118
+ def replace(match):
119
+ _, digits, exponent = Decimal(match.group(0)).as_tuple()
120
+ return '{}e{}'.format(''.join(str(d) for d in digits), exponent)
121
+
122
+ return SCIENTIFIC_NOTATION_PATTERN.sub(replace, string)
123
+
124
+
105
125
  def create_unicode_word_boundary_pattern(word: str) -> str:
106
126
  """
107
127
  Create a regex pattern with Unicode-aware word boundaries.
@@ -469,6 +489,7 @@ def to_postfix(tokens: list) -> list:
469
489
  '^': 4,
470
490
  '/': 3,
471
491
  '*': 3,
492
+ '%': 3,
472
493
  '+': 2,
473
494
  '-': 2,
474
495
  '(': 1
@@ -575,6 +596,11 @@ def evaluate_postfix(tokens: list) -> Union[int, float, str, Decimal]:
575
596
  total = 'undefined'
576
597
  else:
577
598
  total = Decimal(str(a)) / Decimal(str(b))
599
+ elif token == '%':
600
+ if Decimal(str(b)) == 0:
601
+ total = 'undefined'
602
+ else:
603
+ total = to_number(a) % to_number(b)
578
604
  elif token == '.':
579
605
  # Treat decimal points as a binary operator that combines the
580
606
  # integer and fractional part of two numbers
@@ -653,6 +679,9 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
653
679
  # Set all words to lowercase
654
680
  string = string.lower()
655
681
 
682
+ # Keep exponent format numbers (e.g. 2.5e3) together as single tokens
683
+ string = normalize_scientific_notation(string)
684
+
656
685
  # Ignore punctuation
657
686
  if len(string) and not string[-1].isalnum():
658
687
  character = string[-1]
@@ -679,6 +708,10 @@ def tokenize(string: str, language: str = None, escape: str = '___') -> list:
679
708
  # Replace spaced version with non-spaced version
680
709
  string = string.replace(spaced_phrase, phrase)
681
710
 
711
+ # Python style power operator is an alias for '^'. This must be replaced
712
+ # before '*' is split apart as a binary operator below.
713
+ string = string.replace('**', '^')
714
+
682
715
  # Binary operators must have space around them to be tokenized properly
683
716
  # Special handling for minus sign: preserve leading negatives
684
717
  for operator in mathwords.BINARY_OPERATORS: