ffbot 1.2.12__tar.gz → 1.2.13__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ffbot
3
- Version: 1.2.12
3
+ Version: 1.2.13
4
4
  Summary: Automate playing Yahoo Fantasy Football
5
5
  Home-page: https://github.com/amarvin/fantasy-football-bot
6
6
  Author: Alex Marvin
@@ -23,7 +23,7 @@ Requires-Dist: beautifulsoup4
23
23
  Requires-Dist: loguru
24
24
  Requires-Dist: lxml
25
25
  Requires-Dist: pandas
26
- Requires-Dist: pulp
26
+ Requires-Dist: pulp[cbc]
27
27
  Requires-Dist: requests
28
28
  Requires-Dist: tqdm
29
29
  Requires-Dist: user_agent
@@ -0,0 +1 @@
1
+ VERSION = "1.2.13"
@@ -1,15 +1,14 @@
1
1
  from collections import Counter
2
2
 
3
3
  import pandas as pd
4
+ import pulp
4
5
  from loguru import logger
5
6
  from pulp import (
6
- PULP_CBC_CMD,
7
7
  LpBinary,
8
8
  LpContinuous,
9
9
  LpMaximize,
10
10
  LpProblem,
11
- LpStatus,
12
- LpVariable,
11
+ LpSolverDefault,
13
12
  lpSum,
14
13
  value,
15
14
  )
@@ -21,7 +20,7 @@ IR_STATUSES = {
21
20
  "O",
22
21
  "PUP", # e.g. PUP-R
23
22
  }
24
- SOLVER_SETTINGS = PULP_CBC_CMD(msg=False)
23
+ LpSolverDefault.msg = False
25
24
 
26
25
 
27
26
  def optimize(df, week, team, positions):
@@ -32,25 +31,25 @@ def optimize(df, week, team, positions):
32
31
 
33
32
  # Game rules
34
33
  positions = [x.strip() for x in positions.split(",")]
35
- PossiblePositions = dict(
36
- QB={"QB", "Q/W/R/T"},
37
- WR={"WR", "W/R/T", "W/T", "W/R", "Q/W/R/T"},
38
- RB={"RB", "W/R/T", "W/R", "Q/W/R/T"},
39
- TE={"TE", "W/R/T", "W/T", "Q/W/R/T"},
40
- K={"K"},
41
- DEF={"DEF", "D/ST"},
34
+ PossiblePositions = {
35
+ "QB": {"QB", "Q/W/R/T"},
36
+ "WR": {"WR", "W/R/T", "W/T", "W/R", "Q/W/R/T"},
37
+ "RB": {"RB", "W/R/T", "W/R", "Q/W/R/T"},
38
+ "TE": {"TE", "W/R/T", "W/T", "Q/W/R/T"},
39
+ "K": {"K"},
40
+ "DEF": {"DEF", "D/ST"},
42
41
  # IDP positions
43
- CB={"CB", "D", "DB"},
44
- DE={"D", "DE", "DL"},
45
- DT={"D", "DL", "DT"},
46
- LB={"D", "LB"},
47
- S={"D", "DB", "S"},
48
- )
42
+ "CB": {"CB", "D", "DB"},
43
+ "DE": {"D", "DE", "DL"},
44
+ "DT": {"D", "DL", "DT"},
45
+ "LB": {"D", "LB"},
46
+ "S": {"D", "DB", "S"},
47
+ }
49
48
  # Remove positions not available in this league
50
49
  for key, values in PossiblePositions.items():
51
- PossiblePositions[key] = set(
50
+ PossiblePositions[key] = {
52
51
  position for position in values if position in positions
53
- )
52
+ }
54
53
  PossiblePositions = {
55
54
  key: values for key, values in PossiblePositions.items() if values
56
55
  }
@@ -67,17 +66,17 @@ def optimize(df, week, team, positions):
67
66
  POSITIONS = PositionMax.keys()
68
67
 
69
68
  # Pre-process data
70
- TIMES = [t for t in range(week, 18)]
69
+ TIMES = list(range(week, 18))
71
70
  PLAYERS = set()
72
- Names = dict()
73
- Position = dict()
74
- Roster0 = dict()
75
- Owner = dict()
76
- Status = dict()
77
- FreeAgent = dict()
78
- Available = dict()
79
- Projections = dict()
80
- VOR = dict()
71
+ Names = {}
72
+ Position = {}
73
+ Roster0 = {}
74
+ Owner = {}
75
+ Status = {}
76
+ FreeAgent = {}
77
+ Available = {}
78
+ Projections = {}
79
+ VOR = {}
81
80
  for _, row in df.iterrows():
82
81
  p = row["ID"]
83
82
  PLAYERS.add(p)
@@ -91,7 +90,7 @@ def optimize(df, week, team, positions):
91
90
  FreeAgent[p] = pd.isna(owner_id) and owner == "Free Agent"
92
91
  Available[p] = pd.isna(owner_id)
93
92
  for t in TIMES:
94
- Projections[p, t] = float(row["Week {}".format(t)])
93
+ Projections[p, t] = float(row[f"Week {t}"])
95
94
  VOR[p] = row["VOR"]
96
95
  # create other parameters
97
96
  Discounts = {
@@ -122,14 +121,38 @@ def optimize(df, week, team, positions):
122
121
  # Define optimization problem
123
122
  prob = LpProblem("football", LpMaximize)
124
123
 
124
+ # Support PuLP v3 and v4
125
+ is_pulp_v3 = hasattr(pulp.LpVariable, "dicts")
126
+ if is_pulp_v3:
127
+ logger.info("PuLP v3 detected")
128
+ _add_variable_dicts = pulp.LpVariable.dicts
129
+
130
+ def _get_constraint_by_name(prob, constraint_name: str):
131
+ return prob.constraints[constraint_name]
132
+
133
+ def _solve(prob):
134
+ prob.solve(LpSolverDefault)
135
+ assert prob.status == pulp.LpStatusOptimal
136
+
137
+ else:
138
+ logger.info("PuLP v4 detected")
139
+ _add_variable_dicts = prob.add_variable_dicts
140
+
141
+ def _get_constraint_by_name(prob, constraint_name: str):
142
+ return prob.get_constraint_by_name(constraint_name)
143
+
144
+ def _solve(prob):
145
+ stats = prob.solve(LpSolverDefault)
146
+ assert stats.has_solution
147
+
125
148
  # Define decision variables
126
- roster = LpVariable.dicts("roster", PLAYERS, cat=LpBinary)
127
- add = LpVariable.dicts("add", PLAYERS, cat=LpBinary)
128
- drop = LpVariable.dicts("drop", PLAYERS, cat=LpBinary)
129
- assign = LpVariable.dicts("assign", PlayerTimePosition, cat=LpBinary)
130
- points = LpVariable.dicts("points", PlayerTime, cat=LpContinuous)
131
- points_total = LpVariable.dicts("points total", PLAYERS, cat=LpContinuous)
132
- discounted_points_total = LpVariable.dicts(
149
+ roster = _add_variable_dicts("roster", PLAYERS, cat=LpBinary)
150
+ add = _add_variable_dicts("add", PLAYERS, cat=LpBinary)
151
+ drop = _add_variable_dicts("drop", PLAYERS, cat=LpBinary)
152
+ assign = _add_variable_dicts("assign", PlayerTimePosition, cat=LpBinary)
153
+ points = _add_variable_dicts("points", PlayerTime, cat=LpContinuous)
154
+ points_total = _add_variable_dicts("points total", PLAYERS, cat=LpContinuous)
155
+ discounted_points_total = _add_variable_dicts(
133
156
  "discounted points total", PLAYERS, cat=LpContinuous
134
157
  )
135
158
 
@@ -176,8 +199,7 @@ def optimize(df, week, team, positions):
176
199
  # Solve optimization problem
177
200
  solutions_headers = ["Add", "Drop", "Total points", "Discounted points", "VOR"]
178
201
  solutions = []
179
- prob.solve(SOLVER_SETTINGS)
180
- assert LpStatus[prob.status] == "Optimal"
202
+ _solve(prob)
181
203
  known_drops = set()
182
204
  n_drops = 0
183
205
  for p in PLAYERS:
@@ -200,9 +222,8 @@ def optimize(df, week, team, positions):
200
222
  known_adds = set()
201
223
  n_adds = 1
202
224
  while True:
203
- prob.constraints["max_adds"].constant = -n_adds
204
- prob.solve(SOLVER_SETTINGS)
205
- assert LpStatus[prob.status] == "Optimal"
225
+ _get_constraint_by_name(prob, "max_adds").constant = -n_adds
226
+ _solve(prob)
206
227
  this_add = ""
207
228
  for p in PLAYERS:
208
229
  if add[p].varValue and p not in known_adds:
@@ -229,12 +250,11 @@ def optimize(df, week, team, positions):
229
250
  last_vor = vor
230
251
 
231
252
  # Re-solve for each drop to acquire a free agent
232
- del prob.constraints["max_adds"]
253
+ _get_constraint_by_name(prob, "max_adds").constant = -100
233
254
  while True:
234
255
  n_drops += 1
235
- prob.constraints["max_drops"].constant = -n_drops
236
- prob.solve(SOLVER_SETTINGS)
237
- assert LpStatus[prob.status] == "Optimal"
256
+ _get_constraint_by_name(prob, "max_drops").constant = -n_drops
257
+ _solve(prob)
238
258
  this_drop = ""
239
259
  this_add = ""
240
260
  for p in PLAYERS:
@@ -266,12 +286,11 @@ def optimize(df, week, team, positions):
266
286
  last_vor = vor
267
287
 
268
288
  # Re-solve for each waiver claim add without dropping any players
269
- del prob.constraints["only_add_free_agents"]
270
- prob += 0 >= lpSum(add[p] for p in PLAYERS), "max_adds"
289
+ _get_constraint_by_name(prob, "only_add_free_agents").sense = pulp.LpConstraintGE
290
+ _get_constraint_by_name(prob, "max_adds").constant = 0
271
291
  while True:
272
- prob.constraints["max_adds"].constant = -n_adds
273
- prob.solve(SOLVER_SETTINGS)
274
- assert LpStatus[prob.status] == "Optimal"
292
+ _get_constraint_by_name(prob, "max_adds").constant = -n_adds
293
+ _solve(prob)
275
294
  this_add = ""
276
295
  for p in PLAYERS:
277
296
  if add[p].varValue and p not in known_adds:
@@ -298,11 +317,10 @@ def optimize(df, week, team, positions):
298
317
  last_vor = vor
299
318
 
300
319
  # Re-solve for each drop to acquire a waiver claim
301
- del prob.constraints["max_adds"]
320
+ _get_constraint_by_name(prob, "max_adds").constant = -100
302
321
  while True:
303
- prob.constraints["max_drops"].constant = -n_drops
304
- prob.solve(SOLVER_SETTINGS)
305
- assert LpStatus[prob.status] == "Optimal"
322
+ _get_constraint_by_name(prob, "max_drops").constant = -n_drops
323
+ _solve(prob)
306
324
  this_drop = ""
307
325
  this_add = ""
308
326
  for p in PLAYERS:
@@ -30,7 +30,7 @@ def create_session():
30
30
  "User-Agent": generate_user_agent(),
31
31
  }
32
32
  # add retry loop
33
- retry = Retry(backoff_factor=0.6, status_forcelist=[500, 502, 503, 504, 999])
33
+ retry = Retry(backoff_factor=0.6, status_forcelist=[404, 500, 502, 503, 504, 999])
34
34
  adapter = HTTPAdapter(max_retries=retry)
35
35
  s.mount("http://", adapter)
36
36
  s.mount("https://", adapter)
@@ -52,22 +52,20 @@ def scrape(league, is_IDP: bool = False):
52
52
  groups = SEARCH_PLAYER_GROUPS_IDP if is_IDP else SEARCH_PLAYER_GROUPS
53
53
  s = create_session()
54
54
  for group in groups:
55
- logger.info("Scraping all {}...".format(group))
55
+ logger.info(f"Scraping all {group}...")
56
56
  i = 0
57
57
  while True:
58
58
  # Request next 25 best players
59
59
  s.headers["User-Agent"] = generate_user_agent()
60
60
  r = s.get(
61
- "https://football.fantasysports.yahoo.com/f1/{}/players".format(
62
- PUBLIC_LEAGUE_IDP if is_IDP else PUBLIC_LEAGUE
63
- ),
64
- params=dict(
65
- count=i * 25,
66
- pos=group,
67
- sort="PR_S", # sort by projected season rank
68
- stat1="K_K", # ranks
69
- status="ALL",
70
- ),
61
+ f"https://football.fantasysports.yahoo.com/f1/{PUBLIC_LEAGUE_IDP if is_IDP else PUBLIC_LEAGUE}/players",
62
+ params={
63
+ "count": i * 25,
64
+ "pos": group,
65
+ "sort": "PR_S", # sort by projected season rank
66
+ "stat1": "K_K", # ranks
67
+ "status": "ALL",
68
+ },
71
69
  )
72
70
  i += 1
73
71
  soup = bs(r.text, "lxml")
@@ -95,7 +93,7 @@ def scrape(league, is_IDP: bool = False):
95
93
  s = create_session()
96
94
 
97
95
  pid = row["ID"]
98
- url = "https://football.fantasysports.yahoo.com/f1/{}/playernote".format(league)
96
+ url = f"https://football.fantasysports.yahoo.com/f1/{league}/playernote"
99
97
  params = {"pid": pid}
100
98
  s.headers["User-Agent"] = generate_user_agent()
101
99
  r = s.get(url, params=params)
@@ -126,7 +124,7 @@ def scrape(league, is_IDP: bool = False):
126
124
  # Weekly projections
127
125
  df2 = pd.read_html(StringIO(html))[0]
128
126
  for _, row2 in df2.iterrows():
129
- week = "Week {}".format(row2["Week"])
127
+ week = f"Week {row2['Week']}"
130
128
  points = row2.get("Fan Pts")
131
129
  if points is None:
132
130
  continue
@@ -160,7 +158,7 @@ def scrape(league, is_IDP: bool = False):
160
158
  df = df[columns]
161
159
 
162
160
  # Calculate VOR
163
- columns = ["Week {}".format(i) for i in range(current_week(), 18)]
161
+ columns = [f"Week {i}" for i in range(current_week(), 18)]
164
162
  df["Remaining"] = df[columns].sum(axis=1)
165
163
  available = df.loc[df["Owner ID"].isnull()]
166
164
  means = (
@@ -180,7 +178,7 @@ def scrape(league, is_IDP: bool = False):
180
178
  df.sort_values(by="VOR", ascending=False, inplace=True)
181
179
  df = df.round(2)
182
180
 
183
- logger.info("Total runtime: {}".format(datetime.now() - startTime))
181
+ logger.info(f"Total runtime: {datetime.now() - startTime}")
184
182
  return df
185
183
 
186
184
 
@@ -189,7 +187,7 @@ def current_week():
189
187
 
190
188
  # Parse current week from a public league
191
189
  s = create_session()
192
- url = "https://football.fantasysports.yahoo.com/f1/{}/1".format(PUBLIC_LEAGUE)
190
+ url = f"https://football.fantasysports.yahoo.com/f1/{PUBLIC_LEAGUE}/1"
193
191
  s.headers["User-Agent"] = generate_user_agent()
194
192
  r = s.get(url)
195
193
  soup = bs(r.text, "lxml")
@@ -16,7 +16,7 @@ def save(df, week):
16
16
 
17
17
  # Create filename
18
18
  startTime = datetime.now()
19
- filename = join(folder, "{:%Y-%m-%d %H%M} week {}.csv".format(startTime, week))
19
+ filename = join(folder, f"{startTime:%Y-%m-%d %H%M} week {week}.csv")
20
20
 
21
21
  # Save data
22
22
  df.to_csv(filename, index=False)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ffbot
3
- Version: 1.2.12
3
+ Version: 1.2.13
4
4
  Summary: Automate playing Yahoo Fantasy Football
5
5
  Home-page: https://github.com/amarvin/fantasy-football-bot
6
6
  Author: Alex Marvin
@@ -23,7 +23,7 @@ Requires-Dist: beautifulsoup4
23
23
  Requires-Dist: loguru
24
24
  Requires-Dist: lxml
25
25
  Requires-Dist: pandas
26
- Requires-Dist: pulp
26
+ Requires-Dist: pulp[cbc]
27
27
  Requires-Dist: requests
28
28
  Requires-Dist: tqdm
29
29
  Requires-Dist: user_agent
@@ -2,7 +2,7 @@ beautifulsoup4
2
2
  loguru
3
3
  lxml
4
4
  pandas
5
- pulp
5
+ pulp[cbc]
6
6
  requests
7
7
  tqdm
8
8
  user_agent
@@ -1 +0,0 @@
1
- VERSION = "1.2.12"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes