def test_invalid_url_returns_none(self, *args, **kwargs): mock_id = PropertyMock(return_value='BAD') player = Player(None) type(player)._player_id = mock_id result = player._retrieve_html_page() assert result is None
def test_invalid_url_returns_none(self, *args, **kwargs): flexmock(Player) \ .should_receive('_build_url') \ .and_return('') result = Player(None)._retrieve_html_page() assert result is None
def test_no_player_data_returns_no_stats(self): flexmock(Player) \ .should_receive('_retrieve_html_page') \ .and_return(None) stats = Player(None)._pull_player_data() assert stats is None
def main(request): request_json = request.get_json() if request_json and "firstname" in request_json and "lastname" in request_json: firstname, lastname = request_json["firstname"], request_json[ "lastname"] else: return None code = lastname[:5] + firstname[:2] NN = 1 player = Player(code + str(0) + str(NN)) # changes names like P.J. Tucker to pj tucker tempname = player.name.lower().translate( str.maketrans('', '', string.punctuation)) while tempname != firstname + " " + lastname: NN += 1 player = Player(code + str(0) + str(NN)) data = { "name": player.name, "url": "https://www.basketball-reference.com/players/" + str(lastname[0]) + "/" + code + str(0) + str(NN) + ".html", "position": player('2019-20').position, "points": player('2019-20').points, "assists": player('2019-20').assists, "blocks": player('2019-20').blocks, "free_throw_percentage": round(player('2019-20').free_throw_percentage * 100, 3), "three_point_percentage": round(player('2019-20').three_point_percentage * 100, 3), "usage_percentage": round(player('2019-20').usage_percentage, 3), "team": player('2019-20').team_abbreviation } return json.dumps(data)
def test_no_float_returns_default_value_player_class(self): mock_rating = PropertyMock(return_value=['']) mock_index = PropertyMock(return_value=0) player = Player(None) type(player)._player_efficiency_rating = mock_rating type(player)._index = mock_index result = player.player_efficiency_rating assert result is None
def test_no_float_returns_default_value_abstract_class(self): mock_percentage = PropertyMock(return_value=['']) mock_index = PropertyMock(return_value=0) player = Player(None) type(player)._field_goal_percentage = mock_percentage type(player)._index = mock_index result = player.field_goal_percentage assert result is None
def get_player_obj(players): """ Gets a list of bball-reference player ids and returns a sportsreference player object :param players: A list of player ids in string format :return: A list of sportsreference player objects """ player_obj = [] for player in players: player_obj.append(Player(player)) return player_obj
def test_empty_contract_is_none(self): player_info = MockInfo() flexmock(Player) \ .should_receive('_parse_contract_headers') \ .and_return(None) flexmock(Player) \ .should_receive('_parse_contract_wages') \ .and_return(None) flexmock(Player) \ .should_receive('_combine_contract') \ .and_return({}) player = Player(None) result = player._parse_contract(player_info) assert player._contract is None
def __init__(self, id): """ Constructor for PlayerStats class. Arguments ----------- id : string Player's ID. Of the form 'LLLLLFFNN', where 'LLLLL' is the first 5 letters of the player's last name, 'FF' is the first 2 letters of the player's first name, and 'NN' is the index number of the player (usually just 01). """ self.id = id self.player = Player(id)
def main(): global all_teams, averages, players_per for team in all_teams: total = 0 temp_dic = {} counter = 0 for player_id in team: tp = Player(player_id) name = tp.name efficiency = tp('2017-18').player_efficiency_rating temp_dic[name] = efficiency players_per[name] = efficiency if efficiency < 32.5 and efficiency > 0: total += efficiency counter += 1 print(name, efficiency) avg = total/counter averages.append(avg) players_per_by_team.append(temp_dic)
def get_player_data(id): player = Player(id) df = player.dataframe print(df.columns.values) return df
def run(self, ngrams, vars, args): n = vars['player'].split() s = "" if ( len(n[1]) >= 5 ): #edge case for names with shorter than 5 characters/jr. resolved for i in range(5): s += n[1][i] else: for i in range(len(n[1])): s += n[1][i] for i in range(2): s += n[0][i] s += "01" playerid = s.lower() if (n[0] == "Marcus" and n[1] == "Morris"): playerid = "morrima02" player = Player(playerid) position = player.position exp = player.games_played #career stats: average points/rebounds/blocks/etc per 40 minutes C_REB = player.total_rebounds / player.minutes_played * 40 C_PTS = player.points / player.minutes_played * 40 C_AST = player.assists / player.minutes_played * 40 # current year stats: average points/rebounds/blocks/etc per 40 minutes player = player('2019-20') REB = player.total_rebounds / player.minutes_played * 40 #BLK = player.shots_blocked / player.minutes_played * 40 PTS = player.points / player.minutes_played * 40 #FLD_GOAL = player.field_goal_percentage #THR_PT = player.three_point_percentage #TW_PT = player.two_point_percentage AST = player.assists / player.minutes_played * 40 PER = player.player_efficiency_rating str = '' if (PER > 17): vars['goodBadPlayer'] = 'good' if (exp < 246): str = str + "As an unexperienced player, I think " + player.name if (C_PTS >= 16 or C_AST >= 5 or C_REB >= 9): str += " had a great career so far." elif (exp < 500): str = str + "As a player who have some experience, I think " + player.name if (C_PTS >= 16 or C_AST >= 5 or C_REB >= 9): str += " had a great career so far." else: str = str + "As a veteran player, I think " + player.name if (C_PTS >= 16 or C_AST >= 5 or C_REB >= 9): str += " is one of exceptional players that ever played the game." else: str += " had a stable career." if (REB > 4 and PTS > 10 and AST > 5): str = str + vars[ 'receivingTeam'] + ", " + "I think he became the core of the team. And his points, rebounds, and assists reflect that." elif (REB > 7): str = str + "With his rebounding skills, I think the team has really benefited from receiving " + player.name + "." elif (PTS > 12): str = str + "He has been scoring really well making a good contribution to " + vars[ 'receivingTeam'] elif (AST > 5): str = str + "His distribution of ball has really lifted " + vars[ 'receivingTeam'] else: str += "He has been making stable contribution to the team even though his stats don't stand out." str += "And I think his contribution could have stood out more if NBA was not cancelled with pendamic crisis." return str else: vars['goodBadPlayer'] = 'bad' if (PTS <= 12): str += "I don't see much contribution he is making to the team especially with scoring. " elif (position == "C" or position == "PF" and REB <= 4): str += "He's not a good rebounder for his position. " elif (position == "PG" and AST <= 3): str += "He is not that great with his assists to make a contribution to the team. " else: str += "He doesn't have any specialty in points, rebounds, nor assists." str += "I just don't see how he would suddenly get better." if (player('2019-20').minutes_played / player('2019-20').games_played < 12 or player('2019-20').minutes_played == None): str += "Besides, who is this player anyways because I've never heard of him." return str
def setup_method(self, *args, **kwargs): self.results_career = { 'player_id': 'hardeja01', 'season': 'Career', 'name': 'James Harden', 'team_abbreviation': '', 'position': 'SG', 'height': '6-5', 'weight': 220, 'birth_date': datetime(1989, 8, 26), 'nationality': 'United States of America', 'games_played': 687, 'games_started': 474, 'minutes_played': 23239, 'field_goals': 4656, 'field_goal_attempts': 10502, 'field_goal_percentage': .443, 'three_pointers': 1647, 'three_point_attempts': 4523, 'three_point_percentage': .364, 'two_pointers': 3009, 'two_point_attempts': 5979, 'two_point_percentage': .503, 'effective_field_goal_percentage': .522, 'free_throws': 4850, 'free_throw_attempts': 5678, 'free_throw_percentage': .854, 'offensive_rebounds': 516, 'defensive_rebounds': 2966, 'total_rebounds': 3482, 'assists': 4157, 'steals': 1031, 'blocks': 325, 'turnovers': 2383, 'personal_fouls': 1733, 'points': 15809, 'player_efficiency_rating': 23.7, 'true_shooting_percentage': .608, 'three_point_attempt_rate': .431, 'free_throw_attempt_rate': .541, 'offensive_rebound_percentage': 2.5, 'defensive_rebound_percentage': 14.1, 'total_rebound_percentage': 8.4, 'assist_percentage': 30.9, 'steal_percentage': 2.2, 'block_percentage': 1.1, 'turnover_percentage': 15.5, 'usage_percentage': 28.9, 'offensive_win_shares': 80.5, 'defensive_win_shares': 26.2, 'win_shares': 106.7, 'win_shares_per_48_minutes': .220, 'offensive_box_plus_minus': 6.3, 'defensive_box_plus_minus': 0.2, 'box_plus_minus': 6.5, 'value_over_replacement_player': 50.0, 'shooting_distance': 14.7, 'percentage_shots_two_pointers': .569, 'percentage_zero_to_three_footers': .282, 'percentage_three_to_ten_footers': .117, 'percentage_ten_to_sixteen_footers': .068, 'percentage_sixteen_foot_plus_two_pointers': .103, 'percentage_shots_three_pointers': .431, 'field_goal_perc_zero_to_three_feet': .640, 'field_goal_perc_three_to_ten_feet': .331, 'field_goal_perc_ten_to_sixteen_feet': .421, 'field_goal_perc_sixteen_foot_plus_two_pointers': .379, 'two_pointers_assisted_percentage': .199, 'percentage_field_goals_as_dunks': .029, 'dunks': 290, 'three_pointers_assisted_percentage': .514, 'percentage_of_three_pointers_from_corner': .089, 'three_point_shot_percentage_from_corner': .375, 'half_court_heaves': 14, 'half_court_heaves_made': 1, 'point_guard_percentage': 15, 'shooting_guard_percentage': 72, 'small_forward_percentage': 13, 'power_forward_percentage': 0, 'center_percentage': 0, 'on_court_plus_minus': 6.0, 'net_plus_minus': 4.8, 'passing_turnovers': 1236, 'lost_ball_turnovers': 740, 'other_turnovers': 406, 'shooting_fouls': 668, 'blocking_fouls': 25, 'offensive_fouls': 275, 'take_fouls': 27, 'points_generated_by_assists': 9910, 'shooting_fouls_drawn': 2223, 'and_ones': 366, 'shots_blocked': 693, 'salary': 117809888, 'contract': { '2018-19': '$30,431,854', '2019-20': '$37,800,000', '2020-21': '$40,824,000', '2021-22': '$43,848,000', '2022-23': '$46,872,000' } } self.results_2018 = { 'player_id': 'hardeja01', 'season': '2017-18', 'name': 'James Harden', 'team_abbreviation': 'HOU', 'position': 'SG', 'height': '6-5', 'weight': 220, 'birth_date': datetime(1989, 8, 26), 'nationality': 'United States of America', 'games_played': 72, 'games_started': 72, 'minutes_played': 2551, 'field_goals': 651, 'field_goal_attempts': 1449, 'field_goal_percentage': .449, 'three_pointers': 265, 'three_point_attempts': 722, 'three_point_percentage': .367, 'two_pointers': 386, 'two_point_attempts': 727, 'two_point_percentage': .531, 'effective_field_goal_percentage': .541, 'free_throws': 624, 'free_throw_attempts': 727, 'free_throw_percentage': .858, 'offensive_rebounds': 41, 'defensive_rebounds': 348, 'total_rebounds': 389, 'assists': 630, 'steals': 126, 'blocks': 50, 'turnovers': 315, 'personal_fouls': 169, 'points': 2191, 'player_efficiency_rating': 29.8, 'true_shooting_percentage': .619, 'three_point_attempt_rate': .498, 'free_throw_attempt_rate': .502, 'offensive_rebound_percentage': 1.8, 'defensive_rebound_percentage': 15.2, 'total_rebound_percentage': 8.6, 'assist_percentage': 45.1, 'steal_percentage': 2.4, 'block_percentage': 1.7, 'turnover_percentage': 15.1, 'usage_percentage': 36.1, 'offensive_win_shares': 11.6, 'defensive_win_shares': 3.8, 'win_shares': 15.4, 'win_shares_per_48_minutes': .289, 'offensive_box_plus_minus': 9.6, 'defensive_box_plus_minus': 1.3, 'box_plus_minus': 10.9, 'value_over_replacement_player': 8.3, 'shooting_distance': 15.3, 'percentage_shots_two_pointers': .502, 'percentage_zero_to_three_footers': .273, 'percentage_three_to_ten_footers': .118, 'percentage_ten_to_sixteen_footers': .071, 'percentage_sixteen_foot_plus_two_pointers': .039, 'percentage_shots_three_pointers': .498, 'field_goal_perc_zero_to_three_feet': .658, 'field_goal_perc_three_to_ten_feet': .351, 'field_goal_perc_ten_to_sixteen_feet': .437, 'field_goal_perc_sixteen_foot_plus_two_pointers': .368, 'two_pointers_assisted_percentage': .088, 'percentage_field_goals_as_dunks': .013, 'dunks': 19, 'three_pointers_assisted_percentage': .260, 'percentage_of_three_pointers_from_corner': .072, 'three_point_shot_percentage_from_corner': .327, 'half_court_heaves': 2, 'half_court_heaves_made': 0, 'point_guard_percentage': 19, 'shooting_guard_percentage': 75, 'small_forward_percentage': 6, 'power_forward_percentage': 0, 'center_percentage': 0, 'on_court_plus_minus': 10.5, 'net_plus_minus': 5.3, 'passing_turnovers': 188, 'lost_ball_turnovers': 87, 'other_turnovers': 40, 'shooting_fouls': 65, 'blocking_fouls': 2, 'offensive_fouls': 29, 'take_fouls': 5, 'points_generated_by_assists': 1499, 'shooting_fouls_drawn': 273, 'and_ones': 55, 'shots_blocked': 98, 'salary': 28299399, 'contract': { '2018-19': '$30,431,854', '2019-20': '$37,800,000', '2020-21': '$40,824,000', '2021-22': '$43,848,000', '2022-23': '$46,872,000' } } self.player = Player('hardeja01')
def test_nba_player_with_no_stats_handled_without_error(self): player = Player('youngtr01') assert player.name == 'Trae Young'
from sportsreference.nba.roster import Player import matplotlib.pyplot as plt import matplotlib.style as style import numpy as np ##from matplotlib.backends.backend_pdf import PdfPages ##pdf = PdfPages('multipage.pdf') ##fig1 = plt.figure() style.available jharden = Player('hardeja01') points0708 = jharden('2007-08').points points0809 = jharden('2008-09').points points0910 = jharden('2009-10').points points1011 = jharden('2010-11').points points1112 = jharden('2011-12').points points1213 = jharden('2012-13').points points1314 = jharden('2013-14').points points1415 = jharden('2014-15').points points1516 = jharden('2015-16').points points1617 = jharden('2016-17').points points1718 = jharden('2017-18').points turnovers0708 = jharden('2007-08').turnovers turnovers0809 = jharden('2008-09').turnovers turnovers0910 = jharden('2009-10').turnovers turnovers1011 = jharden('2010-11').turnovers turnovers112 = jharden('2011-12').turnovers turnovers1213 = jharden('2012-13').turnovers turnovers1314 = jharden('2013-14').turnovers turnovers1415 = jharden('2014-15').turnovers turnovers1516 = jharden('2014-15').turnovers
def test_missing_weight_returns_none(self, *args, **kwargs): mock_weight = PropertyMock(return_value=None) player = Player(None) type(player)._weight = mock_weight assert not player.weight