This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolitics.py
120 lines (99 loc) · 3.17 KB
/
politics.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
# Hello
import web
import json
import sunlight
import csv
from influenceexplorer import InfluenceExplorer
api = InfluenceExplorer('2d657143a8b64b52b5a8d3a12df38328')
# Load up the api key
try:
with open('sunlight.key') as f: sunlight.API_KEY = f.read().strip()
except IOError:
print 'Loading key from environment'
# A person
class Person:
def __init__(self, row):
self.year = row['year']
self.name = row['name']
self.win = row['win'] == 'W'
self.seat = row['seat']
self.district = row['district']
self.rec_id = row['rec_id']
self.entity_id = row['entity_id']
# get the top contributors for these
def top_contribs(self):
return api.pol.industries(self.entity_id, cycle=self.year)
# Our output format
def nice_data(self):
d = {}
d['top_contribs'] = self.top_contribs()
d['name'] = self.name
d['win'] = self.win
return d
# read in the people
with open('data/nj-data.json') as jsonin:
data = json.load(jsonin)
people = [Person(entry) for entry in data if entry['win']] # skip non-people
# print the number of people
print len(people)
# map up a server
render = web.template.render('templates/', base='layout')
app = web.application((
'/governors/(.+?)', 'Governors',
'/governors', 'GovernorYears',
'/years/(.+?)/(.+?)/(.+?)', 'Filter',
'/years/(.+?)/(.+?)', 'Seats',
'/years/(.+?)', 'Districts',
'/years', 'Years',
'/', 'Index',
'/about', 'About',
), globals())
class GovernorYears:
'Governor year data'
def GET(self):
global people
pep = [x for x in people if x.seat == 'state:governor']
return json.dumps(list(set([p.year for p in pep])))
class Governors:
'Governors for a year'
def GET(self, year):
global people
pep = [x for x in people if x.year == year]
pep = [x for x in pep if x.seat == 'state:governor']
return json.dumps([p.nice_data() for p in pep])
class Districts:
'Races for a year'
def GET(self, year):
global people
pep = [x for x in people if x.year == year]
return json.dumps(list(set([p.district for p in pep])))
class Seats:
'Races for a year'
def GET(self, year, district):
global people
pep = [x for x in people if x.year == year]
pep = [x for x in pep if x.district == district]
return json.dumps(list(set([p.seat for p in pep])))
class Years:
'Filter the data per year, ar, and seat'
def GET(self):
global people
return json.dumps(list(set([p.year for p in people])))
class Filter:
'Filter the data per year, ar, and seat'
def GET(self, year, district, seat):
global people
pep = [x for x in people if x.year == year]
pep = [x for x in pep if x.seat == seat]
pep = [x for x in pep if x.district == district]
return json.dumps([p.nice_data() for p in pep])
class Index:
'Render the base index file'
def GET(self):
return render.index()
class About:
'Render the about page'
def GET(self):
return render.about()
if __name__ == '__main__':
app.run()