-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
85 lines (78 loc) · 3.04 KB
/
utils.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
import simplejson
class Jsonify():
def __init__(self):
self.q1 = self.jsonify_q1
self.q2_dem = self.jsonify_q2_dem
self.q2_conf = self.jsonify_q2_conf
self.q3 = self.jsonify_q3
def jsonify_q1(self, data):
arr = []
for row in data:
r = {"country": self.rename(row[0]),
"quantity": row[1],
"trade_usd": row[2],
"date": row[3]}
arr.append(r)
return simplejson.dumps(arr, encoding="utf8")
def jsonify_q2_conf(self, data):
arr = []
for row in data:
r = {"conflict": row[0],
"income": row[1],
"country": row[2].decode("ISO-8859-1")}
arr.append(r)
return simplejson.dumps(arr, encoding="utf8")
def jsonify_q2_dem(self, data):
arr = []
for row in data:
r = {"dem_index": row[0],
"income": row[1],
"country": row[2].decode("ISO-8859-1")}
arr.append(r)
return simplejson.dumps(arr, encoding="utf8")
def jsonify_q3(self, data):
arr = []
country = ''
for row in data:
r = { "country": row[1].title(),
"total_workers": row[2] if (self.isN(row[2])) else 0,
"female_workers": row[3]*(row[2]/100) if (self.isN(row[2]) and self.isN(row[3])) else 0,
"pop_2013": row[4] if(country != row[1]) else 0,
"pop_2014": row[5] if(country != row[1]) else 0,
"pop_2015": row[6] if(country != row[1]) else 0,
"pop_2016": row[7] if(country != row[1]) else 0,
"pop_2017": row[8] if(country != row[1]) else 0,
"ue_2013": row[9] if(country != row[1]) else 0,
"ue_2014": row[10] if(country != row[1]) else 0,
"ue_2015": row[11] if(country != row[1]) else 0,
"ue_2016": row[12] if(country != row[1]) else 0,
"ue_2017": row[13] if(country != row[1]) else 0}
arr.append(r)
country = row[1]
return simplejson.dumps(arr, encoding="utf8")
def isN(self, n):
if (isinstance(n, (int, long, float, complex))):
return True
else:
return False
def rename(self, country):
if (country == "USA"):
return "United States"
elif (country == "China, Hong Kong SAR"):
return "China"
elif (country == "China, Macao SAR"):
return "China"
elif (country == "Czech Rep."):
return "Czech Republic"
elif (country == "Dominican Rep."):
return "Dominican Republic"
elif (country == "Rep. of Korea"):
return "South Korea"
elif (country == "Russian Federation"):
return "Russia"
elif (country == "Serbia"):
return "Republic of Serbia"
elif (country == "Viet Nam"):
return "Vietnam"
else:
return country.decode("ISO-8859-1")