Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """"The MAIN module in a process that aims to take textual input scores
- for a soccer (football) league and determine who won the season based
- on the scoring rules for the league."""
- import argparse
- from collections import namedtuple
- import pathlib
- class PremierLeague:
- """This class ingests a text file and transforms that into workable
- units to determine the scores of each team in the league"""
- Match = namedtuple('Match', ['team','score'])
- def __init__(self, text_file:pathlib.Path):
- if not isinstance(text_file,pathlib.Path):
- try:
- text_file = pathlib.Path(text_file)
- except Exception as e:
- print(e)
- self.text_file = text_file
- with self.text_file.open('r') as f:
- self.lines = f.readlines()
- self.season = []
- self.teams = dict()
- self.rankings = []
- self.extract_team_data()
- self.extract_teams()
- self.calculate_standings()
- print(self.standings)
- def extract_team_data(self):
- for line in self.lines:
- line.strip()
- game = line.split(', ')
- match = []
- for team in game:
- if team[-1] == '\n':
- team = team[:-1]
- match.append(self.Match(team[:-1],team[-1]))
- self.season.append(match)
- def extract_teams(self):
- for game in self.season:
- for FC in game:
- if FC.team not in self.teams:
- self.teams[FC.team] = 0
- def calculate_standings(self):
- for game in self.season:
- team1 = game[0]
- team2 = game[1]
- if team1.score > team2.score:
- self.teams[team1.team] += 3
- elif team2.score > team1.score:
- self.teams[team2.team] =+ 3
- else:
- # this means it was a tie
- self.teams[team1.team] += 1
- self.teams[team2.team] += 1
- @property
- def standings(self):
- rankings = [self.Match(k,v) for k,v in self.teams.items()]
- rankings.sort(key=self.get_score, reverse=True)
- list_of_strings = ["{0} ,{1} pts".format(team.team, team.score) for team in rankings]
- temp_string = ""
- for index in range(len(list_of_strings)):
- temp_string += "{0}. {1}\n".format(str(index+1), list_of_strings[index])
- return temp_string
- @staticmethod
- def get_score(element):
- """returns the score element of an Match object"""
- return element.score
- if __name__ == '__main__':
- args = argparse.ArgumentParser()
- args.add_argument('--file',action='store', default='test.txt')
- results = args.parse_args()
- scores = PremierLeague(pathlib.Path(results.file))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement