-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions.py
51 lines (38 loc) · 1.34 KB
/
transactions.py
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
from threading import Lock
import pandas as pd
import json
lock = Lock()
file = 'transactions.json'
def get_transactions():
try:
with lock:
with open(file, 'r') as f:
return json.load(f)
except FileNotFoundError:
save_transactions([])
return get_transactions()
def save_transactions(transactions):
with lock:
with open(file, 'w') as f:
json.dump(transactions, f)
def add_transaction(t):
transactions = get_transactions()
transactions.append(t)
save_transactions(transactions)
def get_html(classes='table'):
transactions = get_transactions()
if not transactions:
return f'<table class="{classes}"></table>'
return pd.concat([pd.DataFrame.from_dict({key: [value] for (key, value) in t.items()})
for t in transactions], ignore_index=True).to_html(classes=classes)
def make_transaction(actor, amount, complete=False):
return {'Actor': actor, 'Amount': amount, 'Complete': complete}
if __name__ == '__main__':
test_transactions = [
{'Actor': 'Grant Duffy', 'Amount': 1.0, 'Complete': False},
{'Actor': 'Tom Duffy', 'Amount': 1.0, 'Complete': False},
{'Actor': 'Grant Peltier', 'Amount': 1.0, 'Complete': False},
]
for t in test_transactions:
add_transaction(t)
print(get_html())