def test_nba_boxscore_string_representation(self): expected = ('Boxscore for Detroit Pistons at Los Angeles Lakers ' '(10:30 PM, October 31, 2017)') boxscore = Boxscore(BOXSCORE) assert boxscore.__repr__() == expected
def test_nba_boxscore_string_representation(self): expected = ('Boxscore for Houston Rockets at Utah Jazz ' '(9:00 PM, February 22, 2020)') boxscore = Boxscore(BOXSCORE) assert boxscore.__repr__() == expected
def test_invalid_url_yields_empty_class(self): flexmock(Boxscore) \ .should_receive('_retrieve_html_page') \ .and_return(None) boxscore = Boxscore(BOXSCORE) for key, value in boxscore.__dict__.items(): if key == '_uri': continue assert value is None
def get_day_stats(date, games_dict): df = pd.DataFrame() for i in range(len(games_dict[date])): stats = Boxscore(games_dict[date][i]['boxscore']) game_df = pd.DataFrame(games_dict[date][i], index=[0]) away_team, home_team = game_data(game_df, stats) away_team['date'] = pd.to_datetime(away_team['date'], format='%I:%M %p, %B %d, %Y') home_team['date'] = pd.to_datetime(home_team['date'], format='%I:%M %p, %B %d, %Y') df = pd.concat([df, away_team]) df = pd.concat([df, home_team]) return df
def test_game_summary_with_no_scores_returns_none(self): result = Boxscore(None)._parse_summary( pq("""<table id="line_score"> <tbody> <tr> <td class="center"></td> <td class="center"></td> </tr> <tr> <td class="center"></td> <td class="center"></td> </tr> </tbody> </table>""")) assert result == {'away': [None], 'home': [None]}
def setup_method(self, *args, **kwargs): self.results = { 'date': '10:30 PM, October 31, 2017', 'location': 'STAPLES Center, Los Angeles, California', 'winner': HOME, 'winning_name': 'Los Angeles Lakers', 'winning_abbr': 'LAL', 'losing_name': 'Detroit Pistons', 'losing_abbr': 'DET', 'pace': 97.4, 'away_wins': 5, 'away_losses': 3, 'away_minutes_played': 240, 'away_field_goals': 41, 'away_field_goal_attempts': 94, 'away_field_goal_percentage': .436, 'away_two_point_field_goals': 31, 'away_two_point_field_goal_attempts': 61, 'away_two_point_field_goal_percentage': .508, 'away_three_point_field_goals': 10, 'away_three_point_field_goal_attempts': 33, 'away_three_point_field_goal_percentage': .303, 'away_free_throws': 1, 'away_free_throw_attempts': 3, 'away_free_throw_percentage': .333, 'away_offensive_rebounds': 10, 'away_defensive_rebounds': 34, 'away_total_rebounds': 44, 'away_assists': 21, 'away_steals': 7, 'away_blocks': 3, 'away_turnovers': 12, 'away_personal_fouls': 11, 'away_points': 93, 'away_true_shooting_percentage': .488, 'away_effective_field_goal_percentage': .489, 'away_three_point_attempt_rate': .351, 'away_free_throw_attempt_rate': .032, 'away_offensive_rebound_percentage': 19.2, 'away_defensive_rebound_percentage': 75.6, 'away_total_rebound_percentage': 45.4, 'away_assist_percentage': 51.2, 'away_steal_percentage': 7.2, 'away_block_percentage': 4.6, 'away_turnover_percentage': 11.2, 'away_offensive_rating': 95.5, 'away_defensive_rating': 116.0, 'home_wins': 3, 'home_losses': 4, 'home_minutes_played': 240, 'home_field_goals': 45, 'home_field_goal_attempts': 91, 'home_field_goal_percentage': .495, 'home_two_point_field_goals': 33, 'home_two_point_field_goal_attempts': 65, 'home_two_point_field_goal_percentage': .508, 'home_three_point_field_goals': 12, 'home_three_point_field_goal_attempts': 26, 'home_three_point_field_goal_percentage': .462, 'home_free_throws': 11, 'home_free_throw_attempts': 14, 'home_free_throw_percentage': .786, 'home_offensive_rebounds': 11, 'home_defensive_rebounds': 42, 'home_total_rebounds': 53, 'home_assists': 30, 'home_steals': 9, 'home_blocks': 5, 'home_turnovers': 14, 'home_personal_fouls': 14, 'home_points': 113, 'home_true_shooting_percentage': .582, 'home_effective_field_goal_percentage': .560, 'home_three_point_attempt_rate': .286, 'home_free_throw_attempt_rate': .154, 'home_offensive_rebound_percentage': 24.4, 'home_defensive_rebound_percentage': 80.8, 'home_total_rebound_percentage': 54.6, 'home_assist_percentage': 66.7, 'home_steal_percentage': 9.2, 'home_block_percentage': 8.2, 'home_turnover_percentage': 12.6, 'home_offensive_rating': 116.0, 'home_defensive_rating': 95.5 } flexmock(utils) \ .should_receive('_todays_date') \ .and_return(MockDateTime(YEAR, MONTH)) self.boxscore = Boxscore(BOXSCORE)
def setup_method(self, *args, **kwargs): self.results = { 'date': '9:00 PM, February 22, 2020', 'location': 'Vivint Smart Home Arena, Salt Lake City, Utah', 'winner': AWAY, 'winning_name': 'Houston Rockets', 'winning_abbr': 'HOU', 'losing_name': 'Utah Jazz', 'losing_abbr': 'UTA', 'pace': 103.2, 'away_wins': 36, 'away_losses': 20, 'away_minutes_played': 240, 'away_field_goals': 45, 'away_field_goal_attempts': 92, 'away_field_goal_percentage': .489, 'away_two_point_field_goals': 25, 'away_two_point_field_goal_attempts': 44, 'away_two_point_field_goal_percentage': .568, 'away_three_point_field_goals': 20, 'away_three_point_field_goal_attempts': 48, 'away_three_point_field_goal_percentage': .417, 'away_free_throws': 10, 'away_free_throw_attempts': 14, 'away_free_throw_percentage': .714, 'away_offensive_rebounds': 6, 'away_defensive_rebounds': 36, 'away_total_rebounds': 42, 'away_assists': 20, 'away_steals': 9, 'away_blocks': 5, 'away_turnovers': 10, 'away_personal_fouls': 25, 'away_points': 120, 'away_true_shooting_percentage': .611, 'away_effective_field_goal_percentage': .598, 'away_three_point_attempt_rate': .522, 'away_free_throw_attempt_rate': .152, 'away_offensive_rebound_percentage': 13.0, 'away_defensive_rebound_percentage': 85.7, 'away_total_rebound_percentage': 47.7, 'away_assist_percentage': 44.4, 'away_steal_percentage': 8.7, 'away_block_percentage': 8.5, 'away_turnover_percentage': 9.2, 'away_offensive_rating': 116.3, 'away_defensive_rating': 106.6, 'home_wins': 0, 'home_losses': 0, 'home_minutes_played': 240, 'home_field_goals': 42, 'home_field_goal_attempts': 90, 'home_field_goal_percentage': .467, 'home_two_point_field_goals': 35, 'home_two_point_field_goal_attempts': 59, 'home_two_point_field_goal_percentage': .593, 'home_three_point_field_goals': 7, 'home_three_point_field_goal_attempts': 31, 'home_three_point_field_goal_percentage': .226, 'home_free_throws': 19, 'home_free_throw_attempts': 24, 'home_free_throw_percentage': .792, 'home_offensive_rebounds': 6, 'home_defensive_rebounds': 40, 'home_total_rebounds': 46, 'home_assists': 18, 'home_steals': 1, 'home_blocks': 2, 'home_turnovers': 13, 'home_personal_fouls': 14, 'home_points': 110, 'home_true_shooting_percentage': .547, 'home_effective_field_goal_percentage': .506, 'home_three_point_attempt_rate': .344, 'home_free_throw_attempt_rate': .267, 'home_offensive_rebound_percentage': 14.3, 'home_defensive_rebound_percentage': 87.0, 'home_total_rebound_percentage': 52.3, 'home_assist_percentage': 42.9, 'home_steal_percentage': 1.0, 'home_block_percentage': 4.5, 'home_turnover_percentage': 11.4, 'home_offensive_rating': 106.6, 'home_defensive_rating': 116.3 } flexmock(utils) \ .should_receive('_todays_date') \ .and_return(MockDateTime(YEAR, MONTH)) self.boxscore = Boxscore(BOXSCORE)
def main(): file_path = os.getcwd() with open(file_path + '/nbaScraperModelData.csv', "r") as f: last_line = f.readlines()[-1].strip().split(",") last_recorded_date = last_line[-1] datetime_object = datetime.strptime(last_recorded_date, '%I:%M%p, %B %d, %Y') start_date = datetime_object + timedelta(days=1) start_date = date(2021, 10, 19) end_date = date.today() game_dates = Boxscores(start_date, end_date) for date_key in game_dates.games.keys(): for game_key in game_dates.games[date_key]: game_df = Boxscore(game_key['boxscore']) game_Stats = {} # winning team (not always home) -- home team on top winningTeam = game_df.winner if winningTeam == 'Home': game_Stats['Home'] = game_df.winning_name game_Stats['winningScore'] = game_df.home_points game_Stats['losingScore'] = game_df.away_points else: game_Stats['Home'] = game_df.losing_name game_Stats['winningScore'] = game_df.away_points game_Stats['losingScore'] = game_df.home_points game_Stats['winningTeam'] = game_df.winning_name game_Stats['losingTeam'] = game_df.losing_name game_Stats['Pace_Home'] = game_df.pace game_Stats['Pace_AWAY'] = game_df.pace game_Stats[ 'FG_PpercentHome'] = game_df.home_effective_field_goal_percentage game_Stats[ 'FG_PpercentAWAY'] = game_df.away_effective_field_goal_percentage game_Stats['TOV_Home'] = game_df.home_turnovers game_Stats['TOV_AWAY'] = game_df.away_turnovers game_Stats['ORB_Home'] = game_df.home_offensive_rebounds game_Stats['ORB_AWAY'] = game_df.away_offensive_rebounds game_Stats['FT_Rate_Home'] = game_df.home_free_throw_attempt_rate game_Stats['FT_Rate_AWAY'] = game_df.away_free_throw_attempt_rate game_Stats['Off_Rat_Home'] = game_df.home_offensive_rating game_Stats['Off_Rat_AWAY'] = game_df.away_offensive_rating # get date game_Stats['date'] = game_df.date df = pd.DataFrame(game_Stats, index=[0]) df.to_csv(file_path + '/nbaScrapermodelData.csv', mode='a', header=False)
def setup_method(self, *args, **kwargs): flexmock(Boxscore) \ .should_receive('_parse_game_data') \ .and_return(None) self.boxscore = Boxscore(None)
class TestNBABoxscore: @patch('requests.get', side_effect=mock_pyquery) def setup_method(self, *args, **kwargs): flexmock(Boxscore) \ .should_receive('_parse_game_data') \ .and_return(None) self.boxscore = Boxscore(None) def test_away_team_wins(self): fake_away_points = PropertyMock(return_value=75) fake_home_points = PropertyMock(return_value=70) type(self.boxscore)._away_points = fake_away_points type(self.boxscore)._home_points = fake_home_points assert self.boxscore.winner == AWAY def test_home_team_wins(self): fake_away_points = PropertyMock(return_value=70) fake_home_points = PropertyMock(return_value=75) type(self.boxscore)._away_points = fake_away_points type(self.boxscore)._home_points = fake_home_points assert self.boxscore.winner == HOME def test_winning_name_is_home(self): expected_name = 'Home Name' fake_winner = PropertyMock(return_value=HOME) fake_home_name = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._home_name = fake_home_name assert self.boxscore.winning_name == expected_name def test_winning_name_is_away(self): expected_name = 'Away Name' fake_winner = PropertyMock(return_value=AWAY) fake_away_name = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._away_name = fake_away_name assert self.boxscore.winning_name == expected_name def test_winning_abbr_is_home(self): expected_name = 'HOME' flexmock(utils) \ .should_receive('_parse_abbreviation') \ .and_return(expected_name) fake_winner = PropertyMock(return_value=HOME) fake_home_abbr = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._home_abbr = fake_home_abbr assert self.boxscore.winning_abbr == expected_name def test_winning_abbr_is_away(self): expected_name = 'AWAY' flexmock(utils) \ .should_receive('_parse_abbreviation') \ .and_return(expected_name) fake_winner = PropertyMock(return_value=AWAY) fake_away_abbr = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._away_abbr = fake_away_abbr assert self.boxscore.winning_abbr == expected_name def test_losing_name_is_home(self): expected_name = 'Home Name' fake_winner = PropertyMock(return_value=AWAY) fake_home_name = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._home_name = fake_home_name assert self.boxscore.losing_name == expected_name def test_losing_name_is_away(self): expected_name = 'Away Name' fake_winner = PropertyMock(return_value=HOME) fake_away_name = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._away_name = fake_away_name assert self.boxscore.losing_name == expected_name def test_losing_abbr_is_home(self): expected_name = 'HOME' flexmock(utils) \ .should_receive('_parse_abbreviation') \ .and_return(expected_name) fake_winner = PropertyMock(return_value=AWAY) fake_home_abbr = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._home_abbr = fake_home_abbr assert self.boxscore.losing_abbr == expected_name def test_losing_abbr_is_away(self): expected_name = 'AWAY' flexmock(utils) \ .should_receive('_parse_abbreviation') \ .and_return(expected_name) fake_winner = PropertyMock(return_value=HOME) fake_away_abbr = PropertyMock(return_value=MockName(expected_name)) type(self.boxscore).winner = fake_winner type(self.boxscore)._away_abbr = fake_away_abbr assert self.boxscore.losing_abbr == expected_name def test_invalid_away_record_returns_default_wins(self): fake_record = PropertyMock(return_value='Golden State Warriors 1') type(self.boxscore)._away_record = fake_record assert self.boxscore.away_wins == 0 def test_invalid_away_record_returns_default_losses(self): fake_record = PropertyMock(return_value='Golden State Warriors 1') type(self.boxscore)._away_record = fake_record assert self.boxscore.away_losses == 0 def test_invalid_home_record_returns_default_wins(self): fake_record = PropertyMock(return_value='Golden State Warriors 1') type(self.boxscore)._home_record = fake_record assert self.boxscore.home_wins == 0 def test_invalid_home_record_returns_default_losses(self): fake_record = PropertyMock(return_value='Golden State Warriors 1') type(self.boxscore)._home_record = fake_record assert self.boxscore.home_losses == 0 def test_away_two_point_field_goals_calc(self): fake_none = PropertyMock(return_value=None) fake_int = PropertyMock(return_value=5) type(self.boxscore)._away_field_goals = fake_none type(self.boxscore)._away_three_point_field_goals = fake_none assert self.boxscore.away_two_point_field_goals is None type(self.boxscore)._away_three_point_field_goals = fake_int assert self.boxscore.away_two_point_field_goals is None type(self.boxscore)._away_field_goals = fake_int type(self.boxscore)._away_three_point_field_goals = fake_none assert self.boxscore.away_two_point_field_goals is None type(self.boxscore)._away_field_goals = fake_int type(self.boxscore)._away_three_point_field_goals = fake_int assert isinstance(self.boxscore.away_two_point_field_goals, int) def test_away_two_point_field_goal_attempts_calc(self): fake_none = PropertyMock(return_value=None) fake_int = PropertyMock(return_value=5) type(self.boxscore)._away_field_goal_attempts = fake_none type(self.boxscore)._away_three_point_field_goal_attempts = fake_none assert self.boxscore.away_two_point_field_goal_attempts is None type(self.boxscore)._away_three_point_field_goal_attempts = fake_int assert self.boxscore.away_two_point_field_goal_attempts is None type(self.boxscore)._away_field_goal_attempts = fake_int type(self.boxscore)._away_three_point_field_goal_attempts = fake_none assert self.boxscore.away_two_point_field_goal_attempts is None type(self.boxscore)._away_field_goal_attempts = fake_int type(self.boxscore)._away_three_point_field_goal_attempts = fake_int assert isinstance(self.boxscore.away_two_point_field_goal_attempts, int) def test_away_two_point_field_goal_percentage_calc(self): fake_none = PropertyMock(return_value=None) fake_int = PropertyMock(return_value=5) type(self.boxscore).away_two_point_field_goals = fake_none type(self.boxscore).away_two_point_field_goal_attempts = fake_none assert self.boxscore.away_two_point_field_goal_percentage is None type(self.boxscore).away_two_point_field_goal_attempts = fake_int assert self.boxscore.away_two_point_field_goal_percentage is None type(self.boxscore).away_two_point_field_goals = fake_int type(self.boxscore).away_two_point_field_goal_attempts = fake_none assert self.boxscore.away_two_point_field_goal_percentage is None type(self.boxscore).away_two_point_field_goals = fake_int type(self.boxscore).away_two_point_field_goal_attempts = fake_int assert isinstance(self.boxscore.away_two_point_field_goal_percentage, float) def test_home_to_point_field_goals_calc(self): fake_none = PropertyMock(return_value=None) fake_int = PropertyMock(return_vzalue=5) type(self.boxscore)._home_field_goals = fake_none type(self.boxscore)._home_three_point_field_goals = fake_none assert self.boxscore.home_two_point_field_goals is None type(self.boxscore)._home_three_point_field_goals = fake_int assert self.boxscore.home_two_point_field_goals is None type(self.boxscore)._home_field_goals = fake_int type(self.boxscore)._home_three_point_field_goals = fake_none assert self.boxscore.home_two_point_field_goals is None type(self.boxscore)._home_field_goals = fake_int type(self.boxscore)._home_three_point_field_goals = fake_int assert isinstance(self.boxscore.home_two_point_field_goals, int) def test_home_two_point_field_goal_attempts_calc(self): fake_none = PropertyMock(return_value=None) fake_int = PropertyMock(return_value=5) type(self.boxscore)._home_field_goal_attempts = fake_none type(self.boxscore)._home_three_point_field_goal_attempts = fake_none assert self.boxscore.home_two_point_field_goal_attempts is None type(self.boxscore)._home_three_point_field_goal_attempts = fake_int assert self.boxscore.home_two_point_field_goal_attempts is None type(self.boxscore)._home_field_goal_attempts = fake_int type(self.boxscore)._home_three_point_field_goal_attempts = fake_none assert self.boxscore.home_two_point_field_goal_attempts is None type(self.boxscore)._home_field_goal_attempts = fake_int type(self.boxscore)._home_three_point_field_goal_attempts = fake_int assert isinstance(self.boxscore.home_two_point_field_goal_attempts, int) def test_home_two_point_field_goal_percentage_calc(self): fake_none = PropertyMock(return_value=None) fake_int = PropertyMock(return_value=5) type(self.boxscore).home_two_point_field_goals = fake_none type(self.boxscore).home_two_point_field_goal_attempts = fake_none assert self.boxscore.home_two_point_field_goal_percentage is None type(self.boxscore).home_two_point_field_goal_attempts = fake_int assert self.boxscore.home_two_point_field_goal_percentage is None type(self.boxscore).home_two_point_field_goals = fake_int type(self.boxscore).home_two_point_field_goal_attempts = fake_none assert self.boxscore.home_two_point_field_goal_percentage is None type(self.boxscore).home_two_point_field_goals = fake_int type(self.boxscore).home_two_point_field_goal_attempts = fake_int assert isinstance(self.boxscore.home_two_point_field_goal_percentage, float) def test_game_summary_with_no_scores_returns_none(self): result = Boxscore(None)._parse_summary( pq("""<table id="line_score"> <tbody> <tr> <td class="center"></td> <td class="center"></td> </tr> <tr> <td class="center"></td> <td class="center"></td> </tr> </tbody> </table>""")) assert result == {'away': [None], 'home': [None]} def test_invalid_url_returns_none(self): result = Boxscore(None)._retrieve_html_page('') assert result is None def test_no_class_information_returns_dataframe_of_none(self): mock_points = PropertyMock(return_value=None) type(self.boxscore)._away_points = mock_points type(self.boxscore)._home_points = mock_points assert self.boxscore.dataframe is None def test_nba_game_info(self): fields = { 'date': '7:30 PM, November 9, 2018', 'location': 'State Farm Arena, Atlanta, Georgia' } mock_field = """7:30 PM, November 9, 2018 State Farm Arena, Atlanta, Georgia Logos via Sports Logos.net / About logos """ m = MockBoxscoreData(MockField(mock_field)) for field, value in fields.items(): result = self.boxscore._parse_game_date_and_location(field, m) assert value == result def test_nba_partial_game_info(self): fields = {'date': '7:30 PM, November 9, 2018', 'location': None} mock_field = """7:30 PM, November 9, 2018 Logos via Sports Logos.net / About logos""" m = MockBoxscoreData(MockField(mock_field)) for field, value in fields.items(): result = self.boxscore._parse_game_date_and_location(field, m) assert value == result
def test_invalid_url_returns_none(self): result = Boxscore(None)._retrieve_html_page('') assert result is None
def boxscore(self): """ Returns an instance of the Boxscore class containing more detailed stats on the game. """ return Boxscore(self._boxscore)