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
61 """
62 Player represents a single player and all of his statistical categories.
63 Every player has 'playerid', 'name' and 'home' fields.
64 Additionally, depending upon which statistical categories that player
65 was involved in for the game, he'll have properties such as 'passing_tds',
66 'rushing_yds', 'defense_int' and 'kicking_fgm'.
67
68 In order to know whether a paricular player belongs to a statical category,
69 you may use the filtering methods of a player sequence or alternatively,
70 use the has_cat method with arguments like 'passing', 'rushing', 'kicking',
71 etc. (A player sequence in this case would be an instance of
72 GenPlayerStats.)
73
74 You may also inspect whether a player has a certain property by using
75 the special __dict__ attribute. For example::
76
77 if 'passing_yds' in player.__dict__:
78 # Do something with player.passing_yds
79 """
80 - def __init__(self, playerid, name, home):
81 """
82 Create a new Player instance with the player id (from NFL.com's
83 GameCenter), the player's name (e.g., "T.Brady") and whether the
84 player is playing in a home game or not.
85 """
86 self.playerid = playerid
87 self.name = name
88 self.home = home
89 self._stats = OrderedDict()
90
92 return self.__dict__.get(cat, False)
93
95 for cat in nflgame.statmap.categories:
96 for f in self.__dict__:
97 if f.startswith(cat):
98 self.__dict__[cat] = True
99 break
100
101 @property
103 """
104 Returns the total number of touchdowns credited to this player across
105 all statistical categories.
106 """
107 n = 0
108 for f, v in self.__dict__.iteritems():
109 if f.endswith('tds'):
110 n += v
111 return n
112
113 @property
115 """
116 Returns a dict of all stats for the player.
117 """
118 return self._stats
119
128
130 for k, v in stats.iteritems():
131 self.__dict__[k] = self.__dict__.get(k, 0) + v
132 self._stats[k] = self.__dict__[k]
133 self.__refresh_categories()
134
136 """
137 Simply returns the player's name, e.g., "T.Brady".
138 """
139 return self.name
140
142 """
143 Adds two players together. Only two player objects that correspond
144 to the same human (i.e., GameCenter identifier) can be added together.
145
146 If two different players are added together, an assertion will
147 be raised.
148
149 The effect of adding two player objects simply corresponds to the
150 sums of all statistical values.
151
152 Note that as soon as two players have been added, the 'home' property
153 becomes undefined.
154 """
155 assert self.playerid == other.playerid
156 assert type(self) == type(other)
157
158 new_player = self.__class__(self.playerid, self.name, None)
159 new_player._add_stats(self._stats)
160 new_player._add_stats(other._stats)
161
162 return new_player
163
165 assert self.playerid == other.playerid
166 assert type(self) == type(other)
167
168 new_player = GamePlayerStats(self.playerid, self.name, self.home)
169 new_player._add_stats(self._stats)
170 for bk, bv in other._stats.iteritems():
171 new_player._stats[bk] -= bv
172 if new_player._stats[bk] == 0:
173 del new_player._stats[bk]
174 else:
175 new_player.__dict__[bk] = new_player._stats[bk]
176
177 anydiffs = False
178 for k, v in new_player._stats.iteritems():
179 if v > 0:
180 anydiffs = True
181 break
182 if not anydiffs:
183 return None
184 return new_player
185
194
197 - def __init__(self, playerid, name, home):
200
205
209