-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable NBA Team to be directly accessible
Instead of requiring users to go through the Teams class to get a specific team, the NBA modules now enable a specific team to be directly queried by using the Team class. This reduces computational complexity by removing the need to instantiate every team while also making it more intuitive for users. Signed-Off-By: Robert Clark <robdclark@outlook.com>
- Loading branch information
Showing
4 changed files
with
158 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from .constants import PARSING_SCHEME, SEASON_PAGE_URL | ||
from pyquery import PyQuery as pq | ||
from sportsreference import utils | ||
|
||
|
||
def _add_stats_data(teams_list, team_data_dict): | ||
""" | ||
Add a team's stats row to a dictionary. | ||
Pass table contents and a stats dictionary of all teams to accumulate all | ||
stats for each team in a single variable. | ||
Parameters | ||
---------- | ||
teams_list : generator | ||
A generator of all row items in a given table. | ||
team_data_dict : {str: {'data': str, 'rank': int}} dictionary | ||
A dictionary where every key is the team's abbreviation and every value | ||
is another dictionary with a 'data' key which contains the string | ||
version of the row data for the matched team, and a 'rank' key which is | ||
the rank of the team. | ||
Returns | ||
------- | ||
dictionary | ||
An updated version of the team_data_dict with the passed table row | ||
information included. | ||
""" | ||
# Teams are listed in terms of rank with the first team being #1 | ||
rank = 1 | ||
for team_data in teams_list: | ||
abbr = utils._parse_field(PARSING_SCHEME, team_data, 'abbreviation') | ||
try: | ||
team_data_dict[abbr]['data'] += team_data | ||
except KeyError: | ||
team_data_dict[abbr] = {'data': team_data, 'rank': rank} | ||
rank += 1 | ||
return team_data_dict | ||
|
||
|
||
def _retrieve_all_teams(year): | ||
""" | ||
Find and create Team instances for all teams in the given season. | ||
For a given season, parses the specified NBA stats table and finds all | ||
requested stats. Each team then has a Team instance created which includes | ||
all requested stats and a few identifiers, such as the team's name and | ||
abbreviation. All of the individual Team instances are added to a list. | ||
Parameters | ||
---------- | ||
year : string | ||
The requested year to pull stats from. | ||
Returns | ||
------- | ||
tuple | ||
Returns a ``tuple`` of the team_data_dict and year which represent all | ||
stats for all teams, and the given year that should be used to pull | ||
stats from, respectively. | ||
""" | ||
team_data_dict = {} | ||
|
||
if not year: | ||
year = utils._find_year_for_season('nba') | ||
# If stats for the requested season do not exist yet (as is the case | ||
# right before a new season begins), attempt to pull the previous | ||
# year's stats. If it exists, use the previous year instead. | ||
if not utils._url_exists(SEASON_PAGE_URL % year) and \ | ||
utils._url_exists(SEASON_PAGE_URL % str(int(year) - 1)): | ||
year = str(int(year) - 1) | ||
doc = pq(SEASON_PAGE_URL % year) | ||
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 | ||
for stats_list in [teams_list, opp_teams_list]: | ||
team_data_dict = _add_stats_data(stats_list, team_data_dict) | ||
return team_data_dict, year |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters