Skip to content

Commit

Permalink
Add string representation for all classes
Browse files Browse the repository at this point in the history
All of the methods should have a proper string representation of what
the class represents to better allow users to see what the purpose of
the class is.

Signed-Off-By: Robert Clark <robdclark@outlook.com>
  • Loading branch information
roclark committed Sep 26, 2020
1 parent 243956d commit 7f0aec9
Show file tree
Hide file tree
Showing 54 changed files with 2,141 additions and 58 deletions.
27 changes: 23 additions & 4 deletions sportsreference/fb/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ def __init__(self, player_data, player_id):

self._parse_player_stats(player_data)

def __str__(self):
"""
Return the string representation of the class.
"""
return f'{self.name} ({self.player_id})'

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _parse_nationality(self, player_data):
"""
Parse the player's nationality.
Expand Down Expand Up @@ -1518,23 +1530,30 @@ def __call__(self, player):
return player_instance
raise ValueError('No player found with the requested name or ID')

def __str__(self):
"""
Return the string representation of the class.
"""
players = [f'{x.name} ({x.player_id})'.strip() for x in self._players]
return '\n'.join(players)

def __repr__(self):
"""
Returns a ``list`` of all players for the given team.
Return the string representation of the class.
"""
return self._players
return self.__str__()

def __iter__(self):
"""
Returns an iterator of all of the players on the given team's roster.
"""
return iter(self.__repr__())
return iter(self._players)

def __len__(self):
"""
Returns the number of player on the given team's roster.
"""
return len(self.__repr__())
return len(self._players)

def _player_id(self, player_data):
"""
Expand Down
28 changes: 24 additions & 4 deletions sportsreference/fb/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ def __init__(self, game_data):

self._parse_game_data(game_data)

def __str__(self):
"""
Return the string representation of the class.
"""
return f'{self.date} - {self.opponent}'

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _parse_opponent_id(self, game_data):
"""
Parse the opponent's squad ID.
Expand Down Expand Up @@ -532,23 +544,31 @@ def __call__(self, date):
return game
raise ValueError('No games found for requested date')

def __str__(self):
"""
Return the string representation of the class.
"""
games = [f'{game.date} - {game.opponent}'.strip()
for game in self._games]
return '\n'.join(games)

def __repr__(self):
"""
Returns a ``list`` of all games scheduled for the given team.
Return the string representation of the class.
"""
return self._games
return self.__str__()

def __iter__(self):
"""
Returns an iterator of all of the games scheduled for the given team.
"""
return iter(self.__repr__())
return iter(self._games)

def __len__(self):
"""
Returns the number of scheduled games for the given team.
"""
return len(self.__repr__())
return len(self._games)

def _add_games_to_schedule(self, schedule):
"""
Expand Down
12 changes: 12 additions & 0 deletions sportsreference/fb/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def __init__(self, team_id):
self._squad_id = _lookup_team(team_id)
self._pull_team_page()

def __str__(self):
"""
Return the string representation of the class.
"""
return f'{self.name} ({self.squad_id}) - {self.season}'

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _parse_name(self, doc):
"""
Parse the team's name and season.
Expand Down
25 changes: 25 additions & 0 deletions sportsreference/mlb/boxscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,19 @@ def __init__(self, uri):

self._parse_game_data(uri)

def __str__(self):
"""
Return the string representation of the class.
"""
return (f'Boxscore for {self._away_name.text()} at '
f'{self._home_name.text()} ({self.date})')

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _retrieve_html_page(self, uri):
"""
Download the requested HTML page.
Expand Down Expand Up @@ -1629,6 +1642,18 @@ def __init__(self, date, end_date=None):

self._find_games(date, end_date)

def __str__(self):
"""
Return the string representation of the class.
"""
return f"MLB games for {', '.join(self._boxscores.keys())}"

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

@property
def games(self):
"""
Expand Down
26 changes: 26 additions & 0 deletions sportsreference/mlb/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ def __init__(self, player_id):
self._find_initial_index()
AbstractPlayer.__init__(self, player_id, self._name, player_data)

def __str__(self):
"""
Return the string representation of the class.
"""
return f'{self.name} ({self.player_id})'

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _build_url(self):
"""
Create the player's URL to pull stats from.
Expand Down Expand Up @@ -1445,6 +1457,20 @@ def __init__(self, team, year=None, slim=False):

self._find_players(year)

def __str__(self):
"""
Return the string representation of the class.
"""
players = [f'{player.name} ({player.player_id})'.strip()
for player in self._players]
return '\n'.join(players)

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _pull_team_page(self, url):
"""
Download the team page.
Expand Down
30 changes: 26 additions & 4 deletions sportsreference/mlb/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ def __init__(self, game_data, year):

self._parse_game_data(game_data)

def __str__(self):
"""
Return the string representation of the class.
"""
return f'{self.date} - {self.opponent_abbr}'

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _parse_boxscore(self, game_data):
"""
Parses the boxscore URI for the game.
Expand Down Expand Up @@ -429,19 +441,29 @@ def __call__(self, date, game_number=1):
return game
raise ValueError('No games found for requested date')

def __str__(self):
"""
Return the string representation of the class.
"""
games = [f'{game.date} - {game.opponent_abbr}'.strip()
for game in self._games]
return '\n'.join(games)

def __repr__(self):
"""Returns a ``list`` of all games scheduled for the given team."""
return self._games
"""
Return the string representation of the class.
"""
return self.__str__()

def __iter__(self):
"""
Returns an iterator of all of the games scheduled for the given team.
"""
return iter(self.__repr__())
return iter(self._games)

def __len__(self):
"""Returns the number of scheduled games for the given team."""
return len(self.__repr__())
return len(self._games)

def _pull_schedule(self, abbreviation, year):
"""
Expand Down
34 changes: 28 additions & 6 deletions sportsreference/mlb/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ def __init__(self, team_name=None, team_data=None, rank=None, year=None):

self._parse_team_data(team_data)

def __str__(self):
"""
Return the string representation of the class.
"""
return f'{self.name} ({self.abbreviation}) - {self._year}'

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _retrieve_team_data(self, year, team_name):
"""
Pull all stats for a specific team.
Expand Down Expand Up @@ -1198,6 +1210,20 @@ def __init__(self, year=None):
team_data_dict, year = _retrieve_all_teams(year)
self._instantiate_teams(team_data_dict, year)

def __str__(self):
"""
Return the string representation of the class.
"""
teams = [f'{team.name} ({team.abbreviation})'.strip()
for team in self._teams]
return '\n'.join(teams)

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def __getitem__(self, abbreviation):
"""
Return a specified team.
Expand Down Expand Up @@ -1246,17 +1272,13 @@ def __call__(self, abbreviation):
"""
return self.__getitem__(abbreviation)

def __repr__(self):
"""Returns a ``list`` of all MLB teams for the given season."""
return self._teams

def __iter__(self):
"""Returns an iterator of all of the MLB teams for a given season."""
return iter(self.__repr__())
return iter(self._teams)

def __len__(self):
"""Returns the number of MLB teams for a given season."""
return len(self.__repr__())
return len(self._teams)

def _instantiate_teams(self, team_data_dict, year):
"""
Expand Down
25 changes: 25 additions & 0 deletions sportsreference/nba/boxscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,19 @@ def __init__(self, uri):

self._parse_game_data(uri)

def __str__(self):
"""
Return the string representation of the class.
"""
return (f'Boxscore for {self._away_name.text()} at '
f'{self._home_name.text()} ({self.date})')

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _retrieve_html_page(self, uri):
"""
Download the requested HTML page.
Expand Down Expand Up @@ -1536,6 +1549,18 @@ def __init__(self, date, end_date=None):

self._find_games(date, end_date)

def __str__(self):
"""
Return the string representation of the class.
"""
return f"NBA games for {', '.join(self._boxscores.keys())}"

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

@property
def games(self):
"""
Expand Down
26 changes: 26 additions & 0 deletions sportsreference/nba/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ def __init__(self, player_id):
self._find_initial_index()
AbstractPlayer.__init__(self, player_id, self._name, player_data)

def __str__(self):
"""
Return the string representation of the class.
"""
return f'{self.name} ({self.player_id})'

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _build_url(self):
"""
Create the player's URL to pull stats from.
Expand Down Expand Up @@ -1379,6 +1391,20 @@ def __init__(self, team, year=None, slim=False):

self._find_players(year)

def __str__(self):
"""
Return the string representation of the class.
"""
players = [f'{player.name} ({player.player_id})'.strip()
for player in self._players]
return '\n'.join(players)

def __repr__(self):
"""
Return the string representation of the class.
"""
return self.__str__()

def _pull_team_page(self, url):
"""
Download the team page.
Expand Down
Loading

0 comments on commit 7f0aec9

Please sign in to comment.