|
@@ -1,5 +1,7 @@
|
|
import datetime
|
|
import datetime
|
|
|
|
+import ioex
|
|
import ioex.calcex
|
|
import ioex.calcex
|
|
|
|
+import ioex.reex
|
|
import locale
|
|
import locale
|
|
import pytz
|
|
import pytz
|
|
import re
|
|
import re
|
|
@@ -99,27 +101,28 @@ class Sum(ioex.calcex.Figure):
|
|
sum_regex_currency_first = r'(?P<currency>{}) ?(?P<value>{})'.format(
|
|
sum_regex_currency_first = r'(?P<currency>{}) ?(?P<value>{})'.format(
|
|
currency_regex, value_regex,
|
|
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
|
|
@staticmethod
|
|
def parse_text(text):
|
|
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,
|
|
text,
|
|
)
|
|
)
|
|
- attr = match.groupdict()
|
|
|
|
|
|
+ attr = ioex.dict_collapse(
|
|
|
|
+ match.groupdict(),
|
|
|
|
+ lambda k: k.split('_')[-1],
|
|
|
|
+ )
|
|
return Sum(
|
|
return Sum(
|
|
value=locale.atof(attr['value']),
|
|
value=locale.atof(attr['value']),
|
|
currency=attr['currency'],
|
|
currency=attr['currency'],
|