Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/razinc/fplysis
Browse files Browse the repository at this point in the history
  • Loading branch information
razinc committed Nov 29, 2023
2 parents a231222 + c946e74 commit 1f33054
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
10 changes: 3 additions & 7 deletions custom/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def builder(self, ownership, tqdm_desc):
team_id = player["fixtures"][i]["team_h"]
where = "A"
fdr = teams[team_id][f"FDR_{where}"]
fdr_sum = fdr_sum + fdr
fdr_sum = fdr_sum + fdr
total_games = total_games + 1
team_against_short_name = teams[team_id]["short_name"]
fixtures[gw].append(
Expand All @@ -112,9 +112,7 @@ def builder(self, ownership, tqdm_desc):
"ep_this": float(player["ep_this"]),
"ep_next": float(player["ep_next"]),
"fixtures": fixtures,
"fdr_avg": round(
fdr_sum / total_games, 1
),
"fdr_avg": round(fdr_sum / total_games, 1),
}

if ownership is not None:
Expand All @@ -140,9 +138,7 @@ def sort_by_total_pts_prev_n_gw(self):

def sort_by_fda(self):
self.sort_by_total_pts_prev_n_gw()
self.stats = {
k: v for k, v in self.stats.items() if v["fdr_avg"] < 2.4
}
self.stats = {k: v for k, v in self.stats.items() if v["fdr_avg"] < 2.4}

def sort_by_sum_xg_xa(self):
self.stats = OrderedDict(
Expand Down
10 changes: 7 additions & 3 deletions custom/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ async def get_teams():
teams = await fpl.get_teams(return_json=True)
return teams


async def get_fdr():
async with aiohttp.ClientSession() as session:
fpl = FPL(session)
fdr = await fpl.FDR()
return fdr


def builder():
fdr = asyncio.run(get_fdr())

Expand All @@ -28,9 +30,11 @@ def builder():

teams = {}
for team in asyncio.run(get_teams()):
teams[team["id"]] = {"short_name": team["short_name"],
"FDR_H": round(fdr[team["name"]]["all"]["H"], 2),
"FDR_A": round(fdr[team["name"]]["all"]["A"], 2)}
teams[team["id"]] = {
"short_name": team["short_name"],
"FDR_H": round(fdr[team["name"]]["all"]["H"], 2),
"FDR_A": round(fdr[team["name"]]["all"]["A"], 2),
}
return teams


Expand Down
6 changes: 3 additions & 3 deletions tests/test_players.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ def test_init_with_mock(monkeypatch):
monkeypatch.setattr(Gameweek, "PREV_N_GWS", [5, 6])
monkeypatch.setattr(Gameweek, "NEXT_N_GWS", [8, 9])
# TODO: this works but only patch certain key value pair in the dict, patch the whole var?
monkeypatch.setitem(teams, 1, {'short_name': 'ARS', 'FDR_H': 4.38, 'FDR_A': 4.18})
monkeypatch.setitem(teams, 6, {'short_name': 'BUR', 'FDR_H': 1.77, 'FDR_A': 1.0})
monkeypatch.setitem(teams, 7, {'short_name': 'CHE', 'FDR_H': 3.68, 'FDR_A': 2.42})
monkeypatch.setitem(teams, 1, {"short_name": "ARS", "FDR_H": 4.38, "FDR_A": 4.18})
monkeypatch.setitem(teams, 6, {"short_name": "BUR", "FDR_H": 1.77, "FDR_A": 1.0})
monkeypatch.setitem(teams, 7, {"short_name": "CHE", "FDR_H": 3.68, "FDR_A": 2.42})

async def mock_get_players(self):
return [
Expand Down
10 changes: 8 additions & 2 deletions tests/test_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ async def mock_get_teams():
monkeypatch.setattr("custom.teams.get_teams", mock_get_teams)

async def mock_get_fdr():
return {"Arsenal": {"all": {"H": 4.380170195923214, "A": 4.18487113008998}}, "Aston Villa": {"all": {"H": 2.10799358574808, "A": 5.0}}}
return {
"Arsenal": {"all": {"H": 4.380170195923214, "A": 4.18487113008998}},
"Aston Villa": {"all": {"H": 2.10799358574808, "A": 5.0}},
}

monkeypatch.setattr("custom.teams.get_fdr", mock_get_fdr)

teams = builder()
assert teams == {1: {"short_name": "ARS", "FDR_H": 4.38, "FDR_A": 4.18}, 2: {"short_name": "AVL", "FDR_H": 2.11, "FDR_A": 5.0}}
assert teams == {
1: {"short_name": "ARS", "FDR_H": 4.38, "FDR_A": 4.18},
2: {"short_name": "AVL", "FDR_H": 2.11, "FDR_A": 5.0},
}

0 comments on commit 1f33054

Please sign in to comment.