Skip to content

Commit

Permalink
Add coach to Roster
Browse files Browse the repository at this point in the history
The coach or manager's full name is now a property that can be retrieved
from a team's Roster class.
  • Loading branch information
l3str4nge authored and roclark committed Nov 13, 2020
1 parent 8383487 commit b47b34b
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 13 deletions.
1 change: 1 addition & 0 deletions sportsreference/mlb/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@
}

PLAYER_SCHEME = {
'summary': '[data-template="Partials/Teams/Summary"]',
'season': 'th[data-stat="year_ID"]',
'name': 'h1[itemprop="name"]',
'team_abbreviation': 'td[data-stat="team_ID"]',
Expand Down
36 changes: 34 additions & 2 deletions sportsreference/mlb/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,12 +1450,13 @@ class Roster:
def __init__(self, team, year=None, slim=False):
self._team = team
self._slim = slim
self._coach = None
if slim:
self._players = {}
else:
self._players = []

self._find_players(year)
self._find_players_with_coach(year)

def __str__(self):
"""
Expand Down Expand Up @@ -1555,7 +1556,29 @@ def _get_name(self, player):
name_tag = player('td[data-stat="player"] a')
return name_tag.text()

def _find_players(self, year):
def _parse_coach(self, page):
"""
Parse the team's coach.
Given a copy of the team's roster page, find and parse the team's
coach from the team summary.
Parameters
----------
page : PyQuery object
A PyQuery object representing the team's roster page.
Returns
-------
string
Returns a string of the coach's name.
"""
for line in page(PLAYER_SCHEME['summary']).find('p').items():
strong = line.find('strong')
if hasattr(strong, 'text') and strong.text().strip() == 'Manager:':
return line.find('a').text()

def _find_players_with_coach(self, year):
"""
Find all player IDs for the requested team.
Expand Down Expand Up @@ -1614,6 +1637,8 @@ def _find_players(self, year):
player_instance = Player(player_id)
self._players.append(player_instance)

self._coach = self._parse_coach(page)

@property
def players(self):
"""
Expand All @@ -1624,3 +1649,10 @@ def players(self):
first and last name as listed on the roster page.
"""
return self._players

@property
def coach(self):
"""
Returns a ``string`` of the coach's name, such as 'AJ Hinch'.
"""
return self._coach
1 change: 1 addition & 0 deletions sportsreference/nba/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
}

PLAYER_SCHEME = {
'summary': '[data-template="Partials/Teams/Summary"]',
'season': 'th[data-stat="season"]:first',
'name': 'h1',
'team_abbreviation': 'td[data-stat="team_id"]',
Expand Down
1 change: 1 addition & 0 deletions sportsreference/nba/nba_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def _retrieve_all_teams(year, season_file=None):
doc = utils._pull_page(SEASON_PAGE_URL % year, season_file)
teams_list = utils._get_stats_table(doc, 'div#all_team-stats-base')
opp_teams_list = utils._get_stats_table(doc, 'div#all_opponent-stats-base')

if not teams_list and not opp_teams_list:
utils._no_data_found()
return None, None
Expand Down
37 changes: 34 additions & 3 deletions sportsreference/nba/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,12 +1384,12 @@ class Roster:
def __init__(self, team, year=None, slim=False):
self._team = team
self._slim = slim
self._coach = None
if slim:
self._players = {}
else:
self._players = []

self._find_players(year)
self._find_players_with_coach(year)

def __str__(self):
"""
Expand Down Expand Up @@ -1489,7 +1489,29 @@ def _get_name(self, player):
name_tag = player('td[data-stat="player"] a')
return name_tag.text()

def _find_players(self, year):
def _parse_coach(self, page):
"""
Parse the team's coach.
Given a copy of the team's roster page, find and parse the team's
coach from the team summary.
Parameters
----------
page : PyQuery object
A PyQuery object representing the team's roster page.
Returns
-------
string
Returns a string of the coach's name.
"""
for line in page(PLAYER_SCHEME['summary']).find('p').items():
strong = line.find('strong')
if hasattr(strong, 'text') and strong.text().strip() == 'Coach:':
return line.find('a').text()

def _find_players_with_coach(self, year):
"""
Find all player IDs for the requested team.
Expand Down Expand Up @@ -1541,6 +1563,8 @@ def _find_players(self, year):
player_instance = Player(player_id)
self._players.append(player_instance)

self._coach = self._parse_coach(page)

@property
def players(self):
"""
Expand All @@ -1551,3 +1575,10 @@ def players(self):
first and last name as listed on the roster page.
"""
return self._players

@property
def coach(self):
"""
Returns a ``string`` of the coach's name, such as "Mike D'Antoni".
"""
return self._coach
1 change: 1 addition & 0 deletions sportsreference/ncaab/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
}

PLAYER_SCHEME = {
'summary': '[data-template="Partials/Teams/Summary"]',
'conference': 'td[data-stat="conf_abbr"]',
'season': 'th[data-stat="season"]:first',
'name': 'h1',
Expand Down
36 changes: 34 additions & 2 deletions sportsreference/ncaab/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,12 +656,13 @@ class Roster:
def __init__(self, team, year=None, slim=False):
self._team = team
self._slim = slim
self._coach = None
if slim:
self._players = {}
else:
self._players = []

self._find_players(year)
self._find_players_with_coach(year)

def __str__(self):
"""
Expand Down Expand Up @@ -761,7 +762,29 @@ def _get_name(self, player):
name_tag = player('th[data-stat="player"] a')
return name_tag.text()

def _find_players(self, year):
def _parse_coach(self, page):
"""
Parse the team's coach.
Given a copy of the team's roster page, find and parse the team's
coach from the team summary.
Parameters
----------
page : PyQuery object
A PyQuery object representing the team's roster page.
Returns
-------
string
Returns a string of the coach's name.
"""
for line in page(PLAYER_SCHEME['summary']).find('p').items():
strong = line.find('strong')
if hasattr(strong, 'text') and strong.text().strip() == 'Coach:':
return line.find('a').text()

def _find_players_with_coach(self, year):
"""
Find all player IDs for the requested team.
Expand Down Expand Up @@ -802,6 +825,8 @@ def _find_players(self, year):
player_instance = Player(player_id)
self._players.append(player_instance)

self._coach = self._parse_coach(page)

@property
def players(self):
"""
Expand All @@ -812,3 +837,10 @@ def players(self):
first and last name as listed on the roster page.
"""
return self._players

@property
def coach(self):
"""
Returns a ``string`` of the coach's name, such as 'Matt Painter'.
"""
return self._coach
1 change: 1 addition & 0 deletions sportsreference/ncaaf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
}

PLAYER_SCHEME = {
'summary': '[data-template="Partials/Teams/Summary"]',
'season': 'th[data-stat="year_id"]',
'name': 'h1[itemprop="name"]',
'team_abbreviation': 'td[data-stat="school_name"]',
Expand Down
36 changes: 34 additions & 2 deletions sportsreference/ncaaf/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,12 +883,13 @@ class Roster:
def __init__(self, team, year=None, slim=False):
self._team = team
self._slim = slim
self._coach = None
if slim:
self._players = {}
else:
self._players = []

self._find_players(year)
self._find_players_with_coach(year)

def __str__(self):
"""
Expand Down Expand Up @@ -988,7 +989,29 @@ def _get_name(self, player):
name_tag = player('th[data-stat="player"] a')
return name_tag.text()

def _find_players(self, year):
def _parse_coach(self, page):
"""
Parse the team's coach.
Given a copy of the team's roster page, find and parse the team's
coach from the team summary.
Parameters
----------
page : PyQuery object
A PyQuery object representing the team's roster page.
Returns
-------
string
Returns a string of the coach's name.
"""
for line in page(PLAYER_SCHEME['summary']).find('p').items():
strong = line.find('strong')
if hasattr(strong, 'text') and strong.text().strip() == 'Coach:':
return line.find('a').text()

def _find_players_with_coach(self, year):
"""
Find all player IDs for the requested team.
Expand Down Expand Up @@ -1028,6 +1051,8 @@ def _find_players(self, year):
player_instance = Player(player_id)
self._players.append(player_instance)

self._coach = self._parse_coach(page)

@property
def players(self):
"""
Expand All @@ -1038,3 +1063,10 @@ def players(self):
first and last name as listed on the roster page.
"""
return self._players

@property
def coach(self):
"""
Returns a ``string`` of the coach's name, such as 'Jeff Brohm'.
"""
return self._coach
1 change: 1 addition & 0 deletions sportsreference/nfl/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
}

PLAYER_SCHEME = {
'summary': '[data-template="Partials/Teams/Summary"]',
'season': 'th[data-stat="year_id"]',
'name': 'h1[itemprop="name"]',
'team_abbreviation': 'td[data-stat="team"]',
Expand Down
36 changes: 34 additions & 2 deletions sportsreference/nfl/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,12 +1708,13 @@ class Roster:
def __init__(self, team, year=None, slim=False):
self._team = team
self._slim = slim
self._coach = None
if slim:
self._players = {}
else:
self._players = []

self._find_players(year)
self._find_players_with_coach(year)

def __str__(self):
"""
Expand Down Expand Up @@ -1813,7 +1814,29 @@ def _get_name(self, player):
name_tag = player('td[data-stat="player"] a')
return name_tag.text()

def _find_players(self, year):
def _parse_coach(self, page):
"""
Parse the team's coach.
Given a copy of the team's roster page, find and parse the team's
coach from the team summary.
Parameters
----------
page : PyQuery object
A PyQuery object representing the team's roster page.
Returns
-------
string
Returns a string of the coach's name.
"""
for line in page(PLAYER_SCHEME['summary']).find('p').items():
strong = line.find('strong')
if hasattr(strong, 'text') and strong.text().strip() == 'Coach:':
return line.find('a').text()

def _find_players_with_coach(self, year):
"""
Find all player IDs for the requested team.
Expand Down Expand Up @@ -1853,6 +1876,8 @@ def _find_players(self, year):
player_instance = Player(player_id)
self._players.append(player_instance)

self._coach = self._parse_coach(page)

@property
def players(self):
"""
Expand All @@ -1863,3 +1888,10 @@ def players(self):
first and last name as listed on the roster page.
"""
return self._players

@property
def coach(self):
"""
Returns a ``string`` of the coach's name, such as 'Sean Payton'.
"""
return self._coach
1 change: 1 addition & 0 deletions sportsreference/nhl/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
}

PLAYER_SCHEME = {
'summary': '[data-template="Partials/Teams/Summary"]',
'season': 'th[data-stat="season"]',
'name': 'h1[itemprop="name"]',
'team_abbreviation': 'td[data-stat="team_id"]',
Expand Down
Loading

0 comments on commit b47b34b

Please sign in to comment.