Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.98 KB | None | 0 0
  1. """"The MAIN module in a process that aims to take textual input scores
  2.  
  3. for a soccer (football) league and determine who won the season based
  4.  
  5. on the scoring rules for the league."""
  6.  
  7.  
  8.  
  9. import argparse
  10.  
  11. from collections import namedtuple
  12.  
  13. import pathlib
  14.  
  15.  
  16.  
  17. class PremierLeague:
  18.  
  19.     """This class ingests a text file and transforms that into workable
  20.  
  21.    units to determine the scores of each team in the league"""
  22.  
  23.  
  24.  
  25.     Match = namedtuple('Match', ['team','score'])
  26.  
  27.  
  28.  
  29.     def __init__(self, text_file:pathlib.Path):
  30.  
  31.         if not isinstance(text_file,pathlib.Path):
  32.  
  33.             try:
  34.  
  35.                 text_file = pathlib.Path(text_file)
  36.  
  37.             except Exception as e:
  38.  
  39.                 print(e)
  40.  
  41.  
  42.  
  43.         self.text_file = text_file
  44.  
  45.         with self.text_file.open('r') as f:
  46.  
  47.             self.lines = f.readlines()
  48.  
  49.  
  50.  
  51.         self.season = []
  52.  
  53.         self.teams = dict()
  54.  
  55.         self.rankings = []
  56.  
  57.         self.extract_team_data()
  58.  
  59.         self.extract_teams()
  60.  
  61.         self.calculate_standings()
  62.  
  63.         print(self.standings)
  64.  
  65.  
  66.  
  67.     def extract_team_data(self):
  68.  
  69.         for line in self.lines:
  70.  
  71.             line.strip()
  72.  
  73.             game = line.split(', ')
  74.  
  75.             match = []
  76.  
  77.             for team in game:
  78.  
  79.                 if team[-1] == '\n':
  80.  
  81.                     team = team[:-1]
  82.  
  83.                 match.append(self.Match(team[:-1],team[-1]))
  84.  
  85.             self.season.append(match)
  86.  
  87.  
  88.  
  89.     def extract_teams(self):
  90.  
  91.         for game in self.season:
  92.  
  93.             for FC in game:
  94.  
  95.                 if FC.team not in self.teams:
  96.  
  97.                     self.teams[FC.team] = 0
  98.  
  99.  
  100.  
  101.     def calculate_standings(self):
  102.  
  103.         for game in self.season:
  104.  
  105.             team1 = game[0]
  106.  
  107.             team2 = game[1]
  108.  
  109.             if team1.score > team2.score:
  110.  
  111.                 self.teams[team1.team] += 3
  112.  
  113.             elif team2.score > team1.score:
  114.  
  115.                 self.teams[team2.team] =+ 3
  116.  
  117.             else:
  118.  
  119.                 # this means it was a tie
  120.  
  121.                 self.teams[team1.team] += 1
  122.  
  123.                 self.teams[team2.team] += 1
  124.  
  125.  
  126.  
  127.     @property
  128.  
  129.     def standings(self):
  130.  
  131.         rankings = [self.Match(k,v) for k,v in self.teams.items()]
  132.  
  133.         rankings.sort(key=self.get_score, reverse=True)
  134.  
  135.         list_of_strings = ["{0} ,{1} pts".format(team.team, team.score) for team in rankings]
  136.  
  137.         temp_string = ""
  138.  
  139.         for index in range(len(list_of_strings)):
  140.  
  141.             temp_string += "{0}. {1}\n".format(str(index+1), list_of_strings[index])
  142.  
  143.         return temp_string
  144.  
  145.  
  146.  
  147.     @staticmethod
  148.  
  149.     def get_score(element):
  150.  
  151.         """returns the score element of an Match object"""
  152.  
  153.         return element.score
  154.  
  155.  
  156.  
  157. if __name__ == '__main__':
  158.  
  159.     args = argparse.ArgumentParser()
  160.  
  161.     args.add_argument('--file',action='store', default='test.txt')
  162.  
  163.     results = args.parse_args()
  164.  
  165.     scores = PremierLeague(pathlib.Path(results.file))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement