def is_pa_terminal(cls, ball_tally, strike_tally, pitch_res, event_cd): """ Is PA terminal :param ball_tally: Ball telly :param strike_tally: Strike telly :param pitch_res: pitching result(Retrosheet format) :param event_cd: Event code :return: FLG(T or F) """ if RetroSheet.is_pa_terminal(ball_tally, strike_tally, pitch_res, event_cd): return MlbamConst.FLG_TRUE return MlbamConst.FLG_FALSE
def result(cls, ab, pa, pitch_list): """ At Bat Result :param ab: at bat object(type:Beautifulsoup) :param pa: atbat data for plate appearance :param pitch_list: Pitching data :return: pa result value(dict) """ atbat = OrderedDict() atbat['ball_ct'] = MlbamUtil.get_attribute_stats(ab, 'b', int, None) atbat['strike_ct'] = MlbamUtil.get_attribute_stats(ab, 's', int, None) atbat['pitch_seq'] = ''.join([pitch['pitch_res'] for pitch in pitch_list]) atbat['pitch_type_seq'] = '|'.join([pitch['pitch_type'] for pitch in pitch_list]) atbat['battedball_cd'] = RetroSheet.battedball_cd(pa['event_cd'], pa['event_tx'], pa['ab_des']) return atbat
def _get_pitch(self, soup, pa): """ get pitch data :param soup: Beautifulsoup object :param pa: atbat data for plate appearance :return: pitches result(list) """ pitches = [] ball_tally, strike_tally = 0, 0 for pitch in soup.find_all('pitch'): # pitching result pitch = Pitch.row(pitch, pa, pitches, ball_tally, strike_tally) pitches.append(pitch) # ball count ball_tally, strike_tally = RetroSheet.ball_count(ball_tally, strike_tally, pitch['pitch_res']) return pitches
def _get_pitch(self, soup, pa): """ get pitch data :param soup: Beautifulsoup object :param pa: atbat data for plate appearance :return: pitches result(list) """ pitches = [] ball_tally, strike_tally = 0, 0 for pitch in soup.find_all('pitch'): # pitching result pitch = Pitch.row(pitch, pa, pitches, ball_tally, strike_tally) pitches.append(pitch) # ball count ball_tally, strike_tally = RetroSheet.ball_count( ball_tally, strike_tally, pitch['pitch_res']) return pitches
def result(cls, ab, pa, pitch_list): """ At Bat Result :param ab: at bat object(type:Beautifulsoup) :param pa: atbat data for plate appearance :param pitch_list: Pitching data :return: pa result value(dict) """ atbat = OrderedDict() atbat['ball_ct'] = MlbamUtil.get_attribute_stats(ab, 'b', int, None) atbat['strike_ct'] = MlbamUtil.get_attribute_stats(ab, 's', int, None) atbat['pitch_seq'] = ''.join( [pitch['pitch_res'] for pitch in pitch_list]) atbat['pitch_type_seq'] = '|'.join( [pitch['pitch_type'] for pitch in pitch_list]) atbat['battedball_cd'] = RetroSheet.battedball_cd( pa['event_cd'], pa['event_tx'], pa['ab_des']) return atbat
def pa(cls, ab, game, rosters, inning_number, inning_id, out_ct, hit_location): """ plate appearance data :param ab: at bat object(type:Beautifulsoup) :param game: MLBAM Game object :param rosters: Game Rosters :param inning_number: Inning Number :param inning_id: Inning Id(0:home 1:away) :param pitch_list: Pitching :param out_ct: out count :param hit_location: Hitlocation data(dict) :return: pa value(dict) """ ab_des = MlbamUtil.get_attribute_stats(ab, 'des', str, MlbamConst.UNKNOWN_FULL) event_tx = MlbamUtil.get_attribute_stats(ab, 'event', str, MlbamConst.UNKNOWN_FULL) event_cd = RetroSheet.event_cd(event_tx, ab_des) event_outs_ct = MlbamUtil.get_attribute_stats(ab, 'o', int, 0) start_bases, end_bases = cls._get_bases(ab) pit_mlbid = MlbamUtil.get_attribute_stats(ab, 'pitcher', str, MlbamConst.UNKNOWN_FULL) bat_mlbid = MlbamUtil.get_attribute_stats(ab, 'batter', str, MlbamConst.UNKNOWN_FULL) pit_player = rosters.get(pit_mlbid) bat_player = rosters.get(bat_mlbid) location_key = Inning.HITLOCATION_KEY_FORMAT.format( inning=inning_number, des=event_tx, pitcher=pit_mlbid, batter=bat_mlbid, ) location = hit_location.get(location_key, {}) atbat = OrderedDict() atbat['retro_game_id'] = game.retro_game_id atbat['year'] = game.timestamp.year atbat['month'] = game.timestamp.month atbat['day'] = game.timestamp.day atbat['st_fl'] = game.st_fl atbat['regseason_fl'] = game.regseason_fl atbat['playoff_fl'] = game.playoff_fl atbat['game_type'] = game.game_type atbat['game_type_des'] = game.game_type_des atbat['local_game_time'] = game.local_game_time atbat['game_id'] = game.game_id atbat['home_team_id'] = game.home_team_id atbat['away_team_id'] = game.away_team_id atbat['home_team_lg'] = game.home_team_lg atbat['away_team_lg'] = game.away_team_lg atbat['interleague_fl'] = game.interleague_fl atbat['park_id'] = game.park_id atbat['park_name'] = game.park_name atbat['park_location'] = game.park_loc atbat['inning_number'] = inning_number atbat['bat_home_id'] = inning_id atbat['outs_ct'] = out_ct atbat['pit_mlbid'] = pit_mlbid atbat['pit_first_name'] = pit_player.first atbat['pit_last_name'] = pit_player.last atbat['pit_box_name'] = pit_player.box_name atbat['pit_hand_cd'] = MlbamUtil.get_attribute_stats(ab, 'p_throws', str, MlbamConst.UNKNOWN_FULL) atbat['bat_mlbid'] = bat_mlbid atbat['bat_first_name'] = bat_player.first atbat['bat_last_name'] = bat_player.last atbat['bat_box_name'] = bat_player.box_name atbat['bat_hand_cd'] = MlbamUtil.get_attribute_stats(ab, 'stand', str, MlbamConst.UNKNOWN_FULL) atbat['ab_number'] = MlbamUtil.get_attribute_stats(ab, 'num', int, None) atbat['start_bases'] = start_bases atbat['end_bases'] = end_bases atbat['event_outs_ct'] = event_outs_ct atbat['ab_des'] = ab_des atbat['event_tx'] = event_tx atbat['event_cd'] = event_cd atbat['hit_x'] = location.get('hit_x', None) atbat['hit_y'] = location.get('hit_y', None) return atbat
def test_ball_count(self): """ Ball Count """ # ball:0 strike:0 self.assertEqual(RetroSheet.ball_count(0, 0, 'B'), (1, 0)) self.assertEqual(RetroSheet.ball_count(0, 0, 'S'), (0, 1)) self.assertEqual(RetroSheet.ball_count(0, 0, 'C'), (0, 1)) self.assertEqual(RetroSheet.ball_count(0, 0, 'X'), (0, 1)) self.assertEqual(RetroSheet.ball_count(0, 0, 'F'), (0, 1)) # ball:1 strike:0 self.assertEqual(RetroSheet.ball_count(1, 0, 'B'), (2, 0)) self.assertEqual(RetroSheet.ball_count(1, 0, 'S'), (1, 1)) self.assertEqual(RetroSheet.ball_count(1, 0, 'C'), (1, 1)) self.assertEqual(RetroSheet.ball_count(1, 0, 'X'), (1, 1)) self.assertEqual(RetroSheet.ball_count(1, 0, 'F'), (1, 1)) # ball:2 strike:0 self.assertEqual(RetroSheet.ball_count(2, 0, 'B'), (3, 0)) self.assertEqual(RetroSheet.ball_count(2, 0, 'S'), (2, 1)) self.assertEqual(RetroSheet.ball_count(2, 0, 'C'), (2, 1)) self.assertEqual(RetroSheet.ball_count(2, 0, 'X'), (2, 1)) self.assertEqual(RetroSheet.ball_count(2, 0, 'F'), (2, 1)) # ball:3 strike:0 self.assertEqual(RetroSheet.ball_count(3, 0, 'B'), (4, 0)) self.assertEqual(RetroSheet.ball_count(3, 0, 'S'), (3, 1)) self.assertEqual(RetroSheet.ball_count(3, 0, 'C'), (3, 1)) self.assertEqual(RetroSheet.ball_count(3, 0, 'X'), (3, 1)) self.assertEqual(RetroSheet.ball_count(3, 0, 'F'), (3, 1)) # ball:0 strike:1 self.assertEqual(RetroSheet.ball_count(0, 1, 'B'), (1, 1)) self.assertEqual(RetroSheet.ball_count(0, 1, 'S'), (0, 2)) self.assertEqual(RetroSheet.ball_count(0, 1, 'C'), (0, 2)) self.assertEqual(RetroSheet.ball_count(0, 1, 'X'), (0, 2)) self.assertEqual(RetroSheet.ball_count(0, 1, 'F'), (0, 2)) # ball:1 strike:1 self.assertEqual(RetroSheet.ball_count(1, 1, 'B'), (2, 1)) self.assertEqual(RetroSheet.ball_count(1, 1, 'S'), (1, 2)) self.assertEqual(RetroSheet.ball_count(1, 1, 'C'), (1, 2)) self.assertEqual(RetroSheet.ball_count(1, 1, 'X'), (1, 2)) self.assertEqual(RetroSheet.ball_count(1, 1, 'F'), (1, 2)) # ball:2 strike:1 self.assertEqual(RetroSheet.ball_count(2, 1, 'B'), (3, 1)) self.assertEqual(RetroSheet.ball_count(2, 1, 'S'), (2, 2)) self.assertEqual(RetroSheet.ball_count(2, 1, 'C'), (2, 2)) self.assertEqual(RetroSheet.ball_count(2, 1, 'X'), (2, 2)) self.assertEqual(RetroSheet.ball_count(2, 1, 'F'), (2, 2)) # ball:3 strike:1 self.assertEqual(RetroSheet.ball_count(3, 1, 'B'), (4, 1)) self.assertEqual(RetroSheet.ball_count(3, 1, 'S'), (3, 2)) self.assertEqual(RetroSheet.ball_count(3, 1, 'C'), (3, 2)) self.assertEqual(RetroSheet.ball_count(3, 1, 'X'), (3, 2)) self.assertEqual(RetroSheet.ball_count(3, 1, 'F'), (3, 2)) # ball:0 strike:2 self.assertEqual(RetroSheet.ball_count(0, 2, 'B'), (1, 2)) self.assertEqual(RetroSheet.ball_count(0, 2, 'S'), (0, 3)) self.assertEqual(RetroSheet.ball_count(0, 2, 'C'), (0, 3)) self.assertEqual(RetroSheet.ball_count(0, 2, 'X'), (0, 3)) self.assertEqual(RetroSheet.ball_count(0, 2, 'F'), (0, 2)) # ball:1 strike:2 self.assertEqual(RetroSheet.ball_count(1, 2, 'B'), (2, 2)) self.assertEqual(RetroSheet.ball_count(1, 2, 'S'), (1, 3)) self.assertEqual(RetroSheet.ball_count(1, 2, 'C'), (1, 3)) self.assertEqual(RetroSheet.ball_count(1, 2, 'X'), (1, 3)) self.assertEqual(RetroSheet.ball_count(1, 2, 'F'), (1, 2)) # ball:2 strike:2 self.assertEqual(RetroSheet.ball_count(2, 2, 'B'), (3, 2)) self.assertEqual(RetroSheet.ball_count(2, 2, 'S'), (2, 3)) self.assertEqual(RetroSheet.ball_count(2, 2, 'C'), (2, 3)) self.assertEqual(RetroSheet.ball_count(2, 2, 'X'), (2, 3)) self.assertEqual(RetroSheet.ball_count(2, 2, 'F'), (2, 2)) # ball:3 strike:2 self.assertEqual(RetroSheet.ball_count(3, 2, 'B'), (4, 2)) self.assertEqual(RetroSheet.ball_count(3, 2, 'S'), (3, 3)) self.assertEqual(RetroSheet.ball_count(3, 2, 'C'), (3, 3)) self.assertEqual(RetroSheet.ball_count(3, 2, 'X'), (3, 3)) self.assertEqual(RetroSheet.ball_count(3, 2, 'F'), (3, 2))
def test_battedball_cd(self): """ Get Battedball code """ # Generic out self.assertEqual(RetroSheet.battedball_cd(2, 'Flyout', 'hoge fly out'), 'F') self.assertEqual(RetroSheet.battedball_cd(2, 'Fly Out', 'hoge fly out'), 'F') self.assertEqual(RetroSheet.battedball_cd(2, 'Sac Fly', 'hoge fly out'), 'F') self.assertEqual(RetroSheet.battedball_cd(2, 'Sac Fly DP', 'hoge fly out'), 'F') self.assertEqual(RetroSheet.battedball_cd(2, 'Lineout', 'hoge line out'), 'L') self.assertEqual(RetroSheet.battedball_cd(2, 'Line Out', 'hoge line out'), 'L') self.assertEqual(RetroSheet.battedball_cd(2, 'Bunt Lineout', 'hoge line out'), 'L') self.assertEqual(RetroSheet.battedball_cd(2, 'Pop Out', 'hoge pop out'), 'P') self.assertEqual(RetroSheet.battedball_cd(2, 'Pop out', 'hoge pop out'), 'P') self.assertEqual(RetroSheet.battedball_cd(2, 'Bunt Pop out', 'hoge pop out'), 'P') self.assertEqual(RetroSheet.battedball_cd(2, 'Groundout', 'hoge ground out'), 'G') self.assertEqual(RetroSheet.battedball_cd(2, 'Ground Out', 'hoge ground out'), 'G') self.assertEqual(RetroSheet.battedball_cd(2, 'Sac Bunt', 'hoge ground out'), 'G') self.assertEqual(RetroSheet.battedball_cd(2, 'Bunt Groundout', 'hoge ground out'), 'G') self.assertEqual(RetroSheet.battedball_cd(2, 'Grounded Into DP', 'hoge ground into dp'), 'G') self.assertEqual(RetroSheet.battedball_cd(2, 'Forceout', 'hoge force out grounds'), 'G') self.assertEqual(RetroSheet.battedball_cd(2, 'Forceout', 'hoge force out lines'), 'L') self.assertEqual(RetroSheet.battedball_cd(2, 'Forceout', 'hoge force out flies'), 'F') self.assertEqual(RetroSheet.battedball_cd(2, 'Forceout', 'hoge force out pops'), 'P') self.assertEqual(RetroSheet.battedball_cd(2, 'Forceout', 'hoge force out'), '') self.assertEqual(RetroSheet.battedball_cd(2, 'Double Play', 'hoge dp grounds'), 'G') self.assertEqual(RetroSheet.battedball_cd(2, 'Double Play', 'hoge dp lines'), 'L') self.assertEqual(RetroSheet.battedball_cd(2, 'Double Play', 'hoge dp flies'), 'F') self.assertEqual(RetroSheet.battedball_cd(2, 'Double Play', 'hoge dp pops'), 'P') self.assertEqual(RetroSheet.battedball_cd(2, 'Double Play', 'hoge dp'), '') self.assertEqual(RetroSheet.battedball_cd(2, 'Triple Play', 'hoge tp grounds'), 'G') self.assertEqual(RetroSheet.battedball_cd(2, 'Triple Play', 'hoge tp lines'), 'L') self.assertEqual(RetroSheet.battedball_cd(2, 'Triple Play', 'hoge tp flies'), 'F') self.assertEqual(RetroSheet.battedball_cd(2, 'Triple Play', 'hoge tp pops'), 'P') self.assertEqual(RetroSheet.battedball_cd(2, 'Triple Play', 'hoge tp'), '') self.assertEqual(RetroSheet.battedball_cd(2, 'Sacrifice Bunt D', 'hoge bunt dp grounds'), 'G') self.assertEqual(RetroSheet.battedball_cd(2, 'Sacrifice Bunt D', 'hoge bunt dp lines'), 'L') self.assertEqual(RetroSheet.battedball_cd(2, 'Sacrifice Bunt D', 'hoge bunt dp flies'), 'F') self.assertEqual(RetroSheet.battedball_cd(2, 'Sacrifice Bunt D', 'hoge bunt dp pops'), 'P') self.assertEqual(RetroSheet.battedball_cd(2, 'Sacrifice Bunt D', 'hoge bunt dp'), '') # Strike out self.assertEqual(RetroSheet.battedball_cd(3, 'Strikeout', 'hoge strike out'), '') self.assertEqual(RetroSheet.battedball_cd(3, 'Strikeout - DP', 'hoge strike out dp'), '') # walk, intent walk ,hit by pitch self.assertEqual(RetroSheet.battedball_cd(14, 'Walk', 'hoge walk'), '') self.assertEqual(RetroSheet.battedball_cd(15, 'Intent Walk', 'hoge intent walk'), '') self.assertEqual(RetroSheet.battedball_cd(16, 'Hit By Pitch', 'hoge hit by pitch'), '') # Interference self.assertEqual(RetroSheet.battedball_cd(17, 'hoge interference', 'hoge interference'), '') self.assertEqual(RetroSheet.battedball_cd(17, 'Interference fuga', 'hoge interference'), '') # Error self.assertEqual(RetroSheet.battedball_cd(18, 'hoge error', 'hoge error'), '') self.assertEqual(RetroSheet.battedball_cd(18, 'fuga Error', 'fuga error'), '') # Not Error self.assertEqual(RetroSheet.battedball_cd(0, 'error fuga', 'fuga not error'), '') # Fielder's Choice self.assertEqual(RetroSheet.battedball_cd(19, 'Fielders Choice Out', 'hoge fielders choice'), '') self.assertEqual(RetroSheet.battedball_cd(19, 'Fielders Choice', 'hoge fielders choice'), '') # Single, 2B, 3B, HR self.assertEqual(RetroSheet.battedball_cd(20, 'Single', 'hoge Single on a line drive to fuga'), 'L') self.assertEqual(RetroSheet.battedball_cd(20, 'Single', 'hoge Single fly ball to fuga'), 'F') self.assertEqual(RetroSheet.battedball_cd(20, 'Single', 'hoge Single ground ball to fuga'), 'G') self.assertEqual(RetroSheet.battedball_cd(20, 'Single', 'hoge Single pop up to fuga'), 'P') self.assertEqual(RetroSheet.battedball_cd(20, 'Single', 'hoge Single'), '') self.assertEqual(RetroSheet.battedball_cd(21, 'Double', 'hoge 2B on a line drive to fuga'), 'L') self.assertEqual(RetroSheet.battedball_cd(21, 'Double', 'hoge 2B fly ball to fuga'), 'F') self.assertEqual(RetroSheet.battedball_cd(21, 'Double', 'hoge 2B ground ball to fuga'), 'G') self.assertEqual(RetroSheet.battedball_cd(21, 'Double', 'hoge 2B pop up to fuga'), 'P') self.assertEqual(RetroSheet.battedball_cd(21, 'Double', 'hoge 2B'), '') self.assertEqual(RetroSheet.battedball_cd(22, 'Triple', 'hoge 3B on a line drive to fuga'), 'L') self.assertEqual(RetroSheet.battedball_cd(22, 'Triple', 'hoge 3B fly ball to fuga'), 'F') self.assertEqual(RetroSheet.battedball_cd(22, 'Triple', 'hoge 3B ground ball to fuga'), 'G') self.assertEqual(RetroSheet.battedball_cd(22, 'Triple', 'hoge 3B pop up to fuga'), 'P') self.assertEqual(RetroSheet.battedball_cd(22, 'Triple', 'hoge 3B'), '') self.assertEqual(RetroSheet.battedball_cd(23, 'Home Run', 'hoge 3B on a line drive to fuga'), 'L') self.assertEqual(RetroSheet.battedball_cd(23, 'Home Run', 'hoge 3B fly ball to fuga'), 'F') self.assertEqual(RetroSheet.battedball_cd(23, 'Home Run', 'hoge 3B ground ball to fuga'), 'G') self.assertEqual(RetroSheet.battedball_cd(23, 'Home Run', 'hoge 3B pop up to fuga'), 'P') self.assertEqual(RetroSheet.battedball_cd(23, 'Home Run', 'hoge 3B'), '') # Runner Out self.assertEqual(RetroSheet.battedball_cd(6, 'Runner Out', 'hoge caught stealing'), '') self.assertEqual(RetroSheet.battedball_cd(8, 'Runner Out', 'hoge picks off'), '') # Unknown Event self.assertEqual(RetroSheet.battedball_cd(0, 'bar', 'hoge picks off'), '') self.assertEqual(RetroSheet.battedball_cd(0, 'Runner Out', 'hoge fuga'), '')
def test_pa_terminal(self): """ PA terminal """ # In Play self.assertTrue(RetroSheet.is_pa_terminal(0, 0, 'X', 3)) self.assertTrue(RetroSheet.is_pa_terminal(0, 1, 'X', 20)) self.assertTrue(RetroSheet.is_pa_terminal(1, 2, 'X', 21)) self.assertTrue(RetroSheet.is_pa_terminal(2, 2, 'X', 22)) self.assertTrue(RetroSheet.is_pa_terminal(3, 2, 'X', 23)) # Strike Out(Strike or Call) self.assertTrue(RetroSheet.is_pa_terminal(0, 2, 'S', 3)) self.assertTrue(RetroSheet.is_pa_terminal(1, 2, 'S', 3)) self.assertTrue(RetroSheet.is_pa_terminal(2, 2, 'S', 3)) self.assertTrue(RetroSheet.is_pa_terminal(3, 2, 'S', 3)) self.assertTrue(RetroSheet.is_pa_terminal(0, 2, 'C', 3)) self.assertTrue(RetroSheet.is_pa_terminal(1, 2, 'C', 3)) self.assertTrue(RetroSheet.is_pa_terminal(2, 2, 'C', 3)) self.assertTrue(RetroSheet.is_pa_terminal(3, 2, 'C', 3)) # Not Strike Out self.assertFalse(RetroSheet.is_pa_terminal(0, 1, 'S', 3)) self.assertFalse(RetroSheet.is_pa_terminal(1, 1, 'S', 3)) self.assertFalse(RetroSheet.is_pa_terminal(2, 1, 'S', 3)) self.assertFalse(RetroSheet.is_pa_terminal(3, 1, 'S', 3)) self.assertFalse(RetroSheet.is_pa_terminal(0, 0, 'C', 3)) self.assertFalse(RetroSheet.is_pa_terminal(1, 0, 'C', 3)) self.assertFalse(RetroSheet.is_pa_terminal(2, 0, 'C', 3)) self.assertFalse(RetroSheet.is_pa_terminal(3, 0, 'C', 3)) # Walk(Ball or Intent Ball) self.assertTrue(RetroSheet.is_pa_terminal(3, 0, 'B', 14)) self.assertTrue(RetroSheet.is_pa_terminal(3, 1, 'B', 14)) self.assertTrue(RetroSheet.is_pa_terminal(3, 2, 'B', 14)) self.assertTrue(RetroSheet.is_pa_terminal(3, 0, 'B', 15)) self.assertTrue(RetroSheet.is_pa_terminal(3, 1, 'B', 15)) self.assertTrue(RetroSheet.is_pa_terminal(3, 2, 'B', 15)) # Not Walk self.assertFalse(RetroSheet.is_pa_terminal(2, 0, 'B', 14)) self.assertFalse(RetroSheet.is_pa_terminal(1, 1, 'B', 14)) self.assertFalse(RetroSheet.is_pa_terminal(0, 2, 'B', 14)) self.assertFalse(RetroSheet.is_pa_terminal(2, 0, 'B', 15)) self.assertFalse(RetroSheet.is_pa_terminal(1, 1, 'B', 15)) self.assertFalse(RetroSheet.is_pa_terminal(0, 2, 'B', 15))
def test_event_cd(self): """ Get Event code """ # Generic out self.assertEqual(RetroSheet.event_cd('Flyout', 'hoge fly out'), 2) self.assertEqual(RetroSheet.event_cd('Fly Out', 'hoge fly out'), 2) self.assertEqual(RetroSheet.event_cd('Sac Fly', 'hoge fly out'), 2) self.assertEqual(RetroSheet.event_cd('Sac Fly DP', 'hoge fly out'), 2) self.assertEqual(RetroSheet.event_cd('Lineout', 'hoge line out'), 2) self.assertEqual(RetroSheet.event_cd('Line Out', 'hoge line out'), 2) self.assertEqual(RetroSheet.event_cd('Bunt Lineout', 'hoge line out'), 2) self.assertEqual(RetroSheet.event_cd('Pop Out', 'hoge pop out'), 2) self.assertEqual(RetroSheet.event_cd('Pop out', 'hoge pop out'), 2) self.assertEqual(RetroSheet.event_cd('Bunt Pop out', 'hoge pop out'), 2) self.assertEqual(RetroSheet.event_cd('Groundout', 'hoge ground out'), 2) self.assertEqual(RetroSheet.event_cd('Ground Out', 'hoge ground out'), 2) self.assertEqual(RetroSheet.event_cd('Sac Bunt', 'hoge ground out'), 2) self.assertEqual(RetroSheet.event_cd('Bunt Groundout', 'hoge ground out'), 2) self.assertEqual(RetroSheet.event_cd('Grounded Into DP', 'hoge ground into dp'), 2) self.assertEqual(RetroSheet.event_cd('Forceout', 'hoge force out'), 2) self.assertEqual(RetroSheet.event_cd('Double Play', 'hoge dp'), 2) self.assertEqual(RetroSheet.event_cd('Triple Play', 'hoge tp'), 2) self.assertEqual(RetroSheet.event_cd('Sacrifice Bunt D', 'hoge bunt dp'), 2) # Strike out self.assertEqual(RetroSheet.event_cd('Strikeout', 'hoge strike out'), 3) self.assertEqual(RetroSheet.event_cd('Strikeout - DP', 'hoge strike out dp'), 3) # walk, intent walk ,hit by pitch self.assertEqual(RetroSheet.event_cd('Walk', 'hoge walk'), 14) self.assertEqual(RetroSheet.event_cd('Intent Walk', 'hoge intent walk'), 15) self.assertEqual(RetroSheet.event_cd('Hit By Pitch', 'hoge hit by pitch'), 16) # Interference self.assertEqual(RetroSheet.event_cd('hoge interference', 'hoge interference'), 17) self.assertEqual(RetroSheet.event_cd('Interference fuga', 'hoge interference'), 17) # Error self.assertEqual(RetroSheet.event_cd('hoge error', 'hoge error'), 18) self.assertEqual(RetroSheet.event_cd('fuga Error', 'fuga error'), 18) # Not Error self.assertEqual(RetroSheet.event_cd('error fuga', 'fuga not error'), 0) # Fielder's Choice self.assertEqual(RetroSheet.event_cd('Fielders Choice Out', 'hoge fielders choice'), 19) self.assertEqual(RetroSheet.event_cd('Fielders Choice', 'hoge fielders choice'), 19) # Single, 2B, 3B, HR self.assertEqual(RetroSheet.event_cd('Single', 'hoge Single'), 20) self.assertEqual(RetroSheet.event_cd('Double', 'hoge 2B'), 21) self.assertEqual(RetroSheet.event_cd('Triple', 'hoge 3B'), 22) self.assertEqual(RetroSheet.event_cd('Home Run', 'hoge home run'), 23) # Runner Out self.assertEqual(RetroSheet.event_cd('Runner Out', 'hoge caught stealing'), 6) self.assertEqual(RetroSheet.event_cd('Runner Out', 'hoge picks off'), 8) # Unknown Event self.assertEqual(RetroSheet.event_cd('bar', 'hoge picks off'), 0) self.assertEqual(RetroSheet.event_cd('Runner Out', 'hoge fuga'), 0)
def test_battedball_cd(self): """ Get Battedball code """ # Generic out self.assertEqual(RetroSheet.battedball_cd(2, 'Flyout', 'hoge fly out'), 'F') self.assertEqual( RetroSheet.battedball_cd(2, 'Fly Out', 'hoge fly out'), 'F') self.assertEqual( RetroSheet.battedball_cd(2, 'Sac Fly', 'hoge fly out'), 'F') self.assertEqual( RetroSheet.battedball_cd(2, 'Sac Fly DP', 'hoge fly out'), 'F') self.assertEqual( RetroSheet.battedball_cd(2, 'Lineout', 'hoge line out'), 'L') self.assertEqual( RetroSheet.battedball_cd(2, 'Line Out', 'hoge line out'), 'L') self.assertEqual( RetroSheet.battedball_cd(2, 'Bunt Lineout', 'hoge line out'), 'L') self.assertEqual( RetroSheet.battedball_cd(2, 'Pop Out', 'hoge pop out'), 'P') self.assertEqual( RetroSheet.battedball_cd(2, 'Pop out', 'hoge pop out'), 'P') self.assertEqual( RetroSheet.battedball_cd(2, 'Bunt Pop out', 'hoge pop out'), 'P') self.assertEqual( RetroSheet.battedball_cd(2, 'Groundout', 'hoge ground out'), 'G') self.assertEqual( RetroSheet.battedball_cd(2, 'Ground Out', 'hoge ground out'), 'G') self.assertEqual( RetroSheet.battedball_cd(2, 'Sac Bunt', 'hoge ground out'), 'G') self.assertEqual( RetroSheet.battedball_cd(2, 'Bunt Groundout', 'hoge ground out'), 'G') self.assertEqual( RetroSheet.battedball_cd(2, 'Grounded Into DP', 'hoge ground into dp'), 'G') self.assertEqual( RetroSheet.battedball_cd(2, 'Forceout', 'hoge force out grounds'), 'G') self.assertEqual( RetroSheet.battedball_cd(2, 'Forceout', 'hoge force out lines'), 'L') self.assertEqual( RetroSheet.battedball_cd(2, 'Forceout', 'hoge force out flies'), 'F') self.assertEqual( RetroSheet.battedball_cd(2, 'Forceout', 'hoge force out pops'), 'P') self.assertEqual( RetroSheet.battedball_cd(2, 'Forceout', 'hoge force out'), '') self.assertEqual( RetroSheet.battedball_cd(2, 'Double Play', 'hoge dp grounds'), 'G') self.assertEqual( RetroSheet.battedball_cd(2, 'Double Play', 'hoge dp lines'), 'L') self.assertEqual( RetroSheet.battedball_cd(2, 'Double Play', 'hoge dp flies'), 'F') self.assertEqual( RetroSheet.battedball_cd(2, 'Double Play', 'hoge dp pops'), 'P') self.assertEqual(RetroSheet.battedball_cd(2, 'Double Play', 'hoge dp'), '') self.assertEqual( RetroSheet.battedball_cd(2, 'Triple Play', 'hoge tp grounds'), 'G') self.assertEqual( RetroSheet.battedball_cd(2, 'Triple Play', 'hoge tp lines'), 'L') self.assertEqual( RetroSheet.battedball_cd(2, 'Triple Play', 'hoge tp flies'), 'F') self.assertEqual( RetroSheet.battedball_cd(2, 'Triple Play', 'hoge tp pops'), 'P') self.assertEqual(RetroSheet.battedball_cd(2, 'Triple Play', 'hoge tp'), '') self.assertEqual( RetroSheet.battedball_cd(2, 'Sacrifice Bunt D', 'hoge bunt dp grounds'), 'G') self.assertEqual( RetroSheet.battedball_cd(2, 'Sacrifice Bunt D', 'hoge bunt dp lines'), 'L') self.assertEqual( RetroSheet.battedball_cd(2, 'Sacrifice Bunt D', 'hoge bunt dp flies'), 'F') self.assertEqual( RetroSheet.battedball_cd(2, 'Sacrifice Bunt D', 'hoge bunt dp pops'), 'P') self.assertEqual( RetroSheet.battedball_cd(2, 'Sacrifice Bunt D', 'hoge bunt dp'), '') # Strike out self.assertEqual( RetroSheet.battedball_cd(3, 'Strikeout', 'hoge strike out'), '') self.assertEqual( RetroSheet.battedball_cd(3, 'Strikeout - DP', 'hoge strike out dp'), '') # walk, intent walk ,hit by pitch self.assertEqual(RetroSheet.battedball_cd(14, 'Walk', 'hoge walk'), '') self.assertEqual( RetroSheet.battedball_cd(15, 'Intent Walk', 'hoge intent walk'), '') self.assertEqual( RetroSheet.battedball_cd(16, 'Hit By Pitch', 'hoge hit by pitch'), '') # Interference self.assertEqual( RetroSheet.battedball_cd(17, 'hoge interference', 'hoge interference'), '') self.assertEqual( RetroSheet.battedball_cd(17, 'Interference fuga', 'hoge interference'), '') # Error self.assertEqual( RetroSheet.battedball_cd(18, 'hoge error', 'hoge error'), '') self.assertEqual( RetroSheet.battedball_cd(18, 'fuga Error', 'fuga error'), '') # Not Error self.assertEqual( RetroSheet.battedball_cd(0, 'error fuga', 'fuga not error'), '') # Fielder's Choice self.assertEqual( RetroSheet.battedball_cd(19, 'Fielders Choice Out', 'hoge fielders choice'), '') self.assertEqual( RetroSheet.battedball_cd(19, 'Fielders Choice', 'hoge fielders choice'), '') # Single, 2B, 3B, HR self.assertEqual( RetroSheet.battedball_cd(20, 'Single', 'hoge Single on a line drive to fuga'), 'L') self.assertEqual( RetroSheet.battedball_cd(20, 'Single', 'hoge Single fly ball to fuga'), 'F') self.assertEqual( RetroSheet.battedball_cd(20, 'Single', 'hoge Single ground ball to fuga'), 'G') self.assertEqual( RetroSheet.battedball_cd(20, 'Single', 'hoge Single pop up to fuga'), 'P') self.assertEqual(RetroSheet.battedball_cd(20, 'Single', 'hoge Single'), '') self.assertEqual( RetroSheet.battedball_cd(21, 'Double', 'hoge 2B on a line drive to fuga'), 'L') self.assertEqual( RetroSheet.battedball_cd(21, 'Double', 'hoge 2B fly ball to fuga'), 'F') self.assertEqual( RetroSheet.battedball_cd(21, 'Double', 'hoge 2B ground ball to fuga'), 'G') self.assertEqual( RetroSheet.battedball_cd(21, 'Double', 'hoge 2B pop up to fuga'), 'P') self.assertEqual(RetroSheet.battedball_cd(21, 'Double', 'hoge 2B'), '') self.assertEqual( RetroSheet.battedball_cd(22, 'Triple', 'hoge 3B on a line drive to fuga'), 'L') self.assertEqual( RetroSheet.battedball_cd(22, 'Triple', 'hoge 3B fly ball to fuga'), 'F') self.assertEqual( RetroSheet.battedball_cd(22, 'Triple', 'hoge 3B ground ball to fuga'), 'G') self.assertEqual( RetroSheet.battedball_cd(22, 'Triple', 'hoge 3B pop up to fuga'), 'P') self.assertEqual(RetroSheet.battedball_cd(22, 'Triple', 'hoge 3B'), '') self.assertEqual( RetroSheet.battedball_cd(23, 'Home Run', 'hoge 3B on a line drive to fuga'), 'L') self.assertEqual( RetroSheet.battedball_cd(23, 'Home Run', 'hoge 3B fly ball to fuga'), 'F') self.assertEqual( RetroSheet.battedball_cd(23, 'Home Run', 'hoge 3B ground ball to fuga'), 'G') self.assertEqual( RetroSheet.battedball_cd(23, 'Home Run', 'hoge 3B pop up to fuga'), 'P') self.assertEqual(RetroSheet.battedball_cd(23, 'Home Run', 'hoge 3B'), '') # Runner Out self.assertEqual( RetroSheet.battedball_cd(6, 'Runner Out', 'hoge caught stealing'), '') self.assertEqual( RetroSheet.battedball_cd(8, 'Runner Out', 'hoge picks off'), '') # Unknown Event self.assertEqual(RetroSheet.battedball_cd(0, 'bar', 'hoge picks off'), '') self.assertEqual( RetroSheet.battedball_cd(0, 'Runner Out', 'hoge fuga'), '')
def test_event_cd(self): """ Get Event code """ # Generic out self.assertEqual(RetroSheet.event_cd('Flyout', 'hoge fly out'), 2) self.assertEqual(RetroSheet.event_cd('Fly Out', 'hoge fly out'), 2) self.assertEqual(RetroSheet.event_cd('Sac Fly', 'hoge fly out'), 2) self.assertEqual(RetroSheet.event_cd('Sac Fly DP', 'hoge fly out'), 2) self.assertEqual(RetroSheet.event_cd('Lineout', 'hoge line out'), 2) self.assertEqual(RetroSheet.event_cd('Line Out', 'hoge line out'), 2) self.assertEqual(RetroSheet.event_cd('Bunt Lineout', 'hoge line out'), 2) self.assertEqual(RetroSheet.event_cd('Pop Out', 'hoge pop out'), 2) self.assertEqual(RetroSheet.event_cd('Pop out', 'hoge pop out'), 2) self.assertEqual(RetroSheet.event_cd('Bunt Pop out', 'hoge pop out'), 2) self.assertEqual(RetroSheet.event_cd('Groundout', 'hoge ground out'), 2) self.assertEqual(RetroSheet.event_cd('Ground Out', 'hoge ground out'), 2) self.assertEqual(RetroSheet.event_cd('Sac Bunt', 'hoge ground out'), 2) self.assertEqual( RetroSheet.event_cd('Bunt Groundout', 'hoge ground out'), 2) self.assertEqual( RetroSheet.event_cd('Grounded Into DP', 'hoge ground into dp'), 2) self.assertEqual(RetroSheet.event_cd('Forceout', 'hoge force out'), 2) self.assertEqual(RetroSheet.event_cd('Double Play', 'hoge dp'), 2) self.assertEqual(RetroSheet.event_cd('Triple Play', 'hoge tp'), 2) self.assertEqual( RetroSheet.event_cd('Sacrifice Bunt D', 'hoge bunt dp'), 2) # Strike out self.assertEqual(RetroSheet.event_cd('Strikeout', 'hoge strike out'), 3) self.assertEqual( RetroSheet.event_cd('Strikeout - DP', 'hoge strike out dp'), 3) # walk, intent walk ,hit by pitch self.assertEqual(RetroSheet.event_cd('Walk', 'hoge walk'), 14) self.assertEqual( RetroSheet.event_cd('Intent Walk', 'hoge intent walk'), 15) self.assertEqual( RetroSheet.event_cd('Hit By Pitch', 'hoge hit by pitch'), 16) # Interference self.assertEqual( RetroSheet.event_cd('hoge interference', 'hoge interference'), 17) self.assertEqual( RetroSheet.event_cd('Interference fuga', 'hoge interference'), 17) # Error self.assertEqual(RetroSheet.event_cd('hoge error', 'hoge error'), 18) self.assertEqual(RetroSheet.event_cd('fuga Error', 'fuga error'), 18) # Not Error self.assertEqual(RetroSheet.event_cd('error fuga', 'fuga not error'), 0) # Fielder's Choice self.assertEqual( RetroSheet.event_cd('Fielders Choice Out', 'hoge fielders choice'), 19) self.assertEqual( RetroSheet.event_cd('Fielders Choice', 'hoge fielders choice'), 19) # Single, 2B, 3B, HR self.assertEqual(RetroSheet.event_cd('Single', 'hoge Single'), 20) self.assertEqual(RetroSheet.event_cd('Double', 'hoge 2B'), 21) self.assertEqual(RetroSheet.event_cd('Triple', 'hoge 3B'), 22) self.assertEqual(RetroSheet.event_cd('Home Run', 'hoge home run'), 23) # Runner Out self.assertEqual( RetroSheet.event_cd('Runner Out', 'hoge caught stealing'), 6) self.assertEqual(RetroSheet.event_cd('Runner Out', 'hoge picks off'), 8) # Unknown Event self.assertEqual(RetroSheet.event_cd('bar', 'hoge picks off'), 0) self.assertEqual(RetroSheet.event_cd('Runner Out', 'hoge fuga'), 0)
def pa(cls, ab, game, rosters, inning_number, inning_id, out_ct, hit_location): """ plate appearance data :param ab: at bat object(type:Beautifulsoup) :param game: MLBAM Game object :param rosters: Game Rosters :param inning_number: Inning Number :param inning_id: Inning Id(0:home 1:away) :param pitch_list: Pitching :param out_ct: out count :param hit_location: Hitlocation data(dict) :return: { 'retro_game_id': Retrosheet Game id 'game_type': Game Type(S/R/F/D/L/W) 'game_type_des': Game Type Description (Spring Training or Regular Season or Wild-card Game or Divisional Series or LCS or World Series) 'st_fl': Spring Training FLAG(T or F) 'regseason_fl': Regular Season FLAG(T or F) 'playoff_fl': Play Off Flag(T or F) 'local_game_time': Game Time(UTC -5) 'game_id': Game Id 'home_team_id': Home Team Id 'home_team_lg': Home Team league(AL or NL) 'away_team_id': Away Team Id 'away_team_lg': Away Team league(AL or NL) 'home_team_name': Home Team Name 'away_team_name': Away Team Name 'home_team_name_full': Home Team Name(Full Name) 'away_team_name_full': Away Team Name(Full Name) 'interleague_fl': Inter League Flag(T or F) 'park_id': Park Id 'park_name': Park Name 'park_loc': Park Location 'inning_number': Inning Number 'bat_home_id': Batter Id 'outs_ct': Out count 'pit_mlbid': Pitcher Id 'pit_first_name': Pitcher First Name 'pit_last_name': Pitcher Last Name 'pit_box_name': Pitcher Box name 'pit_hand_cd': Pitcher Throw Hand(R or L) 'bat_first_name': Batter First Name 'bat_last_name': Batter Last Name 'bat_box_name': Batter Box name 'ab_number': At Bat Sequence Number in Game 'start_bases': Bases(Before At Bat) (___, 1__, 12_, 123, etc...) 'end_bases': Bases(After At Bat) (___, 1__, 12_, 123, etc...) 'event_outs_ct': Event Out Count 'ab_des': At Bat Description 'event_tx': Event Text 'event_cd': Event Code for Retrosheet http://www.retrosheet.org/datause.txt 'hit_x': Hit Location(x) 'hit_y': Hit Location(y) 'event_num': Event Sequence Number(atbat, pitch, action) 'home_team_runs': Score(Home) 'away_team_runs': Score(Away) } """ ab_des = MlbamUtil.get_attribute_stats(ab, 'des', str, MlbamConst.UNKNOWN_FULL) event_tx = MlbamUtil.get_attribute_stats(ab, 'event', str, MlbamConst.UNKNOWN_FULL) event_cd = RetroSheet.event_cd(event_tx, ab_des) event_outs_ct = MlbamUtil.get_attribute_stats(ab, 'o', int, 0) start_bases, end_bases = cls._get_bases(ab) pit_mlbid = MlbamUtil.get_attribute_stats(ab, 'pitcher', str, MlbamConst.UNKNOWN_FULL) bat_mlbid = MlbamUtil.get_attribute_stats(ab, 'batter', str, MlbamConst.UNKNOWN_FULL) pit_player = rosters.get(pit_mlbid) bat_player = rosters.get(bat_mlbid) location_key = Inning.HITLOCATION_KEY_FORMAT.format( inning=inning_number, des=event_tx, pitcher=pit_mlbid, batter=bat_mlbid, ) location = hit_location.get(location_key, {}) atbat = OrderedDict() atbat['retro_game_id'] = game.retro_game_id atbat['year'] = game.timestamp.year atbat['month'] = game.timestamp.month atbat['day'] = game.timestamp.day atbat['st_fl'] = game.st_fl atbat['regseason_fl'] = game.regseason_fl atbat['playoff_fl'] = game.playoff_fl atbat['game_type'] = game.game_type atbat['game_type_des'] = game.game_type_des atbat['local_game_time'] = game.local_game_time atbat['game_id'] = game.game_id atbat['home_team_id'] = game.home_team_id atbat['away_team_id'] = game.away_team_id atbat['home_team_lg'] = game.home_team_lg atbat['away_team_lg'] = game.away_team_lg atbat['interleague_fl'] = game.interleague_fl atbat['park_id'] = game.park_id atbat['park_name'] = game.park_name atbat['park_location'] = game.park_loc atbat['inning_number'] = inning_number atbat['bat_home_id'] = inning_id atbat['outs_ct'] = out_ct atbat['pit_mlbid'] = pit_mlbid atbat['pit_first_name'] = pit_player.first atbat['pit_last_name'] = pit_player.last atbat['pit_box_name'] = pit_player.box_name atbat['pit_hand_cd'] = MlbamUtil.get_attribute_stats( ab, 'p_throws', str, MlbamConst.UNKNOWN_FULL) atbat['bat_mlbid'] = bat_mlbid atbat['bat_first_name'] = bat_player.first atbat['bat_last_name'] = bat_player.last atbat['bat_box_name'] = bat_player.box_name atbat['bat_hand_cd'] = MlbamUtil.get_attribute_stats( ab, 'stand', str, MlbamConst.UNKNOWN_FULL) atbat['ab_number'] = MlbamUtil.get_attribute_stats( ab, 'num', int, None) atbat['start_bases'] = start_bases atbat['end_bases'] = end_bases atbat['event_outs_ct'] = event_outs_ct atbat['ab_des'] = ab_des atbat['event_tx'] = event_tx atbat['event_cd'] = event_cd atbat['hit_x'] = location.get('hit_x', None) atbat['hit_y'] = location.get('hit_y', None) atbat['event_num'] = MlbamUtil.get_attribute_stats( ab, 'event_num', int, -1) atbat['home_team_runs'] = MlbamUtil.get_attribute_stats( ab, 'home_team_runs', int, 0) atbat['away_team_runs'] = MlbamUtil.get_attribute_stats( ab, 'away_team_runs', int, 0) return atbat