-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmintObjects.py
More file actions
162 lines (139 loc) · 4.85 KB
/
mintObjects.py
File metadata and controls
162 lines (139 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from thompcoutils.log_utils import get_logger
from datetime import datetime
import inspect
import logging
import re
def clean_dictionary(name, transactions):
logger = get_logger()
logger.debug(name + " before:")
logger.debug(str(transactions))
for transaction in transactions:
for key in transaction:
clean_key = to_string(key)
transaction[clean_key] = to_string(transaction[clean_key])
transaction[clean_key] = parse_all(clean_key, transaction[clean_key])
return transactions
DATE_STRING_FIELDS = [
'dueDate'
]
LONG_DATE_FIELDS = [
'addAccountDate',
'lastUpdated',
'closeDate',
'fiLastUpdated'
]
MONTH_ONLY_DATE_FIELDS = [
"date",
"odate"
]
DOLLAR_FIELDS = [
"amount",
"value",
"dueAmt",
"currentBalance"
]
def date_convert(dateraw):
# Converts dates from json data
cy = datetime.isocalendar(datetime.today())[0]
# noinspection PyBroadException
try:
newdate = datetime.strptime(dateraw + str(cy), '%b %d%Y')
except Exception:
newdate = datetime.strptime(dateraw, '%m/%d/%y')
return newdate
def parse_all(key, val):
this_type = to_string(type(val))
if key in DATE_STRING_FIELDS:
# noinspection PyBroadException
try:
val = datetime.strptime(val, "%m/%d/%Y")
except Exception:
val = None
elif key in LONG_DATE_FIELDS:
# noinspection PyBroadException
try:
val = datetime.fromtimestamp(val / 1e3)
except Exception:
val = None
elif to_string(key) in MONTH_ONLY_DATE_FIELDS:
# noinspection PyBroadException
try:
val = date_convert(val)
except Exception:
val = None
elif to_string(key) in DOLLAR_FIELDS:
# noinspection PyBroadException
try:
val = float(val.replace("$", "").replace(",", ""))
except Exception:
val = None
elif val == "False":
val = False
elif val == "True":
val = True
elif "'unicode'" in this_type:
val = to_string(val)
return val
class MintTransactions:
##############################################
# __init__: constructor for a MintTransactions
# obj: string that describes a group of transactions
##############################################
def __init__(self, obj):
self.transactions = clean_dictionary("MintTransactions", obj)
def dump(self):
logger = get_logger()
for transaction in self.transactions:
logger.debug("Dumping Transaction")
for key in transaction:
logger.debug("\t\t" + key + ":" + to_string(transaction[key]))
def get_financial_institutions(self, start_date):
fis = []
for transaction in self.transactions:
if transaction["fi"] not in fis and transaction['date'] is not None and transaction["date"] >= start_date:
fis.append(transaction["fi"])
return fis
def get_accounts(self, fi, start_date):
accounts = []
for transaction in self.transactions:
if transaction["fi"] == fi and transaction["account"] not in accounts and transaction["date"] >= start_date:
accounts.append(transaction["account"])
return accounts
def get_transactions(self, fi, account, start_date):
transactions = []
total = 0.0
for transaction in self.transactions:
if transaction["fi"] == fi and transaction["account"] == account \
and transaction["account"] not in transactions and transaction["date"] >= start_date \
and transaction["amount"] > 0:
transactions.append(transaction)
amount = transaction["amount"]
if transaction["isDebit"]:
total -= amount
else:
total += amount
transactions.sort(key=lambda item: item['date'], reverse=True)
return transactions, total
class MintAccounts:
##############################################
# __init__: constructor for a MintAccounts
# obj: string that describes a group of accounts
##############################################
def __init__(self, obj):
self.accounts = clean_dictionary("MintAccounts", obj)
def get_account(self, name):
for account in self.accounts:
if account["accountName"] == name:
return account
return None
def dump(self):
logger = get_logger()
for account in self.accounts:
logger.debug("Dumping " + account["name"])
for key in account:
logger.debug("\t\t" + key + ":" + to_string(account[key]))
def to_string(obj):
if isinstance(obj, str):
return re.sub(r'[^\x00-\x7f]', r'', obj)
else:
return str(obj)