forked from Parliament-in-Data/Federal-Parliament-Scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.py
309 lines (251 loc) · 11.5 KB
/
vote.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
from util import clean_string
from bs4 import NavigableString
from typing import List
import activity
from common import Choice
class Vote:
def __init__(self, meeting_topic, vote_number: int, yes: int):
"""A Vote represents a single vote in a meeting.
Args:
meeting_topic (MeetingTopic): The meeting topic
vote_number (int): Number of the vote in this meeting (e.g. 1)
yes (int): Number of yes votes
"""
self.meeting = meeting_topic.meeting
self.meeting_topic = meeting_topic
self.vote_number = vote_number
self.yes = yes
self.unsure = False
class GenericVote(Vote):
"""A Vote represents a single vote in a meeting.
"""
def __init__(self, meeting_topic, vote_number: int, yes: int, no: int, abstention: int):
"""A Vote represents a single vote in a meeting.
Args:
meeting_topic (MeetingTopic): The meeting topic
vote_number (int): Number of the vote in this meeting (e.g. 1)
yes (int): Number of yes votes
no (int): Number of no votes
abstention (int): Number of abstentions
"""
Vote.__init__(self, meeting_topic, vote_number, yes)
self.yes_voters = []
self.no = no
self.no_voters = []
self.abstention = abstention
self.abstention_voters = []
def __repr__(self):
return f"Vote({self.vote_number}, {self.yes}, {self.no}, {self.abstention})"
def to_dict(self, session_base_URI: str):
return {
'id': self.vote_number,
'type': 'general',
'yes': self.yes,
'no': self.no,
'abstention': self.abstention,
'passed': self.has_passed(),
'voters': {
"yes": [f'{session_base_URI}members/{member.uuid}.json' for member in self.yes_voters],
"no": [f'{session_base_URI}members/{member.uuid}.json' for member in self.no_voters],
"abstention": [f'{session_base_URI}members/{member.uuid}.json' for member in self.abstention_voters]
}
}
def has_passed(self):
"""Does this motion have the majority of votes
Returns:
bool: Does this motion have the majority of votes
"""
# FIXME: No Quorum Check (rule 42.5 of parliament)
return self.yes > self.no + self.abstention
@staticmethod
def from_table(meeting_topic, vote_number: int, vote_rows: NavigableString):
"""Generate a new Vote from a parsed table.
Args:
vote_number (int): Number of the vote in this meeting (e.g. 1)
vote_rows (NavigableString): Vote rows as obtained by BeautifulSoup
Returns:
Vote:
"""
yes = int(clean_string(vote_rows[1].find_all(
'td')[1].find('p').get_text()))
no = int(clean_string(vote_rows[2].find_all(
'td')[1].find('p').get_text()))
abstention = int(clean_string(
vote_rows[3].find_all('td')[1].find('p').get_text()))
return GenericVote(meeting_topic, vote_number, yes, no, abstention)
def set_yes_voters(self, l):
"""Set the members who voted for
Args:
l (List[Member]): A list of Members who voted for
"""
if abs(len(l) - self.yes) > 2:
# Sometimes there are some inconsistencies in the counts and the reported names
# We allow some tolerance for this
print(
f'NOTE: The number of yes voters did not match the provided list: {len(l)} instead of {self.yes}')
self.unsure = True
self.yes = len(l)
self.yes_voters = l
post_vote_activity(self, Choice.YES, l)
def set_no_voters(self, l):
"""Set the members who voted against
Args:
l (List[Member]): A list of Members who voted against
"""
if abs(len(l) - self.no) > 2:
# Sometimes there are some inconsistencies in the counts and the reported names
# We allow some tolerance for this
print(
f'NOTE: The number of no voters did not match the provided list: {len(l)} instead of {self.no}')
self.unsure = True
self.no = len(l)
self.no_voters = l
post_vote_activity(self, Choice.NO, l)
def set_abstention_voters(self, l):
"""Set the members who abstained from voting for this motion
Args:
l (List[Member]): A list of Members who abstained from the vote
"""
if abs(len(l) - self.abstention) > 2:
# Sometimes there are some inconsistencies in the counts and the reported names
# We allow some tolerance for this
print(
f'NOTE: The number of abstention voters did not match the provided list: {len(l)} instead of {self.abstention}')
self.unsure = True
self.abstention = len(l)
self.abstention_voters = l
post_vote_activity(self, Choice.ABSTENTION, l)
class LanguageGroupVote(GenericVote):
"""For some voting matters a majority in both Language Groups is needed"""
def __init__(self, meeting_topic, vote_number: int, vote_NL: Vote, vote_FR: Vote):
"""For some voting matters a majority in both Language Groups is needed
Args:
meeting_topic (MeetingTopic): The meeting topic
vote_number (int): Number of the vote in this meeting (e.g. 1)
vote_NL (Vote): The Vote in the Dutch-speaking part of the Parliament
vote_FR (Vote): The Vote in the French-speaking part of the Parliament
"""
GenericVote.__init__(self, meeting_topic, vote_number, vote_NL.yes + vote_FR.yes,
vote_NL.no + vote_FR.no, vote_NL.abstention + vote_FR.abstention)
self.vote_NL = vote_NL
self.vote_FR = vote_FR
def __repr__(self):
return "LanguageGroupVote(%d, %d, %d)" % (self.vote_number, self.vote_NL, self.vote_FR)
def to_dict(self, session_base_URI: str):
return {
'id': self.vote_number,
'type': 'language_group',
'yes': self.vote_NL.yes + self.vote_FR.yes,
'no': self.vote_NL.no + self.vote_FR.no,
'abstention': self.vote_NL.abstention + self.vote_FR.abstention,
'passed': self.has_passed(),
'voters': {
"yes": [f'{session_base_URI}members/{member.uuid}.json' for member in self.yes_voters],
"no": [f'{session_base_URI}members/{member.uuid}.json' for member in self.no_voters],
"abstention": [f'{session_base_URI}members/{member.uuid}.json' for member in self.abstention_voters]
},
'detail': {
"NL": self.vote_NL.to_dict(session_base_URI),
"FR": self.vote_FR.to_dict(session_base_URI)
}
}
def has_passed(self):
"""The vote has to pass in both halves of the parliament.
Returns:
bool: Has the vote obtained the necessary majority?
"""
return self.vote_NL.has_passed() and self.vote_FR.has_passed()
@staticmethod
def from_table(meeting_topic, vote_number: int, vote_rows: NavigableString):
"""Generate a new Vote from a parsed table.
Args:
meeting_topic (MeetingTopic): The meeting topic
vote_number (int): Number of the vote in this meeting (e.g. 1)
vote_rows (NavigableString): Vote rows as obtained by BeautifulSoup
Returns:
Vote:
"""
yes_fr = int(clean_string(
vote_rows[2].find_all('td')[1].find('p').get_text()))
no_fr = int(clean_string(
vote_rows[3].find_all('td')[1].find('p').get_text()))
abstention_fr = int(clean_string(
vote_rows[4].find_all('td')[1].find('p').get_text()))
yes_nl = int(clean_string(
vote_rows[2].find_all('td')[3].find('p').get_text()))
no_nl = int(clean_string(
vote_rows[3].find_all('td')[3].find('p').get_text()))
abstention_nl = int(clean_string(
vote_rows[4].find_all('td')[3].find('p').get_text()))
return LanguageGroupVote(meeting_topic, vote_number, GenericVote(meeting_topic, vote_number, yes_nl, no_nl, abstention_nl), GenericVote(meeting_topic, vote_number, yes_fr, no_fr, abstention_fr))
class ElectronicGenericVote(Vote):
"""Some voting are anonymously organised electronically. We don't have the names in this case"""
def __init__(self, meeting_topic, vote_number: int, yes: int, no: int):
"""A Vote represents a single vote in a meeting.
Args:
meeting_topic (MeetingTopic): The meeting topic
vote_number (int): Number of the vote in this meeting (e.g. 1)
yes (int): Number of yes votes
no (int): Number of no votes
"""
Vote.__init__(self, meeting_topic, vote_number, yes)
self.no = no
def __repr__(self):
return f"ElectronicGenericVote({self.vote_number}, {self.yes}, {self.no})"
def has_passed(self):
return self.yes > self.no and self.yes + self.no > 75
def to_dict(self, session_base_URI: str):
return {
'id': self.vote_number,
'type': 'electronic_generic',
'yes': self.yes,
'no': self.no,
'passed': self.has_passed()
}
class ElectronicAdvisoryVote(Vote):
"""Some voting are anonymously organised electronically to inquire whether more opinions are required.
We don't have the names in this case
"""
def __init__(self, meeting_topic, vote_number: int, yes: int):
"""A Vote represents a single vote in a meeting.
Args:
meeting_topic (MeetingTopic): The meeting topic
vote_number (int): Number of the vote in this meeting (e.g. 1)
yes (int): Number of yes votes
"""
Vote.__init__(self, meeting_topic, vote_number, yes)
def __repr__(self):
return f"ElectronicAdvisoryVote({self.vote_number}, {self.yes})"
def has_passed(self):
"""Does this advisory request reach more than the threshold of 1/3 of the yes votes to pass
Returns:
bool: Does this motion have the majority of votes
"""
return self.yes > 50
def to_dict(self, session_base_URI: str):
return {
'id': self.vote_number,
'type': 'electronic_advisory',
'yes': self.yes,
'passed': self.has_passed()
}
def electronic_vote_from_table(meeting_topic, vote_number: int, vote_start_node: NavigableString):
"""Generate a new electronic (advisory or generic) vote from a parsed table.
Args:
meeting_topic (MeetingTopic): The meeting topic
vote_number (int): Number of the vote in this meeting (e.g. 1)
vote_start_node (NavigableString): Vote start node as obtained by BeautifulSoup
Returns:
Vote:
"""
yes = int(clean_string(vote_start_node.find_all(
'td')[1].find('p').get_text()))
vote_end_node = vote_start_node.find_next_sibling().find_next_sibling()
if not vote_end_node or vote_end_node.name != 'table':
return ElectronicAdvisoryVote(meeting_topic, vote_number, yes)
no = int(clean_string(vote_end_node.find_all(
'td')[1].find('p').get_text()))
return ElectronicGenericVote(meeting_topic, vote_number, yes, no)
def post_vote_activity(vote: Vote, choice: Choice, members: List):
for member in members:
member.post_activity(activity.VoteActivity(member, vote, choice))