-
Notifications
You must be signed in to change notification settings - Fork 10
/
mapper.py
executable file
·77 lines (58 loc) · 1.9 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
69
70
71
72
73
74
75
76
77
#!/usr/bin/python
# The Mapper
import sys
import csv
# Set local variables
iteration = 0
currentCountry = None
previousCountry = None
currentFx = None
previousFx = None
percentChange = None
currentKey = None
fxMap = []
# print "Starting mapper.py"
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
# Uncomment if you want to see the output
# if iteration % 50000 == 0:
# print "Current iteration is %d" % iteration
# iteration += 1
# 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)
#
# print "mapper.py has completed with %d iterations" % (iteration - 1)
# Show the returned values
for i in sorted(fxMap):
print "%-20s - %d" % (i[0], i[1])