1
|
|
|
# pyre-strict |
2
|
|
|
|
3
|
|
|
""" |
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
|
|
|
from re import compile as compile_regex |
8
|
|
|
from typing import List, Any |
9
|
|
|
|
10
|
|
|
import iso4217parse |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class PaymentText: |
14
|
|
|
""" |
15
|
|
|
Use this class to represent your payment text. |
16
|
|
|
""" |
17
|
|
|
text: str = '' |
18
|
|
|
|
19
|
|
|
def __init__(self, text: str) -> None: |
20
|
|
|
self.text = text |
21
|
|
|
|
22
|
|
|
def __repr__(self) -> str: |
23
|
|
|
return self.text |
24
|
|
|
|
25
|
|
|
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
|
|
|
parts: List[str] = self.text.split() |
34
|
|
|
|
35
|
|
|
pattern = compile_regex(r'\*\d{4}') |
36
|
|
|
|
37
|
|
|
try: |
38
|
|
|
if pattern.match(parts[0]): |
39
|
|
|
del parts[0] |
40
|
|
|
except IndexError: |
41
|
|
|
return |
42
|
|
|
|
43
|
|
|
pattern = compile_regex(r'\d{2}\.\d{2}') |
44
|
|
|
|
45
|
|
|
if pattern.match(parts[0]): |
46
|
|
|
del parts[0] |
47
|
|
|
|
48
|
|
|
currency: Any = iso4217parse.by_alpha3(parts[0]) |
49
|
|
|
|
50
|
|
|
if isinstance(currency, iso4217parse.Currency): |
51
|
|
|
del parts[0] |
52
|
|
|
del parts[0] |
53
|
|
|
|
54
|
|
|
pattern = compile_regex(r'\d{1}\.\d{4}') |
55
|
|
|
|
56
|
|
|
if pattern.match(parts[-1]): |
57
|
|
|
del parts[-1] |
58
|
|
|
del parts[-1] |
59
|
|
|
|
60
|
|
|
pattern = compile_regex(r'\d{2}\.\d{2}\.\d{2}') |
61
|
|
|
|
62
|
|
|
if pattern.match(parts[-1]): |
63
|
|
|
del parts[-1] |
64
|
|
|
|
65
|
|
|
pattern = compile_regex(r'.+:') |
66
|
|
|
|
67
|
|
|
if pattern.match(parts[-1]): |
68
|
|
|
del parts[-1] |
69
|
|
|
|
70
|
|
|
pattern = compile_regex(r'\d+,\d{2}') |
71
|
|
|
|
72
|
|
|
if pattern.match(parts[-1]): |
73
|
|
|
del parts[-1] |
74
|
|
|
|
75
|
|
|
currency: Any = iso4217parse.by_alpha3(parts[-1]) |
76
|
|
|
if isinstance(currency, iso4217parse.Currency): |
77
|
|
|
del parts[-1] |
78
|
|
|
|
79
|
|
|
text = ' '.join(parts) |
80
|
|
|
|
81
|
|
|
self.text = text |
82
|
|
|
|