8
8
9
9
import lxml .etree as etree
10
10
11
-
12
11
def __player_stats_info (data , name ):
13
12
home = []
14
13
away = []
15
14
for y in data :
16
- # loops through pitchers
15
+ # loops through pitchers and batters
17
16
for x in y .findall (name ):
18
17
stats = {}
19
18
# loop through and save stats
@@ -26,65 +25,133 @@ def __player_stats_info(data, name):
26
25
away .append (stats )
27
26
return (home , away )
28
27
28
+ def __raw_player_stats_info (data ):
29
+ home_pitchers = []
30
+ away_pitchers = []
31
+ home_batters = []
32
+ away_batters = []
33
+
34
+ for team in data .findall ('team' ):
35
+ home_flag = team .attrib ['team_flag' ] == 'home'
36
+ pitching = team .find ('pitching' )
37
+ for pitcher in pitching .findall ('pitcher' ):
38
+ stats = {}
39
+ for i in pitcher .attrib :
40
+ stats [i ] = pitcher .attrib [i ]
41
+ home_pitchers .append (stats ) if home_flag else away_pitchers .append (stats )
42
+
43
+ batting = team .find ('batting' )
44
+ for batter in batting .findall ('batter' ):
45
+ stats = {}
46
+ for i in batter .attrib :
47
+ stats [i ] = batter .attrib [i ]
48
+ home_batters .append (stats ) if home_flag else away_batters .append (stats )
49
+ home = {
50
+ 'pitchers' : home_pitchers ,
51
+ 'batters' : home_batters
52
+ }
53
+
54
+ away = {
55
+ 'pitchers' : away_pitchers ,
56
+ 'batters' : away_batters
57
+ }
58
+ return (home , away )
29
59
30
60
def player_stats (game_id ):
31
- """Return dictionary of individual stats of a game with matching id."""
61
+ """Return dictionary of individual stats of a game with matching id.
62
+
63
+ The additional pitching/batting is mostly the same stats, except it contains
64
+ some useful stats such as groundouts/flyouts per pitcher (go/ao). MLB decided
65
+ to have two box score files, thus we return the data from both.
66
+ """
32
67
# get data from data module
33
- data = mlbgame .data .get_box_score (game_id )
68
+ box_score = mlbgame .data .get_box_score (game_id )
69
+ raw_box_score = mlbgame .data .get_raw_box_score (game_id )
34
70
# parse XML
35
- parsed = etree .parse (data )
36
- root = parsed .getroot ()
71
+ box_score_tree = etree .parse (box_score ). getroot ( )
72
+ raw_box_score_tree = etree . parse ( raw_box_score ) .getroot ()
37
73
# get pitching and batting info
38
- pitching = root .findall ('pitching' )
39
- batting = root .findall ('batting' )
74
+ pitching = box_score_tree .findall ('pitching' )
75
+ batting = box_score_tree .findall ('batting' )
40
76
# get parsed stats
41
77
pitching_info = __player_stats_info (pitching , 'pitcher' )
42
78
batting_info = __player_stats_info (batting , 'batter' )
79
+ # get parsed additional stats
80
+ additional_stats = __raw_player_stats_info (raw_box_score_tree )
81
+ addl_home_pitching = additional_stats [0 ]['pitchers' ]
82
+ addl_home_batting = additional_stats [0 ]['batters' ]
83
+ addl_away_pitching = additional_stats [1 ]['pitchers' ]
84
+ addl_away_batting = additional_stats [1 ]['batters' ]
85
+
43
86
output = {
44
87
'home_pitching' : pitching_info [0 ],
45
88
'away_pitching' : pitching_info [1 ],
46
89
'home_batting' : batting_info [0 ],
47
- 'away_batting' : batting_info [1 ]
90
+ 'away_batting' : batting_info [1 ],
91
+ 'home_additional_pitching' : addl_home_pitching ,
92
+ 'away_additional_pitching' : addl_away_pitching ,
93
+ 'home_additional_batting' : addl_home_batting ,
94
+ 'away_additional_batting' : addl_away_batting
48
95
}
49
96
return output
50
97
51
-
52
- def team_stats (game_id ):
53
- """Return team stats of a game with matching id."""
54
- # get data from data module
55
- data = mlbgame .data .get_box_score (game_id )
56
- # parse XML
57
- parsed = etree .parse (data )
58
- root = parsed .getroot ()
59
- # get pitching and batting ingo
60
- pitching = root .findall ('pitching' )
61
- batting = root .findall ('batting' )
62
- # dictionary for output
63
- output = {}
64
- # loop through pitching info
65
- for x in pitching :
98
+ def __team_stats_info (data , output , output_key ):
99
+ for x in data :
66
100
stats = {}
67
101
# loop through stats and save
68
102
for y in x .attrib :
69
103
stats [y ] = x .attrib [y ]
70
104
# apply to correct team
71
105
if x .attrib ['team_flag' ] == 'home' :
72
- output ['home_pitching' ] = stats
106
+ # Example: 'home_batting' when output_key is 'batting'
107
+ output ['home_' + output_key ] = stats
73
108
elif x .attrib ['team_flag' ] == 'away' :
74
- output ['away_pitching' ] = stats
75
- # loop through pitching info
76
- for x in batting :
109
+ output ['away_' + output_key ] = stats
110
+ return output
111
+
112
+ def __raw_team_stats_info (data , output ):
113
+ for team in data .findall ('team' ):
114
+ home_flag = team .attrib ['team_flag' ] == 'home'
115
+ pitching = team .find ('pitching' )
77
116
stats = {}
78
- # loop through stats and save
79
- for y in x .attrib :
80
- stats [y ] = x .attrib [y ]
81
- # apply to correct team
82
- if x .attrib ['team_flag' ] == 'home' :
83
- output ['home_batting' ] = stats
84
- elif x .attrib ['team_flag' ] == 'away' :
85
- output ['away_batting' ] = stats
117
+ for stat in pitching .attrib :
118
+ stats [stat ] = pitching .attrib [stat ]
119
+ if home_flag :
120
+ output ['home_additional_pitching' ] = stats
121
+ else :
122
+ output ['away_additional_pitching' ] = stats
123
+
124
+ stats = {}
125
+ batting = team .find ('batting' )
126
+ for stat in batting .attrib :
127
+ stats [stat ] = batting .attrib [stat ]
128
+ if home_flag :
129
+ output ['home_additional_batting' ] = stats
130
+ else :
131
+ output ['away_additional_batting' ] = stats
86
132
return output
87
133
134
+ def team_stats (game_id ):
135
+ """Return team stats of a game with matching id.
136
+
137
+ The additional pitching/batting is mostly the same stats. MLB decided
138
+ to have two box score files, thus we return the data from both.
139
+ """
140
+ # get data from data module
141
+ box_score = mlbgame .data .get_box_score (game_id )
142
+ raw_box_score = mlbgame .data .get_raw_box_score (game_id )
143
+ # parse XML
144
+ box_score_tree = etree .parse (box_score ).getroot ()
145
+ raw_box_score_tree = etree .parse (raw_box_score ).getroot ()
146
+ # get pitching and batting ingo
147
+ pitching = box_score_tree .findall ('pitching' )
148
+ batting = box_score_tree .findall ('batting' )
149
+ # dictionary for output
150
+ output = {}
151
+ output = __team_stats_info (pitching , output , 'pitching' )
152
+ output = __team_stats_info (batting , output , 'batting' )
153
+ output = __raw_team_stats_info (raw_box_score_tree , output )
154
+ return output
88
155
89
156
class Stats (object ):
90
157
"""Hold stats information for a game.
@@ -95,6 +162,10 @@ class Stats(object):
95
162
game_id
96
163
home_batting
97
164
home_pitching
165
+ away_additional_pitching
166
+ away_additional_batting
167
+ home_additional_pitching
168
+ home_additional_batting
98
169
"""
99
170
100
171
def __init__ (self , data , game_id , player ):
@@ -104,7 +175,8 @@ def __init__(self, data, game_id, player):
104
175
"""
105
176
self .game_id = game_id
106
177
output = {'home_pitching' : [], 'away_pitching' : [], 'home_batting' : [],
107
- 'away_batting' : []}
178
+ 'away_batting' : [], 'home_additional_pitching' : [], 'home_additional_batting' : [],
179
+ 'away_additional_pitching' : [], 'away_additional_batting' : []}
108
180
for y in data :
109
181
# create objects for all data
110
182
if player :
@@ -118,7 +190,10 @@ def __init__(self, data, game_id, player):
118
190
self .away_pitching = output ['away_pitching' ]
119
191
self .home_batting = output ['home_batting' ]
120
192
self .away_batting = output ['away_batting' ]
121
-
193
+ self .home_additional_pitching = output ['home_additional_pitching' ]
194
+ self .away_additional_pitching = output ['away_additional_pitching' ]
195
+ self .home_additional_batting = output ['home_additional_batting' ]
196
+ self .away_additional_batting = output ['away_additional_batting' ]
122
197
123
198
class PlayerStats (mlbgame .object .Object ):
124
199
"""Holds stats information for a player.
0 commit comments