Package nflgame :: Module player
[frames] | no frames]

Source Code for Module nflgame.player

  1  import json 
  2  import os.path 
  3   
  4  from nflgame import OrderedDict 
  5  import nflgame.seq 
  6  import nflgame.statmap 
7 8 9 -def _create_players(jsonf=None):
10 """ 11 Creates a dict of Player objects from the players.json file, keyed 12 by GSIS ids. 13 """ 14 if jsonf is None: 15 jsonf = os.path.join(os.path.split(__file__)[0], 'players.json') 16 data = json.loads(open(jsonf).read()) 17 18 players = {} 19 for playerid in data: 20 players[playerid] = Player(data[playerid]) 21 return players
22
23 24 -class Player (object):
25 """ 26 Player instances represent meta information about a single player. 27 This information includes name, team, position, status, height, 28 weight, college, jersey number, birth date, years, pro, etc. 29 30 Player information is populated from NFL.com profile pages. 31 """
32 - def __init__(self, data):
33 self.playerid = data['gsisid'] 34 self.name = data['name'] 35 self.team = data['team'] 36 self.position = data['position'] 37 self.profile_url = data['profile_url'] 38 self.number = data['number'] 39 self.status = data['status'] 40 self.weight = data['weight'] 41 self.height = data['height'] 42 self.college = data['college'] 43 self.years_pro = data['years_pro'] 44 self.birthdate = data['birthdate']
45
46 - def stats(self, year, week=None):
47 games = nflgame.games(year, week) 48 players = nflgame.combine(games).filter(playerid=self.playerid) 49 return list(players)[0]
50
51 - def plays(self, year, week=None):
52 plays = [] 53 games = nflgame.games(year, week) 54 for g in games: 55 plays += filter(lambda p: p.has_player(self.playerid), 56 list(g.drives.plays())) 57 return nflgame.seq.GenPlays(plays)
58
59 - def __str__(self):
60 return '%s (%s, %s)' % (self.name, self.position, self.team)
61
62 63 -class PlayerDefense (Player):
64 - def __init__(self, team):
65 self.playerid = None 66 self.name = team 67 self.team = team 68 self.position = 'DEF'
69
70 - def stats(self, year, week=None):
71 assert False, 'Cannot be called on a defense.'
72
73 - def plays(self, year, week=None):
74 assert False, 'Cannot be called on a defense.'
75
76 - def __str__(self):
77 return '%s Defense' % self.team
78
79 80 -class PlayerStats (object):
81 """ 82 Player represents a single player and all of his statistical categories. 83 Every player has 'playerid', 'name' and 'home' fields. 84 Additionally, depending upon which statistical categories that player 85 was involved in for the game, he'll have properties such as 'passing_tds', 86 'rushing_yds', 'defense_int' and 'kicking_fgm'. 87 88 In order to know whether a paricular player belongs to a statical category, 89 you may use the filtering methods of a player sequence or alternatively, 90 use the has_cat method with arguments like 'passing', 'rushing', 'kicking', 91 etc. (A player sequence in this case would be an instance of 92 GenPlayerStats.) 93 94 You may also inspect whether a player has a certain property by using 95 the special __dict__ attribute. For example:: 96 97 if 'passing_yds' in player.__dict__: 98 # Do something with player.passing_yds 99 """
100 - def __init__(self, playerid, name, home):
101 """ 102 Create a new Player instance with the player id (from NFL.com's 103 GameCenter), the player's name (e.g., "T.Brady") and whether the 104 player is playing in a home game or not. 105 """ 106 self.playerid = playerid 107 self.player = None 108 self.name = name 109 self.home = home 110 self._stats = OrderedDict() 111 if self.playerid in nflgame.players: 112 self.player = nflgame.players[self.playerid]
113
114 - def has_cat(self, cat):
115 return self.__dict__.get(cat, False)
116
117 - def __refresh_categories(self):
118 for cat in nflgame.statmap.categories: 119 for f in self.__dict__: 120 if f.startswith(cat): 121 self.__dict__[cat] = True 122 break
123 124 @property
125 - def tds(self):
126 """ 127 Returns the total number of touchdowns credited to this player across 128 all statistical categories. 129 """ 130 n = 0 131 for f, v in self.__dict__.iteritems(): 132 if f.endswith('tds'): 133 n += v 134 return n
135 136 @property
137 - def twopta(self):
138 """ 139 Returns the total number of two point conversion attempts for 140 the passing, rushing and receiving categories. 141 """ 142 return (self.passing_twopta 143 + self.rushing_twopta 144 + self.receiving_twopta)
145 146 @property
147 - def twoptm(self):
148 """ 149 Returns the total number of two point conversions for 150 the passing, rushing and receiving categories. 151 """ 152 return (self.passing_twoptm 153 + self.rushing_twoptm 154 + self.receiving_twoptm)
155 156 @property
157 - def twoptmissed(self):
158 """ 159 Returns the total number of two point conversion failures for 160 the passing, rushing and receiving categories. 161 """ 162 return (self.passing_twoptmissed 163 + self.rushing_twoptmissed 164 + self.receiving_twoptmissed)
165 166 @property
167 - def stats(self):
168 """ 169 Returns a dict of all stats for the player. 170 """ 171 return self._stats
172
173 - def formatted_stats(self):
174 """ 175 Returns a roughly-formatted string of all statistics for this player. 176 """ 177 s = [] 178 for stat, val in self._stats.iteritems(): 179 s.append('%s: %s' % (stat, val)) 180 return ', '.join(s)
181
182 - def _add_stats(self, stats):
183 for k, v in stats.iteritems(): 184 self.__dict__[k] = self.__dict__.get(k, 0) + v 185 self._stats[k] = self.__dict__[k] 186 self.__refresh_categories()
187
188 - def __str__(self):
189 """ 190 Simply returns the player's name, e.g., "T.Brady". 191 """ 192 return self.name
193
194 - def __add__(self, other):
195 """ 196 Adds two players together. Only two player objects that correspond 197 to the same human (i.e., GameCenter identifier) can be added together. 198 199 If two different players are added together, an assertion will 200 be raised. 201 202 The effect of adding two player objects simply corresponds to the 203 sums of all statistical values. 204 205 Note that as soon as two players have been added, the 'home' property 206 becomes undefined. 207 """ 208 assert self.playerid == other.playerid 209 assert type(self) == type(other) 210 211 new_player = self.__class__(self.playerid, self.name, None) 212 new_player._add_stats(self._stats) 213 new_player._add_stats(other._stats) 214 215 return new_player
216
217 - def __sub__(self, other):
218 assert self.playerid == other.playerid 219 assert type(self) == type(other) 220 221 new_player = GamePlayerStats(self.playerid, self.name, self.home) 222 new_player._add_stats(self._stats) 223 for bk, bv in other._stats.iteritems(): 224 if bk not in new_player._stats: # stat was taken away? ignore. 225 continue 226 227 new_player._stats[bk] -= bv 228 if new_player._stats[bk] == 0: 229 del new_player._stats[bk] 230 else: 231 new_player.__dict__[bk] = new_player._stats[bk] 232 233 anydiffs = False 234 for k, v in new_player._stats.iteritems(): 235 if v > 0: 236 anydiffs = True 237 break 238 if not anydiffs: 239 return None 240 return new_player
241
242 - def __getattr__(self, name):
243 # If name has one of the categories as a prefix, then return 244 # a default value of zero 245 for cat in nflgame.statmap.categories: 246 if name.startswith(cat): 247 return 0 248 print name 249 raise AttributeError
250
251 252 -class GamePlayerStats (PlayerStats):
253 - def __init__(self, playerid, name, home):
254 super(GamePlayerStats, self).__init__(playerid, name, home) 255 self.games = 1
256
257 - def __add__(self, other):
258 new_player = super(GamePlayerStats, self).__add__(other) 259 new_player.games = self.games + other.games 260 return new_player
261
262 263 -class PlayPlayerStats (PlayerStats):
264 pass
265