-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathNBA_playerclass copy.py
285 lines (258 loc) · 10.3 KB
/
NBA_playerclass copy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import sys, os
reqargs = ['pts', 'fg', '3pt', 'ft', 'rb']
stats = {'mins':0.0, 'fg':0, 'fga':0, '3pt':0, '3pta':0, 'ft':0,
'fta':0, 'pts':0, 'stl':0, 'ast':0, 'to':0, 'blk':0,
'rbo':0, 'rbd':0, 'rb':0, 'pf':0}
sks = ('mins', 'fg', 'fga', '3pt', '3pta', 'ft', 'fta', 'pts',
'stl', 'ast', 'to', 'blk', 'rbo', 'rbd', 'rb', 'pf')
class player():
def __init__(self, name, team, ID):
self._name = name # ESPN name; may need to be "names"
self._team = team # ESPN team; likely needs to be "teams"
self._ID = ID # Gloabl ID assigned by yours truely
self.curgame = None # keeps track of the current game
self._gamestats = dict() # dict of dict, keys are gameIDs, values are stats for that game
self._games = set() # set of game IDs the player has played in
self.actionfeed = dict() # dict of key->game, value->actions a player has performed
self.seasonIDs = list() # list of ESPN season IDs
self.playergameIDs = list() # list of single-game player IDs
#for stat in statslist:
def _newgame(self, game, playergameID):
'''
Sets the gameID as the current game, and sets the
current stats equal to 0's; also, automatically flushes old stats
if they have not been flushed; initialize new game stats with
the dictionary "stats" to allow for easy referencing when
updating;
'''
if self.curgame not in self._games and self.curgame:
self._flushgame()
self.curgame = game # current game ID (ESPN)
self.stats = stats.copy() # set current stats to NULL stats dict
self.curactfeed = list() # list of (gameID, [actions]) for cur game
self.playergameIDs.append(playergameID)
def _flushgame(self):
'''
Adds the new game ID to the set of games played in, and
moves the current game data to a stats dictionary with
keys as game ids, and entries are dictionaries with stat
handles as keys; resets current game values;
'''
self._games.add(self.curgame)
self._gamestats[self.curgame] = self.stats.copy()
self.actionfeed[self.curgame] = self.curactfeed.copy()
self.curgame = None
self.stats = None
self.curactfeed = list()
def getcur(self, game):
'''
NEED TO UPDATE SINCE "CURRENT" HAS CHANGED
Returns dictionary of current stats, as well as name, id, and
team;
'''
if game == self.curgame:
s = self.stats.copy()
s['Name'] = self._name
s['ID'] = self._ID
s['Team'] = self._team
return s
else:
raise ValueError("no stats for %s for %s" % (self._name, game))
def getgame(self, game, args=None):
'''
Returns dictionary of game stats, as well as name, id, and
team;
'''
if game in self._games:
s = self._gamestats[game].copy()
if not args:
s['Name'] = self._name
s['ID'] = self._ID
s['Team'] = self._team
return s
else:
raise ValueError("no stats for %s for %s" % (self._name, game))
def getgames(self):
'''
Returns dictionary dictionaries of game stats {game:stats},
as well as name, id, and team (once);
'''
s = dict()
g = list()
s['Name'] = self._name
s['ID'] = self._ID
s['Team'] = self._team
for game in self._games:
s[game] = self.getgame(game, args=1)
g.append(game)
s['games'] = g
s['statlist'] = sks
return s
def update(self, name, stat, increment):
'''
Checks to make sure "name" in a valid player and "stat"
is a valid stat; if so, calls updatestat module to increment
"stat" by "increment"; if not, returns a ValueError
'''
if name in playerlist:
if stat in statlist:
playerstats = PlayerStats[name]
playerstats = updatestat(playerstat, stat, increment)
PlayerStats[name] = playerstats
else:
raise ValueError('stat "%s" not a player stat' % stat)
else:
raise ValueError('player "%s" not a player' % name)
def updatestat(self, time, stat, args):
'''
Updates stat "stat" in "playerstats" by "increment; I think
there is a way to handle these together instead of writing out
separate updates for each one...idk
'''
if stat.lower() in stats.keys():
if stat.lower() in reqargs and args==None:
return -1
else:
new_action = None
if stat=='PTS':
self.stats['pts'] += args
elif stat=='FG':
self.stats['fga'] += 1
if args=='Made':
self.stats['fg'] += 1
new_action = ' '.join([args, 'FG'])
elif stat=='3PT':
self.stats['3pta'] += 1
if args=='Made':
self.stats['3pt'] += 1
new_action = ' '.join([args, '3PT'])
elif stat=='FT':
self.stats['fta'] += 1
if args=='Made':
self.stats['ft'] += 1
new_action = ' '.join([args, 'FT'])
elif stat in ['STL','AST','BLK','TO','PF']:
self.stats[stat.lower()] += 1
new_action = stat
elif stat=='RB':
args = args.split()
args = [arg.split(":") for arg in args]
if args[2][0] > self.stats['rbo']:
new_action = 'RBO'
elif args[5][0] > self.stats['rbd']:
new_action = 'RBD'
self.stats['rbo'] = int(args[2][0])
self.stats['rbd'] = int(args[5][0])
self.stats['rb'] = int(args[2][0]) + int(args[5][0])
elif stat=='MINS':
self.stats['mins'] += args
# append line to action feed
if new_action:
self.actionfeed.append([self.curgame, time, new_action])
return 1
else:
return -2
def name(self):
return self._name
def team(self, Year=None):
return self._team
def ID(self):
return self._ID
def games(self):
return self._games
'''
Need to re-write this so that each instance of "game" class corresponds
to a single game, not a group of games;
'''
class game():
def __init__(self, fhandlepbp, fhandlenam):
self._pbpfile = os.path.splitext(os.path.basename(fhandlepbp))[0]
self._plafile = os.path.splitext(os.path.basename(fhandlenam))[0]
self._home = dict()
self._away = dict()
self._teams = dict()
self._score = dict()
self._stats = dict()
self.games = []
def _addgame(self, game, score):
'''
Updates home, away, teams, score for new game "game" with score
"score";
'''
self.games.append(game)
home, away = game[-3:], game[-6:-3]
self._home[game] = home
self._away[game] = away
self._teams[game] = (home, away)
self._score[game] = {home:score[home], away:score[away]}
def _addstats(self, game, pstats, active):
'''
Updates the player stats for game "game"; just creates a list
of dictionaries output from the player class for the current
game; note that the player class returns a ValueError if a
player name referenced in "active" dose not have their current
game set as "game";
'''
self._stats[game] = [pstats[p].getcur(game) for p in active]
def update(self, game, pstats, active, score):
'''
Adds the current game stats to self under heading of "game"
'''
self._addgame(game, score)
self._addstats(game, pstats, active)
def showgame(self, game):
'''
pprint display the stats from game "game"
'''
temp = getgame(self, game)
def getscore(self, game):
return self._score[game]
def getteams(self, game):
return self._teams[game]
def getgame(self, game):
'''
Return a usable format of the stats from game; specifically, return
a dictionary with player names as keys and stats as dicts, key
"teams" with teams as values, keys with each 3-letter team abv. with
scores as values, and keys str(3-letter team abv. + Team) with
the list of players on that team as values;
'''
out = self.getscore(game)
home, away = self.getteams(game)
hplay, aplay = [], []
for p in self._stats[game]:
if p['Team']==home:
hplay.append(p['Name'])
elif p['Team']==away:
aplay.append(p['Name'])
temp = {}
for stat in stats:
temp[stat] = p[stat]
for stat in ['Name', 'ID', 'Team']:
temp[stat] = p[stat]
out[p['Name']] = temp
out['home'], out['away'] = home, away
out[home+'Team'] = hplay
out[away+'Team'] = aplay
return out
def getgames(self):
'''
Returns the stats from all games contained in self as a dictionary
with keys as game ids and values as returns from 'getgame(game id)";
'''
out = dict()
for game in self.games:
out[game] = self.getgame(game)
out['statlist'] = sks
return out
def starters(self):
'''
Returns the list of starts for the game, self; used primarely in
setting up player game IDs;
'''
return
def players(self):
'''
Returns the list of all players (via ID?) that played in game 'self'
'''
return