-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
136 lines (111 loc) · 5.13 KB
/
app.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
import common # common function library
import logging
from constants import TEAMS, PLAYERS
from copy import deepcopy
from datetime import datetime
from random import randint
from statistics import mean
APP_NAME = "Basketball Team Statistics"
logging.basicConfig(filename='app.log', level=logging.DEBUG)
def distribute_players(player_list, team_list):
clean_list = []
while player_list:
for team in team_list:
random_selection = randint(0, len(player_list) - 1)
player = player_list[random_selection]
player['team'] = team
player['guardians'] = player['guardians'].split(' and ')
player['height'] = int(player['height'][:2])
player['experience'] = True if player['experience'] == 'YES' else False
clean_list.append(player_list.pop(random_selection))
return clean_list
def get_player_count(team_name):
return len([player for player in final_list if player['team'] == team_name])
def check_balancing_successful():
counts = [get_player_count(team) for team in teams]
counts = set(counts)
if len(counts) == 1:
matches = True
logging.info("{}: Team Balancing completed successfully. Each team has {} players".format(datetime.today(),counts))
else:
logging.warning("{}: Team Balancing completed unsuccessfully. Teams have {} players".format(datetime.today(),counts))
def get_player_names(team_name):
names = [player['name'] for player in final_list if player['team'] == team_name]
return ", ".join(names)
def get_other_counts(team_name, key, value):
return len([player for player in final_list if player['team'] == team_name and player[key] == value])
def get_avg_height(team_name):
height = mean([player['height'] for player in final_list if player['team'] == team_name])
return round(height, 2)
def get_guardians(team_name):
guardians = [", ".join(player['guardians']) for player in final_list if player['team'] == team_name]
guardians = set(guardians)
return ", ".join(guardians)
def display_team_list(team_list):
common.print_title("List of Teams with Statistics")
counter = 1
for team in team_list:
print("{}) {}".format(counter, team))
counter += 1
print("\n0) To RETURN to the Main Menu")
def display_team_stat(team_name):
common.clear_screen()
common.print_title("Displaying Team Statistics for: {}".format(team_name))
print("Number of Players: {}".format(get_player_count(team_name)))
print("\nPlayers: {}".format(get_player_names(team_name)))
print("\nNumber of Newbies: {}".format(get_other_counts(team_name, "experience", False)))
print("\nNumber of Pros: {}".format(get_other_counts(team_name, "experience", True)))
print("\nAverage Height: {}".format(get_avg_height(team_name)))
print("\nGuardians: {}".format(get_guardians(team_name)))
def start_app():
logging.info("{}: Application Started".format(datetime.today()))
common.clear_screen()
while True:
common.print_title("Welcome to {}".format(APP_NAME))
option_selected = common.display_menu("""Please select from available menu options e.g. 1:
1) Display Team Statistics
2) Quit
""")
try:
option_selected = int(option_selected)
if option_selected == 1:
common.clear_screen()
sub_menu = True
while sub_menu:
display_team_list(teams)
option_selected = common.display_menu("")
try:
option_selected = int(option_selected)
if option_selected < 0 or option_selected > len(teams):
raise ValueError
elif option_selected == 0:
common.clear_screen()
sub_menu = False
else:
team = teams[option_selected-1]
display_team_stat(team)
input("\nPress ENTER to continue ... ")
common.clear_screen()
except ValueError:
common.invalid_entry(option_selected)
continue
elif option_selected == 2:
print()
common.print_title("Thank you for using {}!".format(APP_NAME))
logging.info("{}: Application Closed".format(datetime.today()))
break
else:
raise ValueError
except ValueError:
common.invalid_entry(option_selected)
continue
if __name__ == "__main__":
teams = deepcopy(TEAMS)
players = deepcopy(PLAYERS)
pros = [player for player in players if player["experience"] == "YES"]
newbies = [player for player in players if player["experience"] == "NO"]
final_list = []
final_list.extend(distribute_players(pros, teams))
final_list.extend(distribute_players(newbies, teams))
check_balancing_successful()
start_app()