Skip to content

Commit 282ec04

Browse files
committed
FIX: player_stats() not iterating through stat groups when multiple positions were played
1 parent e12ad1b commit 282ec04

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ use `statsapi.get()` to call the sports_players endpoint for the 2008 World Seri
133133
lookup Chase Utley's person id from the results, and pass it into `statsapi.player_stats()`
134134
using `type='hitting'` and `group='career'`
135135

136+
```
136137
print( statsapi.player_stats(next(x['id'] for x in statsapi.get('sports_players',{'season':2008,'gameType':'W'})['people'] if x['fullName']=='Chase Utley'), 'hitting', 'career') )
138+
```
137139

138140
### Print a list of scoring plays from the 4/28/2019 Marlins @ Phillies game
139141

statsapi/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -855,12 +855,13 @@ def player_stats(personId,group='[hitting,pitching,fielding]',type='season'):
855855
}
856856

857857
for s in r['people'][0].get('stats',[]):
858-
stat_group = {
859-
'type' : s['type']['displayName'],
860-
'group' : s['group']['displayName'],
861-
'stats' : s['splits'][0]['stat'] #there should only be one item in the list for career stats
862-
}
863-
stat_groups.append(stat_group)
858+
for i in range(0,len(s['splits'])):
859+
stat_group = {
860+
'type' : s['type']['displayName'],
861+
'group' : s['group']['displayName'],
862+
'stats' : s['splits'][i]['stat']
863+
}
864+
stat_groups.append(stat_group)
864865

865866
if len(stat_groups)==0:
866867
raise ValueError('No stats found for given player, type, and group.')
@@ -872,7 +873,9 @@ def player_stats(personId,group='[hitting,pitching,fielding]',type='season'):
872873
stats += ')\n\n'
873874

874875
for x in stat_groups:
875-
stats += x['type'][0:1].upper() + x['type'][1:] + ' ' + x['group'][0:1].upper() + x['group'][1:] + '\n'
876+
stats += x['type'][0:1].upper() + x['type'][1:] + ' ' + x['group'][0:1].upper() + x['group'][1:]
877+
if x['stats'].get('position'): stats += ' ({})'.format(x['stats']['position']['abbreviation'])
878+
stats += '\n'
876879
for y in x['stats'].keys():
877880
if y=='position': continue
878881
stats += '{}: {}\n'.format(y,x['stats'][y])

0 commit comments

Comments
 (0)