def __init__(self, div_stand=False, division=None, conference=None, team=None, team_info_obj=None): self.div_stand = div_stand self.div = division self.conf = conference self.team = team if team_info_obj == None: self.TI = TeamInfo() else: self.TI = team_info_obj self.raw_data = get_standings_data(division=self.div_stand) self._internal = self.raw_data['_internal'] self.league = self.raw_data['league'] self.standard = self.league['standard'] self.conference = self.standard['conference'] self.east = self.conference['east'] self.west = self.conference['west']
def __init__(self, division=None, conference=None, div_stand=False, team_info_obj=None): self.div_stand = div_stand if team_info_obj: self.TI = team_info_obj else: self.TI = TeamInfo() self.S = None self.DS = None if self.div_stand == 'conference': self.S = Standings(div_stand=div_stand, division=division, conference=conference, team_info_obj=self.TI) elif self.div_stand == 'division': self.DS = Standings(div_stand=div_stand, division=division, conference=conference, team_info_obj=self.TI) self.standing_headers = [ 'Team', 'W', 'L', 'PCT', 'GB', 'HOME', 'AWAY', 'DIV', 'CONF', 'L10', 'STRK' ] self.games_back = 'divGamesBehind' if div_stand else 'gamesBehind'
def __init__(self, team_info_obj=None): self.config_folder = os.path.dirname(os.path.abspath(__file__)) self.config_path = os.path.join(self.config_folder, 'config.ini') self.config = configparser.RawConfigParser() if self.config.read(self.config_path) == []: self.create_config() if team_info_obj: self.TI = team_info_obj else: self.TI = TeamInfo()
def __init__(self): colorama.init() self.CA = ConfigureApp() self.TI = TeamInfo() self.PI = PlayerInfo()
class NBA_UI(): def __init__(self): colorama.init() self.CA = ConfigureApp() self.TI = TeamInfo() self.PI = PlayerInfo() def create_widgets(self): self.SB = ScoreBoardUI() self.B = BoxScoreUI() self.S = ScheduleUI(next=True, last=True, full=False) #add to display self.TL = TeamLeadersUI(player_info_obj=self.PI) self.ST = StandingsUI(div_stand=get_info(('Default', 'div_stand')), team_info_obj=self.TI) def run(self, scoreboard=True, boxscore=True, score=True, teamleaders=True, standings=True): self.config_display() self.create_widgets() self.dashboard(scoreboard, boxscore, score, teamleaders, standings) def dashboard(self, scoreboard=True, boxscore=True, score=True, teamleaders=True, standings=True): if scoreboard: self.SB.display(horiz=True) #config option if boxscore: self.B.display() if score: self.S.display() if teamleaders: self.TL.display() if standings: self.ST.display(div_stand=get_info(('Default', 'div_stand'))) self.options() def config_display(self): print(f"Welcome to PyNBAScore\n") self.CA.configure() print(f"Loading Dashboard...") def quit(self): colorama.deinit() sys.exit() def options(self): while True: print(f"\nQ: Quit\n1: Standings\n2: Schedule\n" f"3: Team Leaders\nC: Config Options\nD: Dashboard") user_input = input() if user_input == '1': self.standings_ui() elif user_input == '2': self.schedule_ui() elif user_input == '3': self.team_leaders_ui() elif user_input.lower() == 'c': self.config_dashboard_ui() elif user_input.lower() == 'd': self.dashboard() elif user_input.lower() == 'q': self.quit() """Widget UIS. Used to navigate stats.""" def boxscore_ui(self): print(f"Select a boxscore to look at.") """Standings UI""" def standings_ui(self): print(f"Select an option\n1: Eastern Conference\n2: Western Conference" f"\n3: Division Standings \na: Both Conferences \n") user_input = input() if user_input == '1': self.ST.display(conference='east', div_stand='conference') elif user_input == '2': self.ST.display(conference='west', div_stand='conference') elif user_input == 'a': self.ST.display(conference='east', div_stand='conference') self.ST.display(conference='west', div_stand='conference') elif user_input == '3': print(f"Select a division:" f"\n1: Atlantic \n2: Central \n3: SouthEast \n4: Pacific" f"\n5: SouthWest \n6: NorthWest \na: All divisions" ) user_input = input() if user_input == '1': self.ST.display(division='atlantic', conference='east') elif user_input == '2': self.ST.display(division='central', conference='east') elif user_input == '3': self.ST.display(division='southeast', conference='east') elif user_input == '4': self.ST.display(division='pacific', conference='west') elif user_input == '5': self.ST.display(division='southwest', conference='west') elif user_input == '6': self.ST.display(division='northwest', conference='west') elif user_input == 'a': self.ST.display(division='atlantic', conference='east') self.ST.display(division='central', conference='east') self.ST.display(division='southeast', conference='east') self.ST.display(division='pacific', conference='west') self.ST.display(division='southwest', conference='west') self.ST.display(division='northwest', conference='west') elif user_input == 'q': self.quit() def schedule_ui(self): """UI options for checking teams schedule. TODO: Adjust scheduleUI to have time start for full/vertical options Config options for user to select default showing for option. """ print(f"Select team to check out their schedule.") for team in self.TI.teamsTRI: print(team) user_input = input("\nTricode: ") team = self.TI.get_team(user_input.upper(), id_option='tricode', id_return='nickname') print(f"Team Selected: {team}...Loading Data...") if not team: print(f"Invalid tricode: {user_input}, couldn't find team") else: TS = ScheduleUI(team=team.lower()) TS.display(horiz=True) def team_leaders_ui(self): print(f"Select a team to check their leaders" ) for team in self.TI.teamsTRI: print(team) user_input = input("\nTricode: ") team = self.TI.get_team(user_input.upper(), id_option='tricode', id_return='nickname') print(f"Team Selected: {team}...Loading Data...") if not team: print(f"Invalid tricode: {user_input}, couldn't find team") else: TL = TeamLeadersUI(team=team) TL.display() """Configurate Dashboard, and Other Options UI""" def config_dashboard_ui(self): print(f"Config Dashboard Not Implemented.") print(f"1: Table Styles\n2: Toggle Colors\n3: Toggle Full Names" f"\n4: Dashboard Options") user_input = input() if user_input == '1': #self.table_options_ui() pass elif user_input == '2': print("Color Toggled") self.CA.toggle_color() elif user_input == '3': self.CA.toggle_full_name() elif user_input == '4': self.dashboard_options_ui() elif user_input.lower() == 'q:': self.quit() else: self.options() def dashboard_options_ui(self): print(f"1: Toggle Conference/Division Standings\n") user_input = input() if user_input == '1': self.CA.toggle_standings()
class Standings(object): """A class to sort and hold data for NBA standing_division.json endpoint and the standing_conference.json endpoint. Attributes: div_stand: Boolean, On whether to make call to division endpoit or the conference endpoint. True = Division, False = Conference. div: String, corrosponding to the division of data wanted conf: String, corrosponding to the conference of data wanted team: String, corrosponding to the team of data wanted """ def __init__(self, div_stand=False, division=None, conference=None, team=None, team_info_obj=None): self.div_stand = div_stand self.div = division self.conf = conference self.team = team if team_info_obj == None: self.TI = TeamInfo() else: self.TI = team_info_obj self.raw_data = get_standings_data(division=self.div_stand) self._internal = self.raw_data['_internal'] self.league = self.raw_data['league'] self.standard = self.league['standard'] self.conference = self.standard['conference'] self.east = self.conference['east'] self.west = self.conference['west'] def get_standing_data(self): """Method used to get appropriate data for main dashboard widget. """ if self.div and self.conf: return self.conference[self.conf.lower()][self.div.lower()] elif self.conf: return self.conference[self.conf.lower()] elif self.div: east = ['atlantic', 'central', 'southeast'] if self.div.lower() in east: conf = 'east' else: conf = 'west' return self.conference[conf.lower()][self.div.lower()] else: data = self._get_conf_division(self.team) if self.div_stand == 'division': return self.conference[data[0].lower()][data[1].lower()] elif self.div_stand == 'conference': return self.conference[data[0].lower()] def _get_conf_division(self, team=None): """Method to get division of given team. Args: team: team url, ie raptors, sixers, for team who division you want to acquire. If none, uses favourite team from config. """ if not team: team = get_team() return self.TI.get_conf_division(team)
class ConfigureApp(object): def __init__(self, team_info_obj=None): self.config_folder = os.path.dirname(os.path.abspath(__file__)) self.config_path = os.path.join(self.config_folder, 'config.ini') self.config = configparser.RawConfigParser() if self.config.read(self.config_path) == []: self.create_config() if team_info_obj: self.TI = team_info_obj else: self.TI = TeamInfo() def configure(self): self.config_settings() self._set_default_season() self.check_season() def config_settings(self): if self.config.get('Initial Config', 'config') != 'True': self.set_presets() def set_presets(self): """Sets initial presets in config .ini file for app to run properly. Includes: season year, team of choice... """ fave_team = self._team_select() my_team = self.TI.get_team(fave_team.upper(), id_return='urlName') self.config.set('Default', 'team', str(my_team)) self.config.set('Initial Config', 'config', 'True') self.config.set('Default', 'season', self._set_default_season()) self.config.set('Default', 'color', 'False') with open(self.config_path, 'w') as configfile: self.config.write(configfile) def create_config(self): self.config['Initial Config'] = {'config': 'False'} self.config['Default'] = { 'team': None, 'season': '2019', 'color': 'False', 'standing': 'conference' } with open(self.config_path, 'w') as configfile: self.config.write(configfile) def _team_select(self): """Displays all NBA teams, and provides input to chose a team. TODO: break up code, separate options and functions to grab team. UI method. """ teams = [] for team in self.TI.teamsTRI: print(team) teams.append(team) while True: print(f"Please input desired teams tricode, ex ORL, PHI, PHX etc:") team = input() if team.upper() in teams: return str(team.upper()) elif team.lower() == 'q': sys.exit() else: print(f"invalid input, try again.") def _set_default_season(self): """Sets season default to the year of current season, done so for when season is left blank in any function that reqeuires it """ url = 'http://data.nba.net/10s/prod/v1/today.json' get_data(url) season = get_data(url)['teamSitesOnly']['seasonYear'] return str(season) def check_season(self): """ Checks to make sure the season setting is correct, then corrects it """ current_season = self._set_default_season() season_check = self.config.get('Default', 'season') if season_check != current_season: self.config.set('Default', 'season', current_season) with open(self.config_path, 'w') as configfile: self.config.write(configfile) """Change Config Menus""" def toggle_color(self): if self.config.get('Default', 'color') != 'True': print('Color off, turning on') self.config.set('Default', 'color', 'True') else: print('Color Turned On') self.config.set('Default', 'color', 'False') with open(self.config_path, 'w') as configfile: self.config.write(configfile) def toggle_standings(self): if self.config.get('Default', 'div_stand') == 'conference': self.config.set('Default', 'div_stand', 'division') else: self.config.set('Default', 'div_stand', 'conference') with open(self.config_path, 'w') as configfile: self.config.write(configfile) def toggle_full_name(self): if self.config.get('Default', 'full_names_players') == 'True': self.config.set('Default', 'full_names_players', 'False') else: self.config.set('Default', 'full_names_players', 'True') with open(self.config_path, 'w') as configfile: self.config.write(configfile)
class TestTeamInfo(unittest.TestCase): def test_get_team_data(self): data = get_team_data() self.assertIsNotNone(data) self.assertIn("_internal", data.keys()) @classmethod def setUpClass(self): self.TI = TeamInfo() def test_get_conf_division_east(self): """Tests get_fonf_division method to assure return values are proper. Does so by testing various teams of various divisions in Eastern conference.""" res = self.TI.get_conf_division(team='SiXeRs') self.assertEqual(res, ('East', 'Atlantic')) res = self.TI.get_conf_division(team='cavaliers') self.assertEqual(res, ('East', 'Central')) res = self.TI.get_conf_division(team='hornets') self.assertEqual(res, ('East', 'Southeast')) def test_get_conf_division_west(self): """Tests get_fonf_division method to assure return values are proper. Does so by testing various teams of various divisions in Western conference.""" res = self.TI.get_conf_division(team='Warriors') self.assertEqual(res, ('West', 'Pacific')) res = self.TI.get_conf_division(team='pelIcANS') self.assertEqual(res, ('West', 'Southwest')) res = self.TI.get_conf_division(team='blazers') self.assertEqual(res, ('West', 'Northwest')) def test_get_team_city(self): """Tests the get_team function, specifically the id_option/id_return paramters to assure that they return and fetch by the corect value.""" res = self.TI.get_team(identifier='Toronto', id_option='city', id_return='urlName') exp = 'raptors' self.assertEqual(res, exp) def test_get_team_urlName(self): """Tests the get_team function, specifically the id_option/id_return paramters to assure that they return and fetch by the corect value.""" res = self.TI.get_team(identifier='warriors', id_option='urlName', id_return='city') exp = 'Golden State' self.assertEqual(res, exp) def test_get_team_triCode(self): """Tests the get_team function, specifically the id_option/id_return paramters to assure that they return and fetch by the corect value.""" res = self.TI.get_team(identifier='POR', id_option='triCode', id_return='city') exp = 'Portland' self.assertEqual(res, exp) def test_get_team_fullName(self): """Tests the get_team function, specifically the id_option/id_return paramters to assure that they return and fetch by the corect value.""" res = self.TI.get_team(identifier='LA Clippers', id_option='fullname', id_return='city') exp = 'LA' self.assertEqual(res, exp) def test_get_team_teamId(self): """Tests the get_team function, specifically the id_option/id_return paramters to assure that they return and fetch by the corect value.""" res = self.TI.get_team(identifier='1610612749', id_option='teamId', id_return='nickname') exp = 'Bucks' self.assertEqual(res, exp) def test_get_team_nickname(self): """Tests the get_team function, specifically the id_option/id_return paramters to assure that they return and fetch by the corect value.""" res = self.TI.get_team(identifier='Raptors', id_option='nickname', id_return='tricode') exp = 'TOR' self.assertEqual(res, exp) def test_create_nba_list(self): """Tests to assure create_nba_list functions returns a list of of strings all with len of 3 characters long.""" res = self.TI.create_nba_list() for team in res: self.assertEqual(len(team), 3) def test_create_nba_list_2(self): """Tests to assure all 30 NBA teams(labeled by tri code) and in the teaminfo object list to assure the create_nba_list function is working correctly""" nba_teams = [ 'ATL', 'BOS', 'BKN', 'CHA', 'CHI', 'CLE', 'DAL', 'DEN', 'DET', 'GSW', 'HOU', 'IND', 'LAC', 'LAL', 'MEM', 'MIA', 'MIL', 'MIN', 'NOP', 'NYK', 'OKC', 'ORL', 'PHI', 'PHX', 'POR', 'SAC', 'SAS', 'TOR', 'UTA', 'WAS' ] for team in nba_teams: self.assertIn(team, self.TI.teamsTRI)
def setUpClass(self): self.TI = TeamInfo()
class StandingsUI(Widget): """A class to represent nba divisional standings Attributes: division: division wanted to search for, can look up if unknown conference: conference the division wanted is located in, can look up. div_stand: Choice to search division or conference endpoint. team_info_obj: TeamInfo Class object, used to prevent making multiple instances. S: Standings object, used to hold information for conference standings. DS: Standings object, used to hold information for division standings. """ def __init__(self, division=None, conference=None, div_stand=False, team_info_obj=None): self.div_stand = div_stand if team_info_obj: self.TI = team_info_obj else: self.TI = TeamInfo() self.S = None self.DS = None if self.div_stand == 'conference': self.S = Standings(div_stand=div_stand, division=division, conference=conference, team_info_obj=self.TI) elif self.div_stand == 'division': self.DS = Standings(div_stand=div_stand, division=division, conference=conference, team_info_obj=self.TI) self.standing_headers = [ 'Team', 'W', 'L', 'PCT', 'GB', 'HOME', 'AWAY', 'DIV', 'CONF', 'L10', 'STRK' ] self.games_back = 'divGamesBehind' if div_stand else 'gamesBehind' def display(self, div_stand=None, conference=None, division=None): if not div_stand: div_stand = self.div_stand if division: div_stand = 'division' if div_stand == 'conference': if not self.S: self.S = Standings(div_stand='conference', division=division, conference=conference) self.games_back = 'gamesBehind' if not conference and not division: #No info on conf or div self.standing_data = self.S.get_standing_data() elif conference and not division: #know conf but not div self.standing_data = self.S.conference[conference] elif div_stand == 'division': if not self.DS: self.DS = Standings(div_stand='division', division=division, conference=conference) self.games_back = 'divGamesBehind' if not division and not conference: print('THAT ONE') conference, division = self.DS._get_conf_division() self.standing_data = self.DS.conference[conference.lower()]\ [division.lower()] else: print('THIS ONE') self.standing_data = self.DS.conference[conference][division] self.create_standing_data_keys() if division: self.horizontal_display(self.DS, header=True) else: self.horizontal_display(self.S, header=True) def create_standing_data_keys(self): self.standing_data_keys = [ 'teamId', 'win', 'loss', 'winPct', self.games_back, ('homeWin', 'homeLoss'), ('awayWin', 'awayLoss'), ('divWin', 'divLoss'), ('confWin', 'confLoss'), ('lastTenWin', 'lastTenLoss'), ('isWinStreak', 'streak') ] def create_nested_list(self, data): nested_list = [] """Stats Header""" nested_list.append(self.standing_headers) """Information""" for team in self.standing_data: team_list = [] for item in self.standing_data_keys: if item == 'teamId': team_list.append( self.TI.get_team(team[item], item, 'nickname')) elif type(item) is tuple: if item[0] == 'isWinStreak': if team[item[0]] == True: team_list.append(f"W{team[item[1]]}") else: team_list.append(f"L{team[item[1]]}") else: team_list.append(f"{team[item[0]]}-{team[item[1]]}") else: team_list.append(f"{team[item]}") nested_list.append(team_list) return nested_list def get_nested_list(self, data): """Calls create_nested_list method in order to get the values for the nested list to be passed on to create_tabulate_table method""" return self.create_nested_list(data)