def _build_lineup( self, players: List[Player], context: OptimizationContext, unswappable_players: Optional[List[LineupPlayer]] = None, ) -> Lineup: lineup = [] positions = self._settings.positions[:] if not positions: for player in sorted(players, key=lambda p: p.positions[0]): lineup.append( LineupPlayer( player, player.positions[0], used_fppg=context.players_used_fppg.get(player))) return Lineup(lineup, self._settings.lineup_printer) if unswappable_players: players = [ player for player in players if player not in unswappable_players ] positions = get_remaining_positions(positions, unswappable_players) lineup.extend(unswappable_players) players_with_positions = link_players_with_positions( players, positions) for player, position in players_with_positions.items(): lineup.append( LineupPlayer(player, position.name, used_fppg=context.players_used_fppg.get(player))) positions_order = [pos.name for pos in self._settings.positions] lineup.sort(key=lambda p: positions_order.index(p.lineup_position)) return Lineup(lineup, self._settings.lineup_printer)
def setUp(self): self.future_game_info = GameInfo( home_team='H', away_team='A', game_started=False, starts_at=datetime.now(timezone('EST')) + timedelta(days=1)) self.finished_game_info = GameInfo( home_team='H', away_team='A', game_started=False, starts_at=datetime.now(timezone('EST')) - timedelta(days=1)) self.lineup_optimizer = get_optimizer(Site.DRAFTKINGS, Sport.BASKETBALL) positions = ['PG', 'SG', 'SF', 'PF', 'C', 'PG/SG', 'SF/PF', 'C'] self.active_players = create_players(positions, game_info=self.future_game_info, salary=5000, fppg=20) self.inactive_players = create_players( positions, game_info=self.finished_game_info, salary=4500, fppg=10) self.lineup_optimizer.load_players(self.active_players + self.inactive_players) self.lineup = Lineup([ LineupPlayer(self.active_players[0], 'PG'), LineupPlayer(self.inactive_players[1], 'SG'), LineupPlayer(self.active_players[2], 'SF'), LineupPlayer(self.inactive_players[3], 'PF'), LineupPlayer(self.active_players[4], 'C'), LineupPlayer(self.inactive_players[5], 'G'), LineupPlayer(self.active_players[6], 'F'), LineupPlayer(self.inactive_players[7], 'UTIL'), ])
def import_lineups(self, players): with open(self.filename, 'r') as csv_file: lines = csv.reader(csv_file) try: header = next(lines) start_column = 4 # First 4 columns has info about tournament end_column = header.index('Instructions') - 1 except (IndexError, ValueError): raise LineupOptimizerIncorrectCSV position_names = header[start_column:end_column] players_dict = {player.id: player for player in players} lineups = [] for line in lines: if not line[0]: break lineup_players = [] for index, position in zip(range(start_column, end_column), position_names): try: player_data = line[index] player_data = player_data.replace( '(LOCKED)', '') # Remove possible '(LOCKED)' substring player_id = player_data.split('(')[1][:-1] except IndexError: raise LineupOptimizerIncorrectCSV try: player = players_dict[player_id] except KeyError: raise LineupOptimizerIncorrectCSV( 'Player not found in players pool') lineup_players.append(LineupPlayer(player, position)) lineups.append(Lineup(lineup_players)) return lineups
def import_lineups(self, players): with open(self.filename, 'r') as csv_file: lines = csv.reader(csv_file) try: header = next(lines) start_column = 4 # First 4 columns has info about tournament end_column = header.index('Instructions') - 1 except (IndexError, ValueError): raise LineupOptimizerIncorrectCSV position_names = header[start_column:end_column] players_dict = {player.id: player for player in players} lineups = [] for line in lines: if not line[0]: break lineup_players = [] for index, position in zip(range(start_column, end_column), position_names): try: match = re.search(self.LINEUP_PLAYER_ID_REGEX, line[index]) except IndexError: raise LineupOptimizerIncorrectCSV if not match: raise LineupOptimizerIncorrectCSV player_id = match.group('id') try: player = players_dict[player_id] except KeyError: raise LineupOptimizerIncorrectCSV('Player not found in players pool') lineup_players.append(LineupPlayer(player, position)) lineups.append(Lineup(lineup_players)) return lineups
def _build_lineup(self, players): # type: (List[Player]) -> Lineup players_with_positions = self._link_players_with_positions(players) lineup = [] for player, position in players_with_positions.items(): lineup.append(LineupPlayer(player, position.name)) positions_order = [pos.name for pos in self._settings.positions] lineup.sort(key=lambda p: positions_order.index(p.lineup_position)) return Lineup(lineup)
def _build_lineup(self, players, unswappable_players=None): # type: (List[Player], Optional[List[LineupPlayer]]) -> Lineup lineup = [] positions = self._settings.positions[:] if unswappable_players: players = [player for player in players if player not in unswappable_players] positions = get_remaining_positions(positions, unswappable_players) lineup.extend(unswappable_players) players_with_positions = link_players_with_positions(players, positions) for player, position in players_with_positions.items(): lineup.append(LineupPlayer(player, position.name)) positions_order = [pos.name for pos in self._settings.positions] lineup.sort(key=lambda p: positions_order.index(p.lineup_position)) return Lineup(lineup, self._settings.lineup_printer)