1
|
|
|
# pyre-strict |
2
|
|
|
|
3
|
1 |
|
""" |
4
|
|
|
This module helps you work with payment texts such as |
5
|
|
|
"*4321 29.06 USD 50.00 ITUNES.COM/BILL Rate: 1.0000". |
6
|
|
|
""" |
7
|
1 |
|
from re import compile as compile_regex |
8
|
1 |
|
from typing import List, Any |
9
|
|
|
|
10
|
1 |
|
import iso4217parse |
11
|
|
|
|
12
|
|
|
|
13
|
1 |
|
class PaymentText: |
14
|
|
|
""" |
15
|
|
|
Use this class to represent your payment text. |
16
|
|
|
""" |
17
|
1 |
|
parts: List[str] = [] |
18
|
|
|
|
19
|
1 |
|
def __init__(self, text: str) -> None: |
20
|
1 |
|
self.parts = text.split() |
21
|
|
|
|
22
|
1 |
|
def __repr__(self) -> str: |
23
|
1 |
|
return ' '.join(self.parts) |
24
|
|
|
|
25
|
1 |
|
def generalize(self) -> None: |
26
|
|
|
""" |
27
|
|
|
Remove all parts of the strings that are specific to just this payment, |
28
|
|
|
in order to have a string that is the same across different payments of |
29
|
|
|
the same type (e.g. a monthly payment to Apple for iCloud storage). |
30
|
|
|
|
31
|
|
|
:return: |
32
|
|
|
""" |
33
|
1 |
|
self._clean_leading_card_number() |
34
|
1 |
|
self._clean_leading_date() |
35
|
1 |
|
self._clean_leading_amount_and_currency() |
36
|
1 |
|
self._clean_trailing_exchange_rate() |
37
|
1 |
|
self._clean_trailing_date() |
38
|
1 |
|
self._clean_trailing_amount_and_currency() |
39
|
|
|
|
40
|
1 |
|
def _clean_leading_card_number(self)-> None: |
41
|
1 |
|
pattern = compile_regex(r'\*\d{4}') |
42
|
|
|
|
43
|
1 |
|
try: |
44
|
1 |
|
if pattern.match(self.parts[0]): |
45
|
1 |
|
del self.parts[0] |
46
|
1 |
|
except IndexError: |
47
|
1 |
|
pass |
48
|
|
|
|
49
|
1 |
|
def _clean_leading_date(self)-> None: |
50
|
1 |
|
pattern = compile_regex(r'\d{2}\.\d{2}') |
51
|
|
|
|
52
|
1 |
|
try: |
53
|
1 |
|
if pattern.match(self.parts[0]): |
54
|
1 |
|
del self.parts[0] |
55
|
1 |
|
except IndexError: |
56
|
1 |
|
pass |
57
|
|
|
|
58
|
1 |
|
def _clean_leading_amount_and_currency(self)-> None: |
59
|
1 |
|
try: |
60
|
1 |
|
currency: Any = iso4217parse.by_alpha3(self.parts[0]) |
61
|
1 |
|
if isinstance(currency, iso4217parse.Currency): |
62
|
1 |
|
del self.parts[0] |
63
|
1 |
|
del self.parts[0] |
64
|
1 |
|
except IndexError: |
65
|
1 |
|
pass |
66
|
|
|
|
67
|
1 |
|
def _clean_trailing_exchange_rate(self)-> None: |
68
|
1 |
|
pattern = compile_regex(r'\d{1}\.\d{4}') |
69
|
|
|
|
70
|
1 |
|
try: |
71
|
1 |
|
if pattern.match(self.parts[-1]): |
72
|
1 |
|
del self.parts[-1] |
73
|
1 |
|
del self.parts[-1] |
74
|
1 |
|
except IndexError: |
75
|
1 |
|
pass |
76
|
|
|
|
77
|
1 |
|
def _clean_trailing_date(self)-> None: |
78
|
1 |
|
pattern = compile_regex(r'\d{2}\.\d{2}\.\d{2}') |
79
|
|
|
|
80
|
1 |
|
try: |
81
|
1 |
|
if pattern.match(self.parts[-1]): |
82
|
1 |
|
del self.parts[-1] |
83
|
|
|
|
84
|
1 |
|
pattern = compile_regex(r'.+:') |
85
|
|
|
|
86
|
1 |
|
if pattern.match(self.parts[-1]): |
87
|
1 |
|
del self.parts[-1] |
88
|
1 |
|
except IndexError: |
89
|
1 |
|
pass |
90
|
|
|
|
91
|
1 |
|
def _clean_trailing_amount_and_currency(self)-> None: |
92
|
1 |
|
pattern = compile_regex(r'\d+,\d{2}') |
93
|
|
|
|
94
|
1 |
|
try: |
95
|
1 |
|
if pattern.match(self.parts[-1]): |
96
|
1 |
|
del self.parts[-1] |
97
|
|
|
|
98
|
1 |
|
currency: Any = iso4217parse.by_alpha3(self.parts[-1]) |
99
|
1 |
|
if isinstance(currency, iso4217parse.Currency): |
100
|
1 |
|
del self.parts[-1] |
101
|
1 |
|
except IndexError: |
102
|
|
|
pass |
103
|
|
|
|