/
statistics.py
65 lines (49 loc) · 2.29 KB
/
statistics.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
"""
Helper functions for printing out statistics.
TODO: Refactor into a class
"""
from util import print_to_screen_and_file
from util import get_border
_REPORT_TITLE = 'STATISTICS REPORT'
def print_statistics(text_file, num_wins, winning_ratios, num_contiguous):
"""Prints statistics report.
Includes:
* A report header
* Percentage of which ratio won each election
* Percentage of elections each party won
"""
__print_statistics_report_header(text_file)
__print_winning_ratios(text_file, winning_ratios, num_contiguous)
for party in num_wins.keys():
percent_won = __get_percent_elections_won(num_wins, party, num_contiguous)
__print_percent_won(text_file, party, percent_won)
def __get_percent_elections_won(num_wins, party, num_contiguous):
"""Get the percentage of elections won by a party
:param num_wins: A dictionary containing parties as keys and election wins as values.
:param party: The party.
:param num_contiguous: The number of contiguous redistrictings.
:return: The percent of total elections won.
"""
num_elections_won = num_wins[party]
return num_elections_won / num_contiguous * 100
def __print_statistics_report_header(text_file):
"""Print the header to the statistics report."""
report_title = _REPORT_TITLE
print_to_screen_and_file(report_title, text_file)
border = get_border('-', len(report_title))
print_to_screen_and_file(border, text_file)
def __print_percent_won(text_file, party, percentage_won):
"""Print the percentage of elections the party won."""
message = party + " won " + str(round(percentage_won, 2)) + "% percent of elections."
print_to_screen_and_file(message, text_file)
def __print_winning_ratios(text_file, winning_ratios, num_contiguous):
for key in winning_ratios.keys():
ratio = key.split(':')
num_green_wins = ratio[0]
num_purple_wins = ratio[1]
message = "Green won " + num_green_wins + " districts "
message += "and Purple won " + num_purple_wins + " districts "
percent = winning_ratios[key] / num_contiguous * 100
message += str(round(percent, 2)) + "% of the time."
print_to_screen_and_file(message, text_file)
print_to_screen_and_file("", text_file) # Print extra newline character