-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.py
executable file
·71 lines (55 loc) · 1.72 KB
/
combine.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
#!/usr/bin/python3
#
# Combine two 'conversion' files CUR1->CUR2 and CUR2->CUR3
# and generate a new 'conversion file CUR1->CUR3
#
# File format, one entry per line:
# from_currency, to_currency
# date, rate
# date, rate
# ...
import sys
import argparse
import re
def extractCSVs(s):
return re.sub('\s*,\s*', ',', line.rstrip()).split(',')
parser = argparse.ArgumentParser()
ordinals = [(1, 'first'), (2, 'second'), (3, 'third'), (4, 'fourth'), (5, 'fifth')]
for (n, o) in ordinals:
parser.add_argument('-' + str(n), '--' + o, help=(o + ' file in currency chain'), default='')
parser.add_argument('-o', '--output', help='output filename', default='out.csv')
args = parser.parse_args()
data = {}
dates = []
currencies = []
fileCount = 0
for (n, o) in ordinals:
filename = args.__dict__[o]
if filename != '':
print('Reading %s ...' % (filename))
fileCount += 1
f = open(filename)
line = f.readline()
(fcurr, tcurr) = extractCSVs(line)
if n == 1:
currencies.append(fcurr)
elif fcurr != currencies[n - 1]:
sys.exit('Currency mismatch in ' + o + ' file! ' + currencies[n - 1] + ' <> ' + fcurr)
currencies.append(tcurr)
for line in f:
(date, rate) = extractCSVs(line)
if date not in data.keys():
data[date] = [1, 0]
dates.append(date)
data[date][0] *= float(rate)
data[date][1] += 1
print('Read %d files' % (fileCount))
dates.sort()
f = open(args.output, mode='w')
print('Writing to %s ...' % (args.output))
print('%s, %s' % (currencies[0], currencies[len(currencies)-1]), file=f)
for d in dates:
if data[d][1] == fileCount:
print('%s, %f' % (d, data[d][0]), file=f)
#else:
# print('INFO: skipping %s - insufficient chain :: %f' % (d, data[d][0]))