Skip to content

Commit

Permalink
79 on line length
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBrostoff committed Dec 23, 2023
1 parent 560c4a3 commit 14a121a
Show file tree
Hide file tree
Showing 22 changed files with 421 additions and 130 deletions.
8 changes: 7 additions & 1 deletion draftfast/constants/positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

POSITIONS_BY_SITE_BY_LEAGUE = {
"DRAFT_KINGS": {
"NBA": [["PG", 1, 3], ["SG", 1, 3], ["SF", 1, 3], ["PF", 1, 3], ["C", 1, 2]],
"NBA": [
["PG", 1, 3],
["SG", 1, 3],
["SF", 1, 3],
["PF", 1, 3],
["C", 1, 2],
],
"NBA_SHOWDOWN": [
["CPT", 1, 1],
["FLEX", 5, 5],
Expand Down
8 changes: 6 additions & 2 deletions draftfast/csv_parse/salary_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def generate_players_from_csvs(
errors,
)

with open(salary_file_location, "r", encoding=encoding, errors=errors) as csv_file:
with open(
salary_file_location, "r", encoding=encoding, errors=errors
) as csv_file:
csv_data = csv.DictReader(csv_file)
pos_key = "Position"
is_nhl = ruleset and ruleset.league == "NHL"
Expand Down Expand Up @@ -200,7 +202,9 @@ def _parse_fd_mvp_nfl_row(pos: str, row: dict) -> List[ShowdownPlayer]:
]


def _parse_mvp_mlb_row(pos: str, row: dict, ruleset: RuleSet) -> List[ShowdownPlayer]:
def _parse_mvp_mlb_row(
pos: str, row: dict, ruleset: RuleSet
) -> List[ShowdownPlayer]:
"""
FanDuel CSVs give all players without breaking out an MVP / STAR position.
Unlike DK, salary does not change here.
Expand Down
15 changes: 12 additions & 3 deletions draftfast/csv_parse/uploaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def map_pids(pid_file, encoding, errors, game=DRAFT_KINGS):
)

f.close()
f = islice(open(pid_file, "r", encoding=encoding, errors=errors), n, None)
f = islice(
open(pid_file, "r", encoding=encoding, errors=errors), n, None
)
reader = csv.DictReader(f, fieldnames=fields)
for line in reader:
# DraftKings adds spaces to DST for NFL
Expand All @@ -62,7 +64,11 @@ def map_pids(pid_file, encoding, errors, game=DRAFT_KINGS):

class CSVUploader(object):
def __init__(
self, pid_file, upload_file="./upload.csv", encoding="utf-8", errors="replace"
self,
pid_file,
upload_file="./upload.csv",
encoding="utf-8",
errors="replace",
):
self.upload_file = upload_file
self.encoding = encoding
Expand Down Expand Up @@ -222,5 +228,8 @@ def write_rosters(self, rosters):
writer.writerow(self.HEADERS)
for roster in rosters:
writer.writerow(
[p.get_player_id(self.pid_map) for p in roster.sorted_players()]
[
p.get_player_id(self.pid_map)
for p in roster.sorted_players()
]
)
20 changes: 15 additions & 5 deletions draftfast/exposure.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ def get_exposure_args(
exposures[p.name] = exposures.get(p.name, 0) + 1

if use_random:
return get_exposure_args_random(exposures, exposure_bounds, n, random_seed)
return get_exposure_args_random(
exposures, exposure_bounds, n, random_seed
)

return get_exposure_args_deterministic(exposures, existing_rosters, exposure_bounds)
return get_exposure_args_deterministic(
exposures, existing_rosters, exposure_bounds
)


def get_exposure_args_deterministic(
Expand Down Expand Up @@ -78,7 +82,9 @@ def get_exposure_args_deterministic(
}


def get_exposure_args_random(exposures, exposure_bounds, n, random_seed) -> dict:
def get_exposure_args_random(
exposures, exposure_bounds, n, random_seed
) -> dict:
locked = []

for bound in exposure_bounds:
Expand Down Expand Up @@ -129,7 +135,9 @@ def get_exposure_table(rosters, bounds):
exposures[p.name] = exposures.get(p.name, 0) + 1
players[p.name] = p

exposures = OrderedDict(sorted(exposures.items(), key=lambda t: t[1], reverse=True))
exposures = OrderedDict(
sorted(exposures.items(), key=lambda t: t[1], reverse=True)
)

table_data = []
headers = [
Expand Down Expand Up @@ -162,7 +170,9 @@ def get_exposure_table(rosters, bounds):

continue

table_data.append(players[name].to_exposure_table_row(num, s_min, s_max))
table_data.append(
players[name].to_exposure_table_row(num, s_min, s_max)
)

table = AsciiTable(table_data)
table.justify_columns[4] = "right"
Expand Down
8 changes: 6 additions & 2 deletions draftfast/lineup_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ def _bounds_str(self):

def _exact_bounds_sanity_check(self):
if self.exact <= 0:
raise ConstraintException("Exact bound may not less than or equal to zero")
raise ConstraintException(
"Exact bound may not less than or equal to zero"
)
if self.exact >= len(self.players):
raise ConstraintException(
"Exact bound may not be greater than or equal to number "
Expand All @@ -329,7 +331,9 @@ def _ub_lb_bounds_sanity_check(self):
)
if self.ub < self.lb:
raise ConstraintException(
"Upper bound for {!r} cannot be less than lower bound.".format(self)
"Upper bound for {!r} cannot be less than lower bound.".format(
self
)
)
if self.ub > len(self.players) or self.lb > len(self.players):
raise ConstraintException(
Expand Down
5 changes: 4 additions & 1 deletion draftfast/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ def run(
PLAYER COUNT: {}
""".format(
optimizer_settings, constraints, player_settings, len(players or [])
optimizer_settings,
constraints,
player_settings,
len(players or []),
)
)
return None
Expand Down
38 changes: 29 additions & 9 deletions draftfast/optimizer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import List
from ortools.linear_solver import pywraplp
from draftfast.settings import OptimizerSettings
from draftfast.dke_exceptions import InvalidBoundsException, PlayerBanAndLockException
from draftfast.dke_exceptions import (
InvalidBoundsException,
PlayerBanAndLockException,
)
from draftfast.orm import Player
from draftfast.rules import RuleSet
from draftfast.lineup_constraints import LineupConstraints
Expand Down Expand Up @@ -131,7 +134,9 @@ def _set_player_constraints(self):
ub = 0 if (p.ban or p.position_ban) else 1

if lb > ub:
raise InvalidBoundsException("Invalid bounds for {}".format(p.name))
raise InvalidBoundsException(
"Invalid bounds for {}".format(p.name)
)

if (p.multi_position or self.showdown) and not (
p.position_lock or p.position_ban
Expand Down Expand Up @@ -208,7 +213,10 @@ def _set_stack(self):
stack_cap.SetCoefficient(self.variables[i], 1)

self._set_stacking_type(
stack_lock_pos, stack_lock_eligible_pos, stack_team, stack.count
stack_lock_pos,
stack_lock_eligible_pos,
stack_team,
stack.count,
)

def _set_stacking_type(
Expand Down Expand Up @@ -236,7 +244,9 @@ def _set_stacking_type(
self.solver.Sum(skillplayers_on_team)
>= self.solver.Sum(locked_on_team)
)
self.solver.Add((self.solver.Sum(skillplayers_on_team)) >= count - 1)
self.solver.Add(
(self.solver.Sum(skillplayers_on_team)) >= count - 1
)

def _set_combo(self):
if self.settings:
Expand Down Expand Up @@ -277,7 +287,8 @@ def _set_no_opp_defense(self):
offensive_against = [
self.variables[i]
for i, p in enumerated_players
if p.pos in offensive_pos and p.is_opposing_team_in_match_up(team)
if p.pos in offensive_pos
and p.is_opposing_team_in_match_up(team)
]

# TODO this is gross for showdown
Expand Down Expand Up @@ -323,7 +334,9 @@ def _set_custom_rules(self):
for i, p in self.enumerated_players
if rule.group_b(p)
]
self.solver.Add(rule.comparison(self.solver.Sum, group_a, group_b))
self.solver.Add(
rule.comparison(self.solver.Sum, group_a, group_b)
)

def _set_positions(self):
for position, min_limit, max_limit in self.position_limits:
Expand All @@ -334,7 +347,11 @@ def _set_positions(self):
position_cap.SetCoefficient(self.variables[i], 1)

def _set_general_positions(self):
for general_position, min_limit, max_limit in self.general_position_limits:
for (
general_position,
min_limit,
max_limit,
) in self.general_position_limits:
position_cap = self.solver.Constraint(min_limit, max_limit)

for i, player in self.enumerated_players:
Expand Down Expand Up @@ -368,9 +385,12 @@ def _set_min_teams(self):
for i, p in self.enumerated_players
if p.team == team
]
self.solver.Add(team_var <= self.solver.Sum(players_on_team))
self.solver.Add(
self.max_players_per_team >= self.solver.Sum(players_on_team)
team_var <= self.solver.Sum(players_on_team)
)
self.solver.Add(
self.max_players_per_team
>= self.solver.Sum(players_on_team)
)

if len(teams) > 0:
Expand Down
8 changes: 6 additions & 2 deletions draftfast/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ def __repr__(self):
def identifier(self):
if self.cached_id:
return self.cached_id
self.cached_id = " ".join(sorted([x.roster_id for x in self.sorted_players()]))
self.cached_id = " ".join(
sorted([x.roster_id for x in self.sorted_players()])
)

return self.cached_id

Expand Down Expand Up @@ -370,7 +372,9 @@ def identifier(self):
"""
if self.cached_id:
return self.cached_id
self.cached_id = " ".join(sorted([x.solver_id for x in self.sorted_players()]))
self.cached_id = " ".join(
sorted([x.solver_id for x in self.sorted_players()])
)

return self.cached_id

Expand Down
4 changes: 3 additions & 1 deletion draftfast/pickem/pickem_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def optimize(
constraints: LineupConstraints = LineupConstraints(),
):
lineup_players = []
all_players = list(filter(add_pickem_contraints(player_settings), all_players))
all_players = list(
filter(add_pickem_contraints(player_settings), all_players)
)

if constraints.has_group_constraints():
raise NotImplementedError("Groups are not supported for pickem")
Expand Down
6 changes: 5 additions & 1 deletion draftfast/pickem/pickem_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def __repr__(self):
]
for p in self.players:
table_data.append(p.to_table_row())
return AsciiTable(table_data).table + "\n" + "Total: {}".format(self.total)
return (
AsciiTable(table_data).table
+ "\n"
+ "Total: {}".format(self.total)
)

@property
def players(self):
Expand Down
8 changes: 6 additions & 2 deletions draftfast/player_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
from draftfast.settings import PlayerPoolSettings


def filter_pool(pool: list, player_settings: PlayerPoolSettings) -> List[Player]:
def filter_pool(
pool: list, player_settings: PlayerPoolSettings
) -> List[Player]:
if player_settings.randomize:
for player in pool:
factor = 1 + runiform(-player_settings.randomize, player_settings.randomize)
factor = 1 + runiform(
-player_settings.randomize, player_settings.randomize
)
player.proj = player.proj * factor

return list(
Expand Down
14 changes: 11 additions & 3 deletions draftfast/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,16 @@ def __repr__(self):
def __str__(self):
lines = []
if self.stacks and len(self.stacks):
lines.append("Stacks: {}".format([(x.team, x.count) for x in self.stacks]))
lines.append(
"Stacks: {}".format([(x.team, x.count) for x in self.stacks])
)
if self.min_teams:
lines.append("Min teams: {}".format(self.min_teams))
if self.no_offense_against_defense:
lines.append(
"No offense against D: {}".format(self.no_offense_against_defense)
"No offense against D: {}".format(
self.no_offense_against_defense
)
)
if self.custom_rules:
lines.append("Custom rules: {}".format(self.custom_rules))
Expand All @@ -156,7 +160,11 @@ def __init__(self, pid_file, upload_file, rule_set, rosters):

class Stack(object):
def __init__(
self, team: str, count: int, stack_lock_pos=None, stack_eligible_pos=None
self,
team: str,
count: int,
stack_lock_pos=None,
stack_eligible_pos=None,
):
self.team = team
self.count = count
Expand Down
40 changes: 30 additions & 10 deletions draftfast/test/test_csgo_showdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,39 @@

def _build_mock_player_pool():
player_pool = [
Player(name="A1", cost=5500, proj=100, pos="QB", team="X", matchup="X@Y"),
Player(name="A2", cost=5500, proj=41, pos="QB", team="X", matchup="X@Y"),
Player(name="A11", cost=5500, proj=50, pos="WR", team="X", matchup="X@Y"),
Player(name="A3", cost=5500, proj=42, pos="WR", team="X", matchup="X@Y"),
Player(name="A4", cost=5500, proj=43, pos="WR", team="X", matchup="X@Y"),
Player(name="A5", cost=5500, proj=44, pos="WR", team="X", matchup="X@Y"),
Player(name="A6", cost=5500, proj=45, pos="RB", team="X", matchup="X@Y"),
Player(
name="A1", cost=5500, proj=100, pos="QB", team="X", matchup="X@Y"
),
Player(
name="A2", cost=5500, proj=41, pos="QB", team="X", matchup="X@Y"
),
Player(
name="A11", cost=5500, proj=50, pos="WR", team="X", matchup="X@Y"
),
Player(
name="A3", cost=5500, proj=42, pos="WR", team="X", matchup="X@Y"
),
Player(
name="A4", cost=5500, proj=43, pos="WR", team="X", matchup="X@Y"
),
Player(
name="A5", cost=5500, proj=44, pos="WR", team="X", matchup="X@Y"
),
Player(
name="A6", cost=5500, proj=45, pos="RB", team="X", matchup="X@Y"
),
# Test that max players per team works. Everyone
# on Y is projected for 1 point, under normal
# opto should never be picked.
Player(name="A7", cost=5500, proj=1, pos="RB", team="Y", matchup="X@Y"),
Player(name="A8", cost=5500, proj=1, pos="RB", team="Y", matchup="X@Y"),
Player(name="A9", cost=5500, proj=1, pos="TE", team="Y", matchup="X@Y"),
Player(
name="A7", cost=5500, proj=1, pos="RB", team="Y", matchup="X@Y"
),
Player(
name="A8", cost=5500, proj=1, pos="RB", team="Y", matchup="X@Y"
),
Player(
name="A9", cost=5500, proj=1, pos="TE", team="Y", matchup="X@Y"
),
]

def capt_boost(p):
Expand Down
Loading

0 comments on commit 14a121a

Please sign in to comment.