Skip to content

Commit fc774f9

Browse files
authored
Switch from inline type annotations to using type comments
The inline form was making the code harder to read
1 parent 316d138 commit fc774f9

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

congress.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
VoteValue = float
1616

1717
# Load votes arranged by topic and accumulate votes by senator
18-
vote_value: Dict[str, VoteValue] = {'Nay': -1, 'Not Voting': 0, 'Yea': 1}
19-
accumulated_record: DefaultDict[Senator, List[VoteValue]] = defaultdict(list)
18+
vote_value = {'Nay': -1, 'Not Voting': 0, 'Yea': 1} # type: Dict[str, VoteValue]
19+
accumulated_record = defaultdict(list) # type: DefaultDict[Senator, List[VoteValue]]
2020
for filename in glob.glob('congress_data/*.csv'):
2121
with open(filename, encoding='utf-8') as f:
2222
reader = csv.reader(f)
@@ -27,22 +27,22 @@
2727
accumulated_record[senator].append(vote_value[vote])
2828

2929
# Transform record into plain dict mapping a senator to a tuple of vote values
30-
record: Dict[Senator, Tuple[VoteValue, ...]] = {senator: tuple(votes) for senator, votes in accumulated_record.items() }
30+
record = {senator: tuple(votes) for senator, votes in accumulated_record.items() } # type: Dict[Senator, Tuple[VoteValue, ...]]
3131

3232
# Use k-means to locate the cluster centroids and assign senators to the nearest cluster
3333
centroids = k_means(record.values(), k=3, iterations=50)
3434
clustered_votes = assign_data(centroids, record.values())
3535

3636
# Build a reverse mapping from a pattern of votes to senators who voted that way
37-
votes_to_senators: DefaultDict[Tuple[VoteValue, ...], List[Senator]] = defaultdict(list)
37+
votes_to_senators = defaultdict(list) # type: DefaultDict[Tuple[VoteValue, ...], List[Senator]]
3838
for senator, votes in record.items():
3939
votes_to_senators[votes].append(senator)
40-
assert sum(len(cluster) for cluster in clustered_votes.values()) == 100
40+
assert sum(map(len, clustered_votes.values())) == 100
4141

4242
# Display the clusters and the members of each cluster
4343
for i, votes_in_cluster in enumerate(clustered_votes.values(), start=1):
4444
print(f'=========== Voting Cluster #{i} ===========')
45-
party_totals: Counter = Counter()
45+
party_totals = Counter() # type: Counter
4646
for votes in set(votes_in_cluster):
4747
for senator in votes_to_senators[votes]:
4848
party_totals[senator.party] += 1

0 commit comments

Comments
 (0)