-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data.py
71 lines (62 loc) · 1.88 KB
/
get_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
from pysqlc import DB
class DataGetter:
"""
Simple class to get data for ridings and candiates stored in
the `canfedelxn19` database
"""
def __init__(self):
self.db = DB(db_env='prod', database="canfedelxn19")
def get_ridings(self):
"""
simple functino to get a list of all ridings and their names
"""
q = "SELECT riding_id, riding_name_en FROM elxn_ridings;"
data = self.db.query(q)
ridings = {}
for each in data:
ridings[each[0]] = each[1]
return ridings
def get_riding_info(self, riding_id):
"""
function to get a bunch of info about a riding
"""
q = """
SELECT
riding_id,
riding_name_en,
riding_pop,
riding_median_house_income,
riding_encumbent,
riding_prov
FROM elxn_ridings
WHERE riding_id={};
""".format(riding_id)
try:
data = self.db.query(q, q_type="SELECT")
print(data)
return {
"riding_id": data[0][0],
"riding_name_en": data[0][1],
"riding_population": data[0][2]
}
except Exception as e:
return {"error":"Error in the query: {}".format(e)}
def get_riding_candidates(self, riding_id):
"""
simple API to get the candiates of a specific riding...
"""
q = """
SELECT
candidate_name,
party_name
FROM elxn_candidates
WHERE riding_id = {}
""".format(riding_id)
try:
return {each[1]:each[0] for each in self.db.query(q)}
except Exception as e:
return {"error":"Error in the query: {}".format(e)}
if __name__ == "__main__":
print("starting...")
DG = DataGetter()
print(DG.get_ridings())