-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleague_builder.py
94 lines (73 loc) · 2.95 KB
/
league_builder.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
import csv
CSVFILE = "soccer_players.csv"
TEAMS = ["Sharks", "Dragons", "Raptors"]
TEAMFILE = "teams.txt"
LETTER_TEMPLATE = """Dear {},
I am delighted to inform you that {} has been selected to play
for the {} team in the upcoming season. First practice is at 19:00 on
November 5th 2018 at Old Trafford.
See you there!"""
def get_experienced_players(players):
"""Create a list of players with prior soccer experience.
"""
return [player for player in players if player["Soccer Experience"] == "YES"]
def get_novice_players(players):
"""Create a list of players without prior soccer experience.
"""
return [player for player in players if player["Soccer Experience"] == "NO"]
def create_teams(experienced_players, novice_players):
"""Create a list of teams evenly distributed from available players.
"""
num_teams = len(TEAMS)
# Divide experienced players into number of desired teams
experienced_groups = [experienced_players[i::num_teams] for i in range(num_teams)]
# Divide remaining players into number of desired teams
novice_groups = [novice_players[i::num_teams] for i in range(num_teams)]
# Create list of dicts, each containing final team roster.
return [
{"team": team, "players": experienced_groups.pop() + novice_groups.pop()}
for team in TEAMS
]
def read_player_list():
"""Create a list of players from CSV file.
"""
with open(CSVFILE, "r") as csv_file:
players = csv.DictReader(csv_file)
return [row for row in players]
def write_teams(teams):
"""Write each provided team and its players out to a text file.
"""
# Player information we want for team roster.
pertinent_stats = {"Name", "Soccer Experience", "Guardian Name(s)"}
with open(TEAMFILE, "a") as team_file:
for team in teams:
team_file.write("{}\n".format(team["team"]))
for player in team["players"]:
team_file.write(
"{}, {}, {}\n".format(
*[v for k, v in player.items() if k in pertinent_stats]
)
)
def create_letters(teams):
"""Create a letter for the guardian(s) of each selected player.
"""
for team in teams:
for player in team["players"]:
letter_file = player["Name"].lower().replace(" ", "_") + ".txt"
with open(letter_file, "w") as player_letter:
player_letter.write(
LETTER_TEMPLATE.format(
player["Guardian Name(s)"], player["Name"], team["team"]
)
)
def main():
"""Main program run logic.
"""
available_players = read_player_list()
experienced_players = get_experienced_players(available_players)
novice_players = get_novice_players(available_players)
teams = create_teams(experienced_players, novice_players)
write_teams(teams)
create_letters(teams)
if __name__ == "__main__":
main()