-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfirst.py
More file actions
75 lines (59 loc) · 2.77 KB
/
first.py
File metadata and controls
75 lines (59 loc) · 2.77 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
import csv
from pymongo import MongoClient
from decimal import Decimal, ROUND_HALF_UP, getcontext
from bson.decimal128 import Decimal128
client = MongoClient('mongodb://localhost:27017/')
db = client['theratesapi']
collection = db['currency']
collection.drop() # Comment out this line to update missing data because of any reason.
currencies = ['USD', 'JPY', 'BGN', 'CYP', 'CZK', 'DKK', 'EEK', 'GBP', 'HUF', 'LTL', 'LVL', 'MTL', 'PLN', 'ROL', 'RON', 'SEK', 'SIT', 'SKK', 'CHF', 'ISK', 'NOK', 'HRK', 'RUB', 'TRL', 'TRY', 'AUD', 'BRL', 'CAD', 'CNY', 'HKD', 'IDR', 'ILS', 'INR', 'KRW', 'MXN', 'MYR', 'NZD', 'PHP', 'SGD', 'THB', 'ZAR']
getcontext().prec = 28 # set high precision for intermediate calculations
DECIMAL_PLACES = Decimal('0.000001') # 6 digits after decimal point
def calculate_new_base(new_base, new_row):
new_curr = {
'date': new_row['date'],
'base': new_base,
}
new_curr['rates'] = {}
# Use Decimal for calculations and quantize for 6 digits after decimal
new_curr['rates']['EUR'] = float((Decimal('1') / Decimal(new_row['rates'][new_base])).quantize(DECIMAL_PLACES, rounding=ROUND_HALF_UP))
for x in currencies:
if x in new_row['rates'].keys() and x != new_base:
value = (Decimal(new_row['rates'][x]) / Decimal(new_row['rates'][new_base])).quantize(DECIMAL_PLACES, rounding=ROUND_HALF_UP)
# new_curr['rates'][x] = float(value)
new_curr['rates'][x] = Decimal128(value)
# Check for duplicate before insert
if collection.find_one({'date': new_curr['date'], 'base': new_base}):
print(f"Skipped duplicate for {new_curr['date']} base {new_base}")
return
collection.insert_one(new_curr)
# print(new_curr)
print('base', new_base)
with open('../eurofxref-hist.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# print(row)
# break
orginal_row = row
# print(orginal_row)
orginal_row.pop("")
# print(orginal_row)
new_row = {}
new_row['rates'] = {}
for key in orginal_row.keys():
if orginal_row[key] != 'N/A':
if key == 'Date':
new_row['date'] = orginal_row[key]
else:
new_row['rates'][key] = orginal_row[key]
new_row['base'] = 'EUR'
# print(new_row)
for k in new_row['rates'].keys():
if k not in ('date', 'base'):
calculate_new_base(k, new_row)
# Check for duplicate before insert
if collection.find_one({'date': new_row['date'], 'base': 'EUR'}):
print(f"Skipped duplicate for {new_row['date']} base EUR")
continue
collection.insert_one(new_row)
# print(new_row)