-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_data.py
137 lines (99 loc) · 2.92 KB
/
process_data.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import csv
import json
options = {
'Strongly Disagree': 0,
'Disagree': 1,
'Neutral': 2,
'Agree': 3,
'Strongly Agree': 4
}
categories = {}
questions = []
candidates = {}
races = []
with open('data/categories.csv', 'rU') as csvfile:
reader = csv.reader(csvfile)
next(reader)
for i, row in enumerate(reader):
categories[row[1]] = {
'id': i,
'name': row[0],
'slug': row[1],
'type': row[2]
}
with open('data/questions.csv', 'rU') as csvfile:
reader = csv.reader(csvfile)
next(reader)
for i, row in enumerate(reader):
cat = row[1]
questions.append({
'id': i,
'body': row[0],
'category': row[1],
'direction': row[2],
'formId': row[3]
})
class Candidate:
def __init__(self, name, id, profile):
self.name = name
self.id = id
self.profile = profile
self.answers = [0 for q in questions]
self.answers_count = [0 for q in questions]
def add_response(self, score, question):
if question['direction'] == 'down':
score = 4 - score
self.answers[question['id']] += score
self.answers_count[question['id']] += 1
def get_answers(self):
answers = []
for i, answer in enumerate(self.answers):
if self.answers_count[i] > 0:
answers.append(float(answer) / float(self.answers_count[i]))
else:
answers.append(None)
return answers
def to_json(self):
return {
'id': self.id,
'name': self.name,
'profile': self.profile,
'answers': self.get_answers()
}
with open('data/candidates.csv', 'rU') as csvfile:
reader = csv.reader(csvfile)
next(reader)
for row in reader:
candidates[row[0]] = Candidate(row[0], int(row[1]), row[2])
with open('data/races.csv', 'rU') as csvfile:
reader = csv.reader(csvfile)
next(reader)
for row in reader:
races.append({
'title': row[0],
'candidates': [int(i) for i in row[1].split(',')]
})
with open('data/responses.csv', 'rU') as csvfile:
reader = csv.reader(csvfile)
next(reader)
for row in reader:
candidate = candidates[row[1]]
for i, response in enumerate(row[3:]):
if response:
#score = options[response]
score = int(response) - 1
candidate.add_response(score, questions[i])
def get_candidates(candidates):
result = [None for c in candidates]
for slug in candidates:
c = candidates[slug]
result[c.id] = c.to_json()
return result
data = {
'categories': categories,
'candidates': get_candidates(candidates),
'questions': questions,
'races': races
}
with open('data/data.json', 'w') as outfile:
json.dump(data, outfile)