def test_merge(self): boggs = Player("Wade Boggs", 26, 1, "P", LEFT) boggs2 = Player("Wade Boggs", 26, 1, "P", LEFT) self.assertEqual(boggs, boggs2) boggs2.number = None boggs.merge(boggs2) self.assertEqual(boggs.number, 26)
def load(self, remove_errors=True): with open( os.path.join(self.cache_path, "gc_{}.json".format(self.gameid)), 'r') as f: d = json.load(f, object_pairs_hook=OrderedDict) self._away_roster = PlayerList() self._home_roster = PlayerList() skip = ["bat_stats", "pitch_stats", "name"] for playerd in d["home_lineup"]: p = Player(playerd["name"]) for key, val in playerd.items(): if key not in skip: setattr(p, key, val) self._home_roster.append(p) for playerd in d["away_lineup"]: p = Player(playerd["name"]) for key, val in playerd.items(): if key not in skip: setattr(p, key, val) self._away_roster.append(p) for playerd in d["home_roster"]: p = Player(playerd["name"]) for key, val in playerd.items(): if key not in skip: setattr(p, key, val) self._home_roster.append(p) for playerd in d["away_roster"]: p = Player(playerd["name"]) for key, val in playerd.items(): if key not in skip: setattr(p, key, val) self._away_roster.append(p) self._away_roster_share = copy.copy(self._away_roster) self._home_roster_share = copy.copy(self._home_roster) self.gameid = d["gameid"] self.url = d.get("url") self.home_team = d["home_team"] self.away_team = d["away_team"] self.list_of_halfs = d["list_of_halfs"] if remove_errors: for h in self.list_of_halfs: for e in h: if ERROR_KEY in e: del (e[ERROR_KEY]) self.current_half = self.list_of_halfs[0]
def test_complete(self): self.lineup.add_player(Player("Wade Boggs", 26, 1, "3B", LEFT)) self.lineup.add_player(Player("Dwight Evans", 24, 2, "RF", RIGHT)) self.lineup.add_player(Player("Jim Rice", 14, 3, "LF", RIGHT)) self.lineup.add_player(Player("Mike Easler", 22, 4, "DH", LEFT)) self.lineup.add_player(Player("Tony Armas", 20, 5, "CF", RIGHT)) self.lineup.add_player(Player("Bill Buckner", 6, 6, "1B", LEFT)) self.lineup.add_player(Player("Rich Gedman", 10, 7, "C", LEFT)) self.lineup.add_player(Player("Marty Barret", 17, 8, "2B", RIGHT)) self.lineup.add_player(Player("Jackie Gutierrez", 41, 9, "SS", RIGHT)) self.lineup.add_player(Player("Oil Can Boyd", 41, 10, "P", RIGHT)) self.assertTrue(self.lineup.is_complete())
def setUp(self): self.lineup = lineup.Lineup() self.lineup.add_player(Player("Wade Boggs", 26, 1, "3B", LEFT)) self.lineup.add_player(Player("Dwight Evans", 24, 2, "RF", RIGHT)) self.lineup.add_player(Player("Jim Rice", 14, 3, "LF", RIGHT)) self.lineup.add_player(Player("Mike Easler", 'X', 4, "DH", LEFT)) # test numberless 'X' self.lineup.add_player(Player("Tony Armas", 20, 5, "CF", RIGHT)) self.lineup.add_player(Player("Bill Buckner", 6, 6, "1B", LEFT)) self.lineup.add_player(Player("Rich Gedman", 10, 7, "C", LEFT)) self.lineup.add_player(Player("Marty Barret", 17, 8, "2B", RIGHT)) self.lineup.add_player(Player("Jackie Gutierrez", 41, 9, "SS", RIGHT)) self.lineup.add_player(Player("Oil Can Boyd", 41, 10, "P", RIGHT))
def _update_html_player_table(self, is_home, batting, table): """ helper method to build player lists parsed from the html pages. """ starting_pitcher = True if is_home: current_list = self._home_html_player_list else: current_list = self._away_html_player_list for t in table.find_all("tr"): if t.a is not None: # number, name, P AB R H RBI BB SO AVG #[u'2', u'Campbell, D', u'SS', u'4', u'0', u'0', u'0', u'0', u'1', u'.302'] table_values = [td.text for td in t.find_all("td")] player_num = table_values[0] player_name = table_values[1] player_id = t.a.attrs.get("href").split('=')[1] if batting: starter = player_name[0] != u"\xa0" positions = [p for p in t.find_all("td")[2].childGenerator() if type(p) is not Tag] player_position = positions[0] player = Player(player_name, player_num, position=player_position, iddict={"pointstreak": player_id}, starter=starter) # starting means no indent in lineup player.all_positions = positions player.verify_bat_stats = dict(AB=int(table_values[3]), R=int(table_values[4]), H=int(table_values[5]), RBI=int(table_values[6]), BB=int(table_values[7]), SO=int(table_values[8]), AVG=float(table_values[9]) ) current_list.update_player(player) if starter: curmax = current_list.max_order() if curmax is None: player.order = 1 else: player.order = curmax + 1 else: player = Player(player_name, player_num, position='P', iddict={"pointstreak": player_id}) player.verify_pitch_stats = dict(IP=float(table_values[2]), H=int(table_values[3]), R=int(table_values[4]), ER=int(table_values[5]), BB=int(table_values[6]), SO=int(table_values[7]), ERA=float(table_values[8]) ) if starting_pitcher: player.starter = True starting_pitcher = False current_list.update_player(player)
def test_incomplete(self): self.lineup.add_player(Player("Wade Boggs", 26, 1, "3B", LEFT)) self.assertFalse(self.lineup.is_complete()) self.lineup.add_player(Player("Dwight Evans", 24, 2, "RF", RIGHT)) self.lineup.add_player(Player("Jim Rice", 14, 3, "LF", RIGHT)) self.lineup.add_player(Player("Mike Easler", 7, 4, "DH", LEFT)) self.lineup.add_player(Player("Tony Armas", 20, 5, "CF", RIGHT)) self.lineup.add_player(Player("Bill Buckner", 6, 6, "1B", LEFT)) self.lineup.add_player(Player("Rich Gedman", 10, 7, "C", LEFT)) self.lineup.add_player(Player("Marty Barret", 17, 8, "2B", RIGHT)) self.lineup.add_player(Player("Jackie Gutierrez", 41, 9, "SS", RIGHT)) self.assertFalse(self.lineup.is_complete()) self.assertRaises(LineupError, self.lineup.is_complete, raise_reason=True)
def _make_pitcher(self, player_dict, team_id=None): new_player = Player( player_dict.get("Name"), player_dict.get("Number"), player_dict.get("Order"), constants.P, player_dict.get("Hand"), iddict={"pointstreak": player_dict.get("PlayerId")}, team_id=team_id) if new_player.name is not None: return new_player else: return None
def _make_players(self, player_dict_list, team_id=None): out = PlayerList() for player_dict in player_dict_list: try: new_player = Player( player_dict.get("Name"), player_dict.get("Number"), player_dict.get("Order"), player_dict.get("Position"), player_dict.get("Hand"), iddict={"pointstreak": player_dict.get("PlayerId")}, team_id=team_id) if new_player.name is not None: out.add_player(new_player) except (AttributeError, KeyError): logger.error("making Player from dict = %s" % player_dict) raise return out
def test_merge_keep_longer_name(self): boggs = Player("Wade Boggs", 26, 1, "P", LEFT) boggs2 = Player("Boggs, W", 26, 1, "P", LEFT) boggs.merge(boggs2) self.assertEqual(boggs.name, "Wade Boggs")
def test_closest_match2(self): test = Player("Boggs, W", None, None, None, None) result = test.find_closest_name(self.lineup) self.assertEqual(result, self.lineup.find_player_by_number(26))
def test_closest_match(self): test = Player("Wade Boggs", None, 1, None, LEFT) result = test.find_closest_name(self.lineup) self.assertEqual(result, self.lineup.find_player_by_number(26))
def _update_html_player_table(self, is_home, batting, table): """ helper method to build player lists parsed from the html pages. """ starting_pitcher = True if is_home: current_list = self._home_html_player_list else: current_list = self._away_html_player_list for t in table.find_all("tr"): if t.a is not None: # number, name, P AB R H RBI BB SO AVG #[u'2', u'Campbell, D', u'SS', u'4', u'0', u'0', u'0', u'0', u'1', u'.302'] table_values = [td.text for td in t.find_all("td")] player_num = table_values[0] player_name = table_values[1] player_id = t.a.attrs.get("href").split('=')[1] if batting: starter = player_name[0] != u"\xa0" positions = [ p for p in t.find_all("td")[2].childGenerator() if type(p) is not Tag ] player_position = positions[0] player = Player( player_name, player_num, position=player_position, iddict={"pointstreak": player_id}, starter=starter) # starting means no indent in lineup player.all_positions = positions player.verify_bat_stats = dict(AB=int(table_values[3]), R=int(table_values[4]), H=int(table_values[5]), RBI=int(table_values[6]), BB=int(table_values[7]), SO=int(table_values[8]), AVG=float(table_values[9])) current_list.update_player(player) if starter: curmax = current_list.max_order() if curmax is None: player.order = 1 else: player.order = curmax + 1 else: player = Player(player_name, player_num, position='P', iddict={"pointstreak": player_id}) player.verify_pitch_stats = dict(IP=float(table_values[2]), H=int(table_values[3]), R=int(table_values[4]), ER=int(table_values[5]), BB=int(table_values[6]), SO=int(table_values[7]), ERA=float( table_values[8])) if starting_pitcher: player.starter = True starting_pitcher = False current_list.update_player(player)
def test_move(self): self.lineup.add_player(Player("Wade Boggs", 26, 1, "3B", LEFT)) self.lineup.move_player("Wade Boggs", "LF") self.assertEqual( self.lineup.find_player_by_position("LF").name, "Wade Boggs")
def test_update_to_add(self): self.lineup.add_player(Player("Wade Boggs", 26, 1, "P", LEFT)) self.lineup.update_player(Player("Bill Buckner", 6, 6, "1B", LEFT)) self.assertEquals(2, len(self.lineup))
def test_update_error(self): self.lineup.add_player(Player("Wade Boggs", 26, 1, "P", LEFT)) self.lineup.update_player(Player("Wade Boggs", 26, 1, "3B", LEFT)) self.assertRaises(StandardError, self.lineup.update_player, Player(None, 26, 1, "3B", LEFT))
def test_update_position(self): self.lineup.add_player(Player("Wade Boggs", 26, 1, "3B", LEFT)) self.lineup.update_position(Player("Big Bird", 26, None, "3B", LEFT)) self.assertEqual( self.lineup.find_player_by_position("3B").name, "Big Bird") self.assertEqual(self.lineup.find_player_by_order(1).name, "Big Bird")
def test_update(self): self.lineup.add_player(Player("Wade Boggs", 26, 1, "P", LEFT)) self.lineup.update_player(Player("Wade Boggs", 26, 1, "3B", LEFT)) self.assertEqual( self.lineup.find_player_by_position("3B").name, "Wade Boggs")
def test_name_equals(self): boggs = Player("Wade Boggs", 26, 1, "P", LEFT) self.assertEquals(boggs.name, "wade boggs ")
def test_name_not_equals(self): boggs = Player("Wade Boggs JR", 26, 1, "P", LEFT) self.assertFalse(boggs.name != "wade boggs jr") self.assertTrue(boggs.name == "wade boggs jr")
def test_remove(self): self.lineup.add_player(Player("Wade Boggs", 26, 1, "3B", LEFT)) self.lineup.remove_player("Wade Boggs") self.assertRaises(KeyError, self.lineup.find_player_by_name, "Wade Boggs")
def test_double_add(self): self.assertRaises(LineupError, self.lineup.add_player, Player("Oil Can Boyd", 41, 10, "P", RIGHT))
def test_find_by_position(self): self.lineup.add_player(Player("Wade Boggs", 26, 1, "3B", LEFT)) self.assertEquals("Wade Boggs", self.lineup.find_player_by_position("3B").name)