Python Module for NBA Player Statistics Calculator
Budget: $30 – $250 USD
Project Overview
Create a Python module that calculates NBA player statistics using the balldontlie API. The module should fetch and process player game statistics across multiple seasons, providing both rolling averages and game-by-game totals in specific formats.
Key Requirements
Data Source
Use the balldontlie API (https://www.balldontlie.io/api/v1)
API endpoints to be used:
/players (for player search)
/stats (for game statistics)
Core Functionality
Player Identification:
- Accept player name as input
- Convert player name to player_id using the API
- Handle cases where player isn't found
Game Data Collection
- Fetch game statistics sorted by most recent first
- Only include games where the player logged minutes played (min > 0)
- Pull data across multiple seasons if needed
- Must collect enough games with minutes played to satisfy the maximum requirement (30 games)
- Handle missing/null values in the data
Statistical Calculations
a. Rolling Averages (Last 10 Games):
Minutes played (player_minutes_avg_L10)
Points (player_points_avg_L10)
Field Goals:
Made (player_fg_made_avg_L10)
Attempted (player_fg_attempted_avg_L10)
Percentage (player_fg_pct_avg_L10)
Free Throws:
Made (player_ft_made_avg_L10)
Attempted (player_ft_attempted_avg_L10)
Percentage (player_ft_pct_avg_L10)
Three Pointers:
Made (player_3p_made_avg_L10)
Attempted (player_3p_attempted_avg_L10)
Percentage (player_3p_pct_avg_L10)
Rebounds:
Offensive (player_offensive_rebounds_avg_L10)
Defensive (player_defensive_rebounds_avg_L10)
Total (player_total_rebounds_avg_L10)
Assists (player_assists_avg_L10)
Turnovers (player_turnovers_avg_L10)
b. Game-by-Game Totals:
Values must be comma-separated strings in chronological order (most recent first)
Three Pointers:
Last 5 games (player_3p_totals_L5)
Last 10 games (player_3p_totals_L10)
Last 20 games (player_3p_totals_L20)
Last 30 games (player_3p_totals_L30)
Assists:
Last 5 games (player_assist_totals_L5)
Last 10 games (player_assist_totals_L10)
Last 20 games (player_assist_totals_L20)
Last 30 games (player_assist_totals_L30)
Points:
Last 5 games (player_point_totals_L5)
Last 10 games (player_point_totals_L10)
Last 20 games (player_point_totals_L20)
Last 30 games (player_point_totals_L30)
Rebounds:
Last 5 games (player_rebound_totals_L5)
Last 10 games (player_rebound_totals_L10)
Last 20 games (player_rebound_totals_L20)
Last 30 games (player_rebound_totals_L30)
Output Format
Averages Output Format
pythonCopy{
"player_minutes_avg_L10": 32.5,
"player_points_avg_L10": 24.5,
"player_fg_made_avg_L10": 9.2,
# ... (other averages as specified)
"games_counted": 10
}
Totals Output Format
pythonCopy{
"player_3p_totals_L5": "2,3,1,2,4",
"player_3p_totals_L10": "2,3,1,2,4,2,1,3,2,1",
"player_assist_totals_L5": "8,6,5,7,9",
"player_assist_totals_L10": "8,6,5,7,9,6,8,7,6,8",
# ... and so on for L20 and L30
}
Technical Requirements
Written in Python 3.7+
Use type hints for better code maintenance
Include error handling for API failures
Handle rate limiting from the API
Include documentation and example usage
Must work across season boundaries (e.g., if requesting 30 games and only 20 are available in current season, pull 10 from previous season)
Dependencies
requests library for API calls
Example Implementation
A reference implementation has been provided in the attached code. Key classes and methods:
pythonCopyclass NBAStatsCalculator:
def __init__(self, api_key: str = None)
def get_player_id(self, player_name: str) -> int
def get_player_stats(self, player_id: int, num_games: int) -> List[Dict]
def calculate_averages(self, stats: List[Dict], num_games: int) -> Dict[str, float]
def calculate_totals(self, stats: List[Dict], game_increments: List[int]) -> Dict[str, Dict[str, str]]
def get_player_analysis(self, player_name: str) -> Dict[str, Union[Dict[str, float], Dict[str, Dict[str, str]]]]
Testing Requirements
Test with various player names including those with special characters
Test with players who have played fewer games than requested
Test with players who span multiple seasons
Test filtering of games where player did not log minutes
Test handling of DNP (Did Not Play) games
Test handling of missing/null values in game data
Test error handling for API failures
Deliverables
Python module implementing the specified functionality
Documentation including:
Installation instructions
Usage examples
Function documentation
Basic test suite demonstrating functionality
Requirements.txt file
Create a Python module that calculates NBA player statistics using the balldontlie API. The module should fetch and process player game statistics across multiple seasons, providing both rolling averages and game-by-game totals in specific formats.
Key Requirements
Data Source
Use the balldontlie API (https://www.balldontlie.io/api/v1)
API endpoints to be used:
/players (for player search)
/stats (for game statistics)
Core Functionality
Player Identification:
- Accept player name as input
- Convert player name to player_id using the API
- Handle cases where player isn't found
Game Data Collection
- Fetch game statistics sorted by most recent first
- Only include games where the player logged minutes played (min > 0)
- Pull data across multiple seasons if needed
- Must collect enough games with minutes played to satisfy the maximum requirement (30 games)
- Handle missing/null values in the data
Statistical Calculations
a. Rolling Averages (Last 10 Games):
Minutes played (player_minutes_avg_L10)
Points (player_points_avg_L10)
Field Goals:
Made (player_fg_made_avg_L10)
Attempted (player_fg_attempted_avg_L10)
Percentage (player_fg_pct_avg_L10)
Free Throws:
Made (player_ft_made_avg_L10)
Attempted (player_ft_attempted_avg_L10)
Percentage (player_ft_pct_avg_L10)
Three Pointers:
Made (player_3p_made_avg_L10)
Attempted (player_3p_attempted_avg_L10)
Percentage (player_3p_pct_avg_L10)
Rebounds:
Offensive (player_offensive_rebounds_avg_L10)
Defensive (player_defensive_rebounds_avg_L10)
Total (player_total_rebounds_avg_L10)
Assists (player_assists_avg_L10)
Turnovers (player_turnovers_avg_L10)
b. Game-by-Game Totals:
Values must be comma-separated strings in chronological order (most recent first)
Three Pointers:
Last 5 games (player_3p_totals_L5)
Last 10 games (player_3p_totals_L10)
Last 20 games (player_3p_totals_L20)
Last 30 games (player_3p_totals_L30)
Assists:
Last 5 games (player_assist_totals_L5)
Last 10 games (player_assist_totals_L10)
Last 20 games (player_assist_totals_L20)
Last 30 games (player_assist_totals_L30)
Points:
Last 5 games (player_point_totals_L5)
Last 10 games (player_point_totals_L10)
Last 20 games (player_point_totals_L20)
Last 30 games (player_point_totals_L30)
Rebounds:
Last 5 games (player_rebound_totals_L5)
Last 10 games (player_rebound_totals_L10)
Last 20 games (player_rebound_totals_L20)
Last 30 games (player_rebound_totals_L30)
Output Format
Averages Output Format
pythonCopy{
"player_minutes_avg_L10": 32.5,
"player_points_avg_L10": 24.5,
"player_fg_made_avg_L10": 9.2,
# ... (other averages as specified)
"games_counted": 10
}
Totals Output Format
pythonCopy{
"player_3p_totals_L5": "2,3,1,2,4",
"player_3p_totals_L10": "2,3,1,2,4,2,1,3,2,1",
"player_assist_totals_L5": "8,6,5,7,9",
"player_assist_totals_L10": "8,6,5,7,9,6,8,7,6,8",
# ... and so on for L20 and L30
}
Technical Requirements
Written in Python 3.7+
Use type hints for better code maintenance
Include error handling for API failures
Handle rate limiting from the API
Include documentation and example usage
Must work across season boundaries (e.g., if requesting 30 games and only 20 are available in current season, pull 10 from previous season)
Dependencies
requests library for API calls
Example Implementation
A reference implementation has been provided in the attached code. Key classes and methods:
pythonCopyclass NBAStatsCalculator:
def __init__(self, api_key: str = None)
def get_player_id(self, player_name: str) -> int
def get_player_stats(self, player_id: int, num_games: int) -> List[Dict]
def calculate_averages(self, stats: List[Dict], num_games: int) -> Dict[str, float]
def calculate_totals(self, stats: List[Dict], game_increments: List[int]) -> Dict[str, Dict[str, str]]
def get_player_analysis(self, player_name: str) -> Dict[str, Union[Dict[str, float], Dict[str, Dict[str, str]]]]
Testing Requirements
Test with various player names including those with special characters
Test with players who have played fewer games than requested
Test with players who span multiple seasons
Test filtering of games where player did not log minutes
Test handling of DNP (Did Not Play) games
Test handling of missing/null values in game data
Test error handling for API failures
Deliverables
Python module implementing the specified functionality
Documentation including:
Installation instructions
Usage examples
Function documentation
Basic test suite demonstrating functionality
Requirements.txt file