1 import json
2 import os.path
3
4 from nflgame import OrderedDict
5 import nflgame.seq
6 import nflgame.statmap
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
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 """
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):
50
51 - def plays(self, year, week=None):
58
60 return '%s (%s, %s)' % (self.name, self.position, self.team)
61
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
77 return '%s Defense' % self.team
78
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, team):
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.name = name
108 self.home = home
109 self.team = team
110 self._stats = OrderedDict()
111
112 self.player = None
113 if self.playerid in nflgame.players:
114 self.player = nflgame.players[self.playerid]
115
117 for f in self._stats:
118 if f.startswith(cat):
119 return True
120 return False
121
122 @property
124 """
125 Guesses the position of this player based on the statistical
126 categories present in this object when player meta is not
127 present.
128
129 Note that if this resorts to a guess, then it will be more
130 effective on aggregate data rather than data from just a
131 single play. (e.g., if a QB runs the ball, and that's the
132 only data available, the position returned will be RB.)
133
134 When a position is guessed, only the following positions will
135 be returned: QB, RB, WR, DEF, K and P.
136 """
137
138 if self.player is not None:
139 return self.player.position
140
141 stats = [
142 (self.passing_att, 'QB'),
143 (self.rushing_att, 'RB'),
144 (self.receiving_tar, 'WR'),
145 (self.defense_tkl, 'DEF'),
146 (self.defense_ast, 'DEF'),
147 (self.kicking_tot, 'K'),
148 (self.kicking_fga, 'K'),
149 (self.punting_tot, 'P'),
150 ]
151 return sorted(stats, reverse=True)[0][1]
152
153 @property
155 """
156 Returns the total number of touchdowns credited to this player across
157 all statistical categories.
158 """
159 n = 0
160 for f, v in self.__dict__.iteritems():
161 if f.endswith('tds'):
162 n += v
163 return n
164
165 @property
167 """
168 Returns the total number of two point conversion attempts for
169 the passing, rushing and receiving categories.
170 """
171 return (self.passing_twopta
172 + self.rushing_twopta
173 + self.receiving_twopta)
174
175 @property
177 """
178 Returns the total number of two point conversions for
179 the passing, rushing and receiving categories.
180 """
181 return (self.passing_twoptm
182 + self.rushing_twoptm
183 + self.receiving_twoptm)
184
185 @property
187 """
188 Returns the total number of two point conversion failures for
189 the passing, rushing and receiving categories.
190 """
191 return (self.passing_twoptmissed
192 + self.rushing_twoptmissed
193 + self.receiving_twoptmissed)
194
195 @property
197 """
198 Returns a dict of all stats for the player.
199 """
200 return self._stats
201
210
212 for k, v in stats.iteritems():
213 self.__dict__[k] = self.__dict__.get(k, 0) + v
214 self._stats[k] = self.__dict__[k]
215
217 for k, v in stats.iteritems():
218 self.__dict__[k] = v
219 self._stats[k] = self.__dict__[k]
220
222 """
223 Simply returns the player's name, e.g., "T.Brady".
224 """
225 return self.name
226
228 """
229 Adds two players together. Only two player objects that correspond
230 to the same human (i.e., GameCenter identifier) can be added together.
231
232 If two different players are added together, an assertion will
233 be raised.
234
235 The effect of adding two player objects simply corresponds to the
236 sums of all statistical values.
237
238 Note that as soon as two players have been added, the 'home' property
239 becomes undefined if the two operands have different values of 'home'.
240 """
241 assert self.playerid == other.playerid
242 assert type(self) == type(other)
243
244 if self.home != other.home:
245 home = None
246 else:
247 home = self.home
248 new_player = self.__class__(self.playerid, self.name, home, self.team)
249 new_player._add_stats(self._stats)
250 new_player._add_stats(other._stats)
251
252 return new_player
253
255 assert self.playerid == other.playerid
256 assert type(self) == type(other)
257
258 new_player = GamePlayerStats(self.playerid,
259 self.name, self.home, self.team)
260 new_player._add_stats(self._stats)
261 for bk, bv in other._stats.iteritems():
262 if bk not in new_player._stats:
263 continue
264
265 new_player._stats[bk] -= bv
266 if new_player._stats[bk] == 0:
267 del new_player._stats[bk]
268 else:
269 new_player.__dict__[bk] = new_player._stats[bk]
270
271 anydiffs = False
272 for k, v in new_player._stats.iteritems():
273 if v > 0:
274 anydiffs = True
275 break
276 if not anydiffs:
277 return None
278 return new_player
279
288
291 - def __init__(self, playerid, name, home, team):
294
299
303