-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcef.py
94 lines (81 loc) · 3 KB
/
cef.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
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
#!/usr/bin/env python3
import os
import sys
import json
import pandas as pd
class CurrencyExchangeFunction:
def __init__(self):
tmpdir = "historicrates"
self.exfile = os.path.join(tmpdir, "exchange_data_auto.txt")
self.df_buy = None
def analyse(self):
snapshotfiles = os.listdir(os.path.dirname(self.exfile))
snapshotfiles = [sf for sf in snapshotfiles if sf.startswith(os.path.basename(self.exfile) + ".")]
snapshotfiles.sort()
curidx = {}
df_buy = pd.DataFrame({"Currency": []})
for snapshotfile in snapshotfiles:
date = snapshotfile.split(".")[-1]
f = open(os.path.join(os.path.dirname(self.exfile), snapshotfile))
c = json.load(f)
for k, v in c.items():
buy = v[1]
rowpos = curidx.setdefault(k, df_buy.shape[0])
df_buy.loc[rowpos, "Currency"] = k
df_buy.loc[rowpos, date] = buy
df_buy = df_buy.set_index(["Currency"])
self.df_buy = df_buy
def daterange(self):
dr = {"start": self.df_buy.columns[0], "end": self.df_buy.columns[-1]}
print(dr)
return json.dumps(dr)
def evolution(self, fromdate=None, todate=None, withtop=True):
evolution = {}
fromidx = 0
toidx = -1
if fromdate:
fromidx = self.df_buy.columns.get_loc(fromdate)
if todate:
toidx = self.df_buy.columns.get_loc(todate)
for row in self.df_buy.iterrows():
evolution[row[0]] = 100 * (row[1][toidx] - row[1][fromidx]) / row[1][fromidx]
if withtop:
self.evolution_top(evolution, fromidx, toidx)
return self.evolution_all(evolution, fromidx, toidx)
def evolution_all(self, evolution, fromidx, toidx):
evolution = {k: round(evolution[k], 2) for k in sorted(evolution)}
print(evolution)
return json.dumps(evolution)
def evolution_top(self, evolution, fromidx, toidx):
evolution = {k: evolution[k] for k in sorted(evolution, key=lambda k: evolution[k], reverse=True)}
for idx, (k, v) in enumerate(evolution.items()):
if idx < 5 or idx >= len(evolution) - 5:
print(f"#{idx:2} {k} {round(v,3):+6.2f}%")
elif idx == 5:
print("...")
def gfunc(request):
#req = request.get_json()
req = request.args
cef = CurrencyExchangeFunction()
cef.analyse()
if "dates" in req:
return cef.daterange()
else:
fromdate = None
todate = None
if "fromdate" in req:
fromdate = req["fromdate"]
if "todate" in req:
todate = req["todate"]
return cef.evolution(fromdate, todate, False)
if __name__ == "__main__":
fromdate = None
todate = None
if len(sys.argv) >= 2:
fromdate = sys.argv[1]
if len(sys.argv) == 3:
todate = sys.argv[2]
cef = CurrencyExchangeFunction()
cef.analyse()
cef.daterange()
cef.evolution(fromdate, todate, True)