Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add yearly nine_cat_averages to Player #447

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions espn_api/basketball/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,15 @@
'WAIVER': 180,
'TRADED': 244
}

NINE_CAT_STATS = {
'3PTM',
'AST',
'BLK',
'FG%',
'FT%',
'PTS',
'REB',
'STL',
'TO'
}
16 changes: 13 additions & 3 deletions espn_api/basketball/player.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from .constant import POSITION_MAP, PRO_TEAM_MAP, STATS_MAP, STAT_ID_MAP
from .constant import NINE_CAT_STATS, POSITION_MAP, PRO_TEAM_MAP, STATS_MAP, STAT_ID_MAP
from espn_api.utils.utils import json_parsing
from datetime import datetime
from functools import cached_property

class Player(object):
'''Player are part of team'''
def __init__(self, data, year, pro_team_schedule = None):
self.name = json_parsing(data, 'fullName')
self.playerId = json_parsing(data, 'id')
self.year = year
self.position = POSITION_MAP[json_parsing(data, 'defaultPositionId') - 1]
self.lineupSlot = POSITION_MAP.get(data.get('lineupSlotId'), '')
self.eligibleSlots = [POSITION_MAP[pos] for pos in json_parsing(data, 'eligibleSlots')]
Expand Down Expand Up @@ -50,10 +52,18 @@ def __init__(self, data, year, pro_team_schedule = None):
self.avg_points = self.stats.get(f'{year}_total', {}).get('applied_avg', 0)
self.projected_total_points= self.stats.get(f'{year}_projected', {}).get('applied_total', 0)
self.projected_avg_points = self.stats.get(f'{year}_projected', {}).get('applied_avg', 0)

def __repr__(self):
return f'Player({self.name})'

def _stat_id_pretty(self, id: str, scoring_period):
id_type = STAT_ID_MAP.get(id[:2])
return f'{id[2:]}_{id_type}' if id_type else str(scoring_period)

@cached_property
def nine_cat_averages(self):
return {
k: round(v, (3 if k in {'FG%', 'FT%'} else 1))
for k, v in self.stats.get(f'{self.year}_total', {}).get("avg", {}).items()
if k in NINE_CAT_STATS
}
23 changes: 18 additions & 5 deletions tests/basketball/integration/test_league.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class LeagueTest(TestCase):
def setUp(self):
self.league = League(411647, 2019)

def test_league_init(self):

self.assertEqual(self.league.scoringPeriodId, 178)
Expand All @@ -27,7 +27,7 @@ def test_league_draft(self):
self.assertEqual(draft[1].round_num, 1)
self.assertEqual(draft[2].round_pick, 3)
self.assertEqual(draft[2].team.team_name, 'Denver Nuggets ')

def test_league_free_agents(self):
free_agents = self.league.free_agents()

Expand All @@ -41,6 +41,19 @@ def test_player_info(self):
self.assertEqual(player.schedule['2']['team'], 'BKN')
self.assertEqual(player.stats['2']['team'], 'BKN')
self.assertEqual(player.stats['2']['total']['PTS'], 24.0)
self.assertEqual(player.nine_cat_averages,
{
'PTS': 17.3,
'BLK': 1.7,
'STL': 1.7,
'AST': 1.4,
'REB': 15.6,
'TO': 2.2,
'3PTM': 0.1,
'FG%': 0.533,
'FT%': 0.59
}
)

def test_league_box_scores(self):
final_matchup = self.league.box_scores()[0]
Expand All @@ -56,7 +69,7 @@ def test_league_box_scores(self):

self.assertEqual(scoring_period_matchup.home_score, 234.0)
self.assertEqual(scoring_period_matchup.away_lineup[0].points, 0)

def test_league_box_scores_category(self):
league = League(1631984064, 2023)

Expand All @@ -69,7 +82,7 @@ def test_league_box_scores_category(self):

def test_past_league(self):
league = League(411647, 2017)

self.assertEqual(league.scoringPeriodId, 170)

def test_past_league_scoreboard(self):
Expand All @@ -78,7 +91,7 @@ def test_past_league_scoreboard(self):

self.assertTrue(scores[0].home_final_score > 0)
self.assertTrue(scores[0].away_final_score > 0)

def test_blank_league_init(self):
blank_league = League(411647, 2019, fetch_league=False)
self.assertEqual(len(blank_league.teams), 0)