Browse Source

Sum.sum_regex: single regex string instead of multiple patterns (list)

Fabian Peter Hammerle 7 years ago
parent
commit
7c261f9a2e
3 changed files with 42 additions and 17 deletions
  1. 17 14
      finoex/__init__.py
  2. 3 3
      setup.py
  3. 22 0
      tests/test_sum.py

+ 17 - 14
finoex/__init__.py

@@ -1,5 +1,7 @@
 import datetime
+import ioex
 import ioex.calcex
+import ioex.reex
 import locale
 import pytz
 import re
@@ -99,27 +101,28 @@ class Sum(ioex.calcex.Figure):
     sum_regex_currency_first = r'(?P<currency>{}) ?(?P<value>{})'.format(
         currency_regex, value_regex,
     )
-
-    sum_regex = [
-        r'^\$?(?P<value>{}) (?P<currency>{})$'.format(
-            value_regex, currency_regex,
+    sum_regex = r'({})'.format('|'.join([
+        ioex.reex.rename_groups(
+            sum_regex_value_first,
+            lambda n: {'value': 'pre_value', 'currency': 'post_currency'}[n]
         ),
-        r'^(?P<currency>{}) ?(?P<value>{})$'.format(
-            currency_regex, value_regex,
+        ioex.reex.rename_groups(
+            sum_regex_currency_first,
+            lambda n: {'currency': 'pre_currency', 'value': 'post_value'}[n]
         ),
-    ]
+    ]))
 
     @staticmethod
     def parse_text(text):
-        for pattern in Sum.sum_regex:
-            match = re.search(pattern, text, re.UNICODE)
-            if match:
-                break
-        assert not match is None, '\n{}\ntext: {!r}'.format(
-            '\n'.join(Sum.sum_regex),
+        match = re.search('^{}$'.format(Sum.sum_regex), text, re.UNICODE)
+        assert not match is None, '\nregex: {}\ntext: {!r}'.format(
+            Sum.sum_regex,
             text,
         )
-        attr = match.groupdict()
+        attr = ioex.dict_collapse(
+            match.groupdict(),
+            lambda k: k.split('_')[-1],
+        )
         return Sum(
             value=locale.atof(attr['value']),
             currency=attr['currency'],

+ 3 - 3
setup.py

@@ -4,18 +4,18 @@ import glob
 
 setup(
     name = 'finoex',
-    version = '0.9.1',
+    version = '0.10.0',
     # description = '',
     author = 'Fabian Peter Hammerle',
     author_email = 'fabian.hammerle@gmail.com',
     url = 'https://git.hammerle.me/fphammerle/finoex',
-    download_url = 'https://git.hammerle.me/fphammerle/finoex/archive/0.7.0.tar.gz',
+    download_url = 'https://git.hammerle.me/fphammerle/finoex/archive/0.10.0.tar.gz',
     keywords = ['finances'],
     # classifiers = [],
     packages = ['finoex'],
     # scripts = glob.glob('scripts/*'),
     install_requires = [
-        'ioex>=0.10.1',
+        'ioex>=0.13.0',
         'pytz',
         ],
     tests_require = ['pytest'],

+ 22 - 0
tests/test_sum.py

@@ -151,3 +151,25 @@ def test_sum_regex_currency_first(haystack, expected_needles):
         haystack,
     )
     assert expected_needles == [m.groupdict() for m in matches]
+
+
+@pytest.mark.parametrize(('haystack', 'expected_needles'), [
+    ["0,50 US$", [{'post_currency': 'US$', 'pre_value': '0,50'}]],
+    ["0,50 €", [{'post_currency': '€', 'pre_value': '0,50'}]],
+    ["1234 ¥", [{'post_currency': '¥', 'pre_value': '1234'}]],
+    ["1.23 US$", [{'post_currency': 'US$', 'pre_value': '1.23'}]],
+    ["1.23 €", [{'post_currency': '€', 'pre_value': '1.23'}]],
+    ["US$ 0,50", [{'pre_currency': 'US$', 'post_value': '0,50'}]],
+    ["€ 0,50", [{'pre_currency': '€', 'post_value': '0,50'}]],
+    ["¥1234", [{'pre_currency': '¥', 'post_value': '1234'}]],
+    ["US$ 1.23", [{'pre_currency': 'US$', 'post_value': '1.23'}]],
+    ["€1.23", [{'pre_currency': '€', 'post_value': '1.23'}]],
+])
+def test_sum_regex(haystack, expected_needles):
+    matches = re.finditer(
+        finoex.Sum.sum_regex,
+        haystack,
+    )
+    attr = [{k: v for k, v in m.groupdict().items() if v is not None}
+            for m in matches]
+    assert expected_needles == attr