def GetPlayersFromJSON(players_dict, exposure_all): players = [] in_players = [] ex_players = [] for p in players_dict: player = Player( p['ID'], p['PLAYER'], p['PLAYER'], [p['POS']], p['TEAM'], float(p['SAL']), float(p['FPTS']), projected_ownership=float(p['TM/P'])) players.append(player) max_exp = int(p['max']) min_exp = int(p['min']) if exposure_all['max_all'] != '': max_exp = min(int(exposure_all['max_all']), int(p['max'])) if exposure_all['min_all'] != '': min_exp = max(int(exposure_all['min_all']), int(p['min'])) if max_exp < 100: player.max_exposure = float(max_exp) / 100 if min_exp > 0: player.min_exposure = float(min_exp) / 100 if p['inc_exc_state'] == 1: in_players.append(player) elif p['inc_exc_state'] == 2: ex_players.append(player) return [players, in_players, ex_players]
def transform_player(player, gameType): return Player( player["id"], player["firstName"], player["lastName"], player["draftPositions"].split("/") if gameType and 'Showdown' in gameType else player["position"].split("/"), player["team"], float(player["salary"]), float(player["fppg"]), player.get("status") == "O", None, player.get("minExposure"), player.get("projectedOwnership"))
def test_adding_player_to_formed_position(self): self.lineup_optimizer.reset_lineup() players = [] for i in '1234': players.append(Player(int(i), i, i, ['PG'], 'DEN', 10, 2)) for i in range(3): self.lineup_optimizer.add_player_to_lineup(players[i]) with self.assertRaises(LineupOptimizerException): self.lineup_optimizer.add_player_to_lineup(players[3])
def test_remove_player_from_lineup(self): optimizer = self.lineup_optimizer player = Player('1', 'P', 'P', ['PG'], 'DEN', 10, 2) optimizer.extend_players([player]) optimizer.add_player_to_lineup(player) optimizer.remove_player_from_lineup(player) self.assertNotIn(player, optimizer.locked_players) with self.assertRaises(LineupOptimizerException): optimizer.remove_player_from_lineup(player)
def test_remove_player_from_lineup(self): pool = self.player_pool player = Player('1', 'P', 'P', ['PG'], 'DEN', 10, 2) pool.extend_players([player]) pool.lock_player(player) pool.unlock_player(player) self.assertNotIn(player, pool.locked_players) with self.assertRaises(LineupOptimizerException): pool.unlock_player(player)
def GetPlayersFromFile(timestamp): page = open(os.path.dirname(os.path.abspath(__file__)) + '/' + timestamp + '_players.json', 'r').read() data = json.loads(page) players_dict = data['players'] players = [] for p in players_dict: player = Player( p['ID'], p['PLAYER'], p['PLAYER'], [p['POS']], p['TEAM'], float(p['SAL']), float(p['FPTS'])) players.append(player) if int(p['max']) < 100: player.max_exposure = float(p['max']) / 100 if int(p['min']) > 0: player.min_exposure = float(p['min']) / 100 return players
def setUpClass(cls): optimizer = get_optimizer(Site.YAHOO, Sport.BASKETBALL) cls.players = [ Player('1', 'First Name 1', 'Last Name 1', ['PG'], 'Team1', 20, 20), Player('2', 'First Name 2', 'Last Name 2', ['SG'], 'Team2', 20, 20), Player('3', 'First Name 3', 'Last Name 3', ['SF'], 'Team3', 20, 20), Player('4', 'First Name 4', 'Last Name 4', ['PF'], 'Team4', 20, 20), Player('5', 'First Name 5', 'Last Name 5', ['C'], 'Team5', 20, 20), Player('6', 'First Name 6', 'Last Name 6', ['PG', 'SG'], 'Team6', 20, 20), Player('7', 'First Name 7', 'Last Name 7', ['SF', 'PF'], 'Team7', 20, 20), Player('8', 'First Name 8', 'Last Name 8', ['PG', 'SG', 'SF'], 'Team8', 20, 20), ] optimizer.load_players(cls.players) cls.lineups = list(optimizer.optimize(1))
def GetTestPlayersFromPyDFS(): url = "https://raw.githubusercontent.com/DimaKudosh/pydfs-lineup-optimizer/master/tests/players.json" page = urlopen(url).read() players_dict = json.loads(page)['players'] players = [Player( p['id'], p['first_name'], p['last_name'], p['positions'], p['team'], p['salary'], p['fppg'] ) for i, p in enumerate(players_dict)] return players
def test_lineup_with_max_players(self): self.lineup_optimizer.reset_lineup() players = [ Player(1, 'P1', 'P1', ['PG'], 'DEN', 10, 2), Player(5, 'P5', 'P5', ['SG'], 'DEN', 10, 2), Player(2, 'P2', 'P2', ['SF'], 'DEN', 10, 2), Player(6, 'P6', 'P6', ['PF'], 'DEN', 10, 2), Player(3, 'P3', 'P3', ['C'], 'DEN', 10, 2), Player(7, 'P7', 'P7', ['PG'], 'DEN', 10, 2), Player(4, 'P4', 'P4', ['PF'], 'DEN', 10, 2), Player(8, 'P8', 'P8', ['PG'], 'DEN', 10, 2) ] self.lineup_optimizer.extend_players(players) for player in players: self.lineup_optimizer.add_player_to_lineup(player) gen = self.lineup_optimizer.optimize(10) self.assertEqual(len(list(gen)), 1)
def transform_player(player): player = Player( player[0], player[1], player[2], player[3].split("/"), player[4], player[5], player[6], # True if player["status"] == "O" else False, player[7] if player[7] != "O" else None, # False ) return player
def nba_to_pydfs(players): ''' Takes results, make ready to create Player objects for pydfs_lineup_optimizer Args: players (list): day's worth of stats Returns: players (list): list of players, fixed for use in pydfs_lineup_optimizer ''' return [ Player(p['nbacom_player_id'], p['first_name'], p['last_name'], p.get('dfs_position').split('/'), p.get('team_code'), float(p.get('salary', 100000)), float(p.get('dk_points', 0))) for p in players ]
def preprocess_fantasylabs(players): # id, first_name, last_name, positions, team, salary, fppg, is_injured=False): pydfs_players = [] for p in players: parts = p.get('Player_Name', '').split(' ') if parts and len(parts) == 2: first_name, last_name = parts else: first_name = p.get('Player_Name') last_name = '' positions = [p.get('Position')] salary = float(p.get('Salary', 0)) fppg = float(p.get('AvgPts', 0)) pydfs_players.append( Player('', first_name, last_name, positions, '', salary, fppg)) return pydfs_players
def GetPlayersFromRotoWire(site): url_dk = "https://www.rotowire.com/daily/tables/optimizer-nfl.php?sport=NFL&site=DraftKings&projections=&type=main&slate=all&ownership=" url_fd = "https://www.rotowire.com/daily/tables/optimizer-nfl.php?sport=NFL&site=FanDuel&projections=&type=main&slate=Main&ownership=" url_yh = "https://www.rotowire.com/daily/tables/optimizer-nfl.php?sport=NFL&site=Yahoo&projections=&type=main&slate=all&ownership=" url = url_fd # if site == 'DraftKings': # url = url_dk # elif site == 'Yahoo': # url = url_yh page = urlopen(url).read() players_dict = json.loads(page) players = [Player( p['id'], p['first_name'], p['last_name'], [p['position']], p['team'], float(p['salary']), float(p['proj_points']) ) for i, p in enumerate(players_dict)] return players
def test_adding_player_with_salary_bigger_than_budget(self): self.lineup_optimizer.reset_lineup() player = Player(1, '1', '1', ['PG'], 'DEN', 100000, 2) with self.assertRaises(LineupOptimizerException): self.lineup_optimizer.add_player_to_lineup(player)
if player.player: if player.player.name == "{} {}".format( data['first_name'], data['last_name']): pts = base_fantasy_points(player) data['last_pts'] = pts try: data['last_proj'] = last_df[last_df.Name == "{} {}".format( data['first_name'], data['last_name'])].Projected.values[0] except: data['last_proj'] = 0 df = json_normalize(data) main_df = main_df.append(df, ignore_index=True, sort=False) try: player_list.append( Player(int(player_dict['id']), str(data['first_name']), str(data['last_name']), data['position'].split("/"), data['team'], data['salary_dk'], float(data['projected']))) except Exception as e: continue print("[*] Calculating percent change from last week.") main_df['pct_change'] = (main_df.last_pts - main_df.last_proj) / main_df.last_proj * 100 print("[*] Generating optimal lineups") optimizer.load_players(player_list) player = optimizer.get_player_by_name('Garoppolo') optimizer.add_player_to_lineup(player) for lineup in optimizer.optimize(n=5): print(lineup)
def test_adding_player_with_salary_bigger_than_budget(self): player = Player('1', '1', '1', ['PG'], 'DEN', 100000, 2) with self.assertRaises(LineupOptimizerException): self.player_pool.lock_player(player)