-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmapper.py
68 lines (51 loc) · 1.64 KB
/
mapper.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
#!/usr/bin/python3
# The Mapper
import sys
import csv
# Set local variables
iteration = 0
currentCountry = None
previousCountry = None
currentFx = None
previousFx = None
percentChange = None
currentKey = None
fxMap = []
infile = sys.stdin
next(infile) # skip first line of input file
for line in infile:
line = line.strip()
line = line.split(',', 2)
try:
# Get data from line
currentCountry = line[1].rstrip()
if len(line[2]) == 0:
continue
currentFx = float(line[2])
if currentCountry != previousCountry:
previousCountry = currentCountry
previousFx = currentFx
previousLine = line
continue
# If country same as previous, add to map
elif currentCountry == previousCountry:
percentChange = ((currentFx - previousFx) / previousFx) * 100.00
percentChange = round(percentChange, 2)
percentChange = percentChange
currentKey = "%s: %6.2f%%" % (currentCountry, percentChange)
# Set the array with tuple keys
fxMap.append(tuple([currentKey, 1]))
# Update Values
previousCountry = currentCountry
previousFx = currentFx
previousLine = line
# Handle unexpected errors
except Exception as e:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
print("currentFx: %.2f previousFx: %.2f" % (currentFx, previousFx))
print(message)
sys.exit(0)
# Show the returned values
for i in sorted(fxMap):
print("%-20s - %d" % (i[0], i[1]))