This repository has been archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathentities-without-website
executable file
·230 lines (169 loc) · 6.42 KB
/
entities-without-website
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
#! /usr/bin/python3
# -*- coding: utf-8; -*-
import sys
import os
import os.path
import glob
import json
import pprint
program_dir = os.path.dirname(os.path.abspath(__file__))
parties_data_dir = os.path.join(program_dir, "data", "parties")
candidates_data_dir = os.path.join(program_dir, "data", "people")
def get_data_file_paths(data_dir, suffix=".json"):
""" Get a collection of data file paths from the specified directory.
:param data_dir: The directory path to inspect for data files.
:param suffix: The suffix to match on data files.
:return: A collection of filename paths.
"""
data_file_glob = os.path.join(data_dir, "*" + suffix)
paths = glob.glob(data_file_glob)
return paths
class Party:
""" A political party. """
def __init__(self, name, code, website=None):
self.name = name
self.code = code
self.website = website
def __repr__(self):
text = (
"<Party {code}: {name!r} (website {website})>".format(
**vars(self)))
return text
def read_party_from_json(infile):
""" Read the party data from the specified input file.
:param infile: An open input file from which to read the JSON data.
:return: A `Party` instance.
"""
attrs = json.loads(infile.read())
party = Party(**attrs)
return party
def get_parties_from_files(file_paths):
""" Get a collection of parties from the specified data file paths.
:param file_paths: An iterable of file paths to JSON data files.
:return: A collection of `Party` instances.
"""
parties = set()
errors = set()
for path in file_paths:
try:
infile = open(path, 'r')
except (IOError, OSError) as exc:
errors.add((path, exc))
continue
try:
party = read_party_from_json(infile)
except (TypeError, ValueError) as exc:
errors.add((path, exc))
continue
parties.add(party)
if errors:
raise RuntimeError(
"The following errors occurred:\n"
"{errors}\n".format(errors=pprint.pformat(errors)))
return parties
def get_parties_from_data_dir(data_dir):
""" Get parties from the files in data_dir. """
data_file_paths = get_data_file_paths(data_dir)
parties = get_parties_from_files(data_file_paths)
return parties
def report_parties_without_website(parties):
""" Report parties in data_dir that lack a website attribute. """
parties_without_website = set(
party for party in parties
if not getattr(party, 'website'))
sys.stdout.write(
"Parties without website:\n"
"{parties}\n".format(parties=pprint.pformat(
parties_without_website)))
class Candidate:
""" A person standing for election. """
def __init__(
self, *,
first_name, last_name,
party=None, candidate=None, ballot_position=None, group=None,
retiring=None, elected=None,
term_start=None, expiring=None, previous_terms=None,
website=None, wikipedia=None):
self.first_name = first_name
self.last_name = last_name
self.party = party
self.candidate = candidate
self.website = website
self.wikipedia = wikipedia
def __repr__(self):
text = (
"<Candidate {first_name} {last_name} [{party}]:"
" (website: {website} Wikipedia: {wikipedia})>".format(
**vars(self)))
return text
def read_candidate_from_json(infile):
""" Read the candidate data from the specified input file.
:param infile: An open input file from which to read the JSON data.
:return: A `Candidate` instance.
"""
attrs = json.loads(infile.read())
candidate = Candidate(**attrs)
return candidate
def get_candidates_from_files(file_paths):
""" Get a collection of candidates from the specified data file paths.
:param file_paths: An iterable of file paths to JSON data files.
:return: A collection of `Candidate` instances.
"""
candidates = set()
errors = set()
for path in file_paths:
try:
infile = open(path, 'r')
except (IOError, OSError) as exc:
errors.add((path, exc))
continue
try:
candidate = read_candidate_from_json(infile)
except (TypeError, ValueError) as exc:
errors.add((path, exc))
continue
candidates.add(candidate)
if errors:
raise RuntimeError(
"The following errors occurred:\n"
"{errors}\n".format(errors=pprint.pformat(errors)))
return candidates
def get_candidates_from_data_dir(data_dir):
""" Get candidates from the files in data_dir. """
data_file_paths = get_data_file_paths(data_dir)
candidates = get_candidates_from_files(data_file_paths)
return candidates
def report_candidates_without_website(candidates):
""" Report candidates that lack a website attribute. """
candidates_without_website = set(
candidate for candidate in candidates
if not getattr(candidate, 'website', None))
sys.stdout.write(
"Candidates without website:\n"
"{candidates}\n".format(candidates=pprint.pformat(
candidates_without_website)))
def report_candidates_without_wikipedia(candidates):
""" Report candidates in data_dir that lack a ‘wikipedia’ attribute. """
candidates_without_wikipedia = set(
candidate for candidate in candidates
if not getattr(candidate, 'wikipedia', None))
sys.stdout.write(
"Candidates without Wikipedia URL:\n"
"{candidates}\n".format(candidates=pprint.pformat(
candidates_without_wikipedia)))
def main(argv):
""" Main process for this program. """
parties = get_parties_from_data_dir(parties_data_dir)
report_parties_without_website(parties)
candidates = get_candidates_from_data_dir(candidates_data_dir)
report_candidates_without_website(candidates)
report_candidates_without_wikipedia(candidates)
if __name__ == "__main__":
from sys import argv as _argv
exit_code = main(_argv)
sys.exit(exit_code)
# Local variables:
# coding: utf-8
# mode: python
# End:
# vim: fileencoding=utf-8 filetype=python :