-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.py
192 lines (156 loc) · 6.9 KB
/
datasource.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import MySQLdb
import getpass
import math
class DataSource:
"""
DataSource executes all of the queries on the database.
It also formats the data to send back to the frontend, typically in a list
or some other collection or object.
"""
def __init__(self):
self.connection = MySQLdb.connect(host="edwardlee.mysql.pythonanywhere-services.com",
user="edwardlee",
passwd="mahjames",
db="edwardlee$electiondata")
self.cur = self.connection.cursor()
def getListOfStates(self):
"""
Returns a list of al the states in the database
PARAMETERS:
none
RETURN:
a list of all states
"""
self.cur.execute("SELECT DISTINCT state from electiondata ORDER BY state;")
states_response = [i[0] for i in self.cur.fetchall()]
return states_response
def getTotalVotesInCounty(self, county, state):
"""
Returns the number of total votes casted in specified county.
PARAMETERS:
county
state
RETURN:
an int of the number of total votes casted in this county
"""
self.cur.execute(
"SELECT SUM(totalvotes) FROM electiondata WHERE area = '" + county + "' AND state = '" + state + "'")
return self.cur.fetchone()[0]
def getTotalVotesInState(self, state):
"""
Returns the number of total votes casted in specified state.
PARAMETERS:
state
RETURN:
an int of the number of total votes casted in this state
"""
self.cur.execute("SELECT SUM(totalvotes) FROM electiondata WHERE state = '" + state + "'")
return self.cur.fetchone()[0]
def getTotalVotesUSA(self):
"""
Returns the number of total votes casted nationwide.
PARAMETERS:
none
RETURN:
an int of the number of total votes casted nationwide
"""
self.cur.execute("SELECT SUM(totalvotes) FROM electiondata")
return self.cur.fetchone()[0]
def getCountiesInState(self, state):
"""
Returns a list of the counties that make up the specified state.
PARAMETERS:
state
RETURN:
a list of all of the counties that make up this state
"""
self.cur.execute("SELECT area from electiondata WHERE state = '" + state + "'")
counties_response = [i[0] for i in self.cur.fetchall()]
return counties_response
def getPartyPercentageC(self, party, county, state):
"""
Returns the percentage of total votes garnered by specified party within specified county.
PARAMETERS:
party
in the form of 'rep' 'dem' 'third' or 'other' for compatibility with our SQL database
county
state
RETURN:
a decimal or percentage of total votes garnered by this party within this county.
"""
self.cur.execute(
"SELECT " + party + "votespercent FROM electiondata WHERE area = '" + county + "' AND state = '" + state + "'")
return self.cur.fetchone()[0]
def getPartyPercentageS(self, party, state):
"""
Returns the percentage of total votes garnered by specified party within specified state.
PARAMETERS:
party
state
RETURN:
a decimal or percentage of total votes garnered by this party within this state.
"""
self.cur.execute("SELECT SUM(" + party + "votes) FROM electiondata WHERE state = '" + state + "'")
sum = self.cur.fetchone()[0]
self.cur.execute("SELECT SUM(totalvotes) FROM electiondata WHERE state = '" + state + "'")
partyPercent = sum / self.cur.fetchone()[0]
return partyPercent
def getPartyPercentageUSA(self, party):
"""
Returns the percentage of total votes garnered by specified party, nationwide.
PARAMETERS:
party
RETURN:
a decimal or percentage of total votes garnered by this party, nationwide.
"""
self.cur.execute("SELECT SUM(" + party + "votes) FROM electiondata;")
sum = self.cur.fetchone()[0]
self.cur.execute("SELECT SUM(totalvotes) FROM electiondata;")
partyPercent = sum / self.cur.fetchone()[0]
return partyPercent
def getCountiesInShareRange(self, party, start, end, state=None):
"""
Returns a list of all of the counties in the specified state in which the specified party percentage resides within the specified starting
and ending percentages (inclusive). If state is not entered, the function searches from all the counties in the country.
PARAMETERS:
start - the low bound of the party percentage range
end - the high bound of the party percentage range
party
state
RETURN:
a list of all of the counties in the state with the specified party percentage in the specified range.
"""
if state and state != "":
self.cur.execute("SELECT area, state FROM electiondata WHERE state = '" + state + "' AND " + party + "votespercent BETWEEN " + start + " and " + end)
else:
self.cur.execute("SELECT area, state FROM electiondata WHERE " + party + "votespercent BETWEEN " + start + " and " + end)
output = self.cur.fetchall()
if len(output) > 75:
return ["bad request", "Found " + str(len(output)) + " results. Please narrow your range."]
elif len(output) == 0:
return ["bad request", "No counties found in specified range."]
else:
return output
def getStatesInShareRange(self, party, start, end):
"""
Returns a list of all of the states in which the largest party percentage resides within the specified starting
and ending percentages (inclusive).
PARAMETERS:
start - the low bound of the party percentage range
end - the high bound of the party percentage range
RETURN:
a list of all of the states with the largest party percentage in the specified range.
"""
output = []
the_states = self.getListOfStates()
for state in the_states:
if float(start)/100 <= self.getPartyPercentageS(party, state) <= float(end)/100:
output.append(state)
if len(output) == 0:
return ["bad request", "No states found in specified range."]
else:
return output
def __del__(self):
if hasattr(self, 'cur'):
self.cur.close()
self.connection.close()