def update_stats(path, week): print("Updating stats.") s = Stats() curr_stats = s.get_week_stats('regular', config['seasonYear'], week) with open(path, 'w') as outfile: json.dump(curr_stats, outfile, indent=2) outfile.close() return curr_stats
def test_get_player_week_stats(): stats = Stats() week_stats = stats.get_week_stats("regular", 2018, 5) player_week_stats = stats.get_player_week_stats(week_stats, "1262") assert isinstance(player_week_stats, dict) player_week_stats = stats.get_player_week_stats(week_stats, "300000000") assert player_week_stats is None
def make_roster_dict(starters_list, bench_list): """ Takes in a teams starter list and bench list and makes a dictionary with positions. :param starters_list: List of a teams starters :param bench_list: List of a teams bench players :return: {starters:{position: []} , bench:{ position: []} } """ week = get_current_week() players = Players().get_all_players() stats = Stats() week_stats = stats.get_week_stats("regular", STARTING_YEAR, week) roster_dict = {"starters": {}, "bench": {}} for player_id in starters_list: player = players[player_id] player_position = player["position"] player_name = player["first_name"] + " " + player["last_name"] try: player_std_score = week_stats[player_id]["pts_std"] except KeyError: player_std_score = None player_and_score_tup = (player_name, player_std_score) if player_position not in roster_dict["starters"]: roster_dict["starters"][player_position] = [player_and_score_tup] else: roster_dict["starters"][player_position].append( player_and_score_tup) for player_id in bench_list: player = players[player_id] player_position = player["position"] player_name = player["first_name"] + " " + player["last_name"] try: player_std_score = week_stats[player_id]["pts_std"] except KeyError: player_std_score = None player_and_score_tup = (player_name, player_std_score) if player_position not in roster_dict["bench"]: roster_dict["bench"][player_position] = [player_and_score_tup] else: roster_dict["bench"][player_position].append(player_and_score_tup) return roster_dict
def get_negative_starters(league_id): """ Finds all of the players that scores negative points in standard and :param league_id: Int league_id :return: Dict {"owner_name":[("player_name", std_score), ...], "owner_name":...} """ week = get_current_week() league = League(league_id) users = league.get_users() matchups = league.get_matchups(week) stats = Stats() # WEEK STATS NEED TO BE FIXED week_stats = stats.get_week_stats("regular", STARTING_YEAR, week) players = Players() players_dict = players.get_all_players() owner_id_to_team_dict = map_users_to_team_name(users) roster_id_to_owner_id_dict = map_roster_id_to_owner_id(league_id) result_dict = {} for i, matchup in enumerate(matchups): starters = matchup["starters"] negative_players = [] for starter_id in starters: try: std_pts = week_stats[str(starter_id)]["pts_std"] except KeyError: std_pts = 0 if std_pts < 0: player_info = players_dict[starter_id] player_name = "{} {}".format(player_info["first_name"], player_info["last_name"]) negative_players.append((player_name, std_pts)) if len(negative_players) > 0: owner_id = roster_id_to_owner_id_dict[matchup["roster_id"]] if owner_id is None: team_name = "Team name not available" + str(i) else: team_name = owner_id_to_team_dict[owner_id] result_dict[team_name] = negative_players return result_dict
def test_get_player_week_score(capsys): stats = Stats() week_stats = stats.get_week_stats("regular", 2018, 5) score = stats.get_player_week_score(week_stats, "GB") assert isinstance(score, dict) assert score["pts_ppr"] != None score = stats.get_player_week_score(week_stats, "9999") assert isinstance(score, dict) assert score["pts_ppr"] == None score = stats.get_player_week_score(week_stats, "5170") assert isinstance(score, dict) assert score["pts_ppr"] != None
def get_bench_points(league_id): """ :param league_id: Int league_id :return: List [(team_name, score), ...] """ week = get_current_week() league = League(league_id) users = league.get_users() matchups = league.get_matchups(week) stats = Stats() # WEEK STATS NEED TO BE FIXED week_stats = stats.get_week_stats("regular", STARTING_YEAR, week) owner_id_to_team_dict = map_users_to_team_name(users) roster_id_to_owner_id_dict = map_roster_id_to_owner_id(league_id) result_list = [] for matchup in matchups: starters = matchup["starters"] all_players = matchup["players"] bench = set(all_players) - set(starters) std_points = 0 for player in bench: try: std_points += week_stats[str(player)]["pts_std"] except: continue owner_id = roster_id_to_owner_id_dict[matchup["roster_id"]] if owner_id is None: team_name = "Team name not available" else: team_name = owner_id_to_team_dict[owner_id] result_list.append((team_name, std_points)) return result_list
# column specifies the week points_arr = np.zeros((len(owner_roster_list), 17)) matchup_arr = np.zeros((len(owner_roster_list), 17)) win_arr = np.zeros((len(owner_roster_list), 17)) overall_win_arr = np.zeros((len(owner_roster_list), 17)) potential_points_arr = np.zeros((len(owner_roster_list), 17)) sleeper_points_arr = np.zeros((len(owner_roster_list), 17)) all_players = players.get_all_players() # print('test') weekly_matchup_list = np.array([]) weeks = 16 for week in range(weeks): weekly_matchup = league.get_matchups(week) week_stats = stats.get_week_stats('regular', 2019, week) # Using weekly_scores = [] weekly_wins = [] for team in weekly_matchup: sleeper_points = team['points'] sleeper_points_arr[team['roster_id'] - 1, week - 1] = sleeper_points team_roster_id = team['roster_id'] team_points = team['points'] # Sleeper's reported points team_players = team['players'] team_starters = team['starters'] team_matchup_id = team[ 'matchup_id'] # Matchup id keeps track of who plays who
def test_get_week_stats(): stats = Stats() week_stats = stats.get_week_stats('regular', 2018, '2') assert isinstance(week_stats, list)
from sleeper_wrapper import League from sleeper_wrapper import Stats league = League(601091358869999616) league.get_league() league.get_users() stats = Stats() stats.get_all_stats("regular", "2020") stats.get_week_stats("regular", "2020", "1")