Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply_live_bonus #75

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
assign fixture stats on init
johnmadden86 committed Oct 19, 2019
commit 2f12a0f8364f5bdb318ce203b7f1f30f04e9c6bf
87 changes: 47 additions & 40 deletions fpl/models/fixture.py
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ def add_player(location, information):
location.append({"player": player, "goals": goals})


class Fixture():
class Fixture:
"""A class representing fixtures in the Fantasy Premier League.

Basic usage::
@@ -27,98 +27,105 @@ class Fixture():
>>> asyncio.run(main())
Arsenal vs. Man City - 10 Aug 19:00
"""

def __init__(self, fixture_information):
for k, v in fixture_information.items():
if k == 'stats':
v = {w['identifier']: {'a': w['a'], 'h': w['h']} for w in v}
setattr(self, k, v)

def _get_players(self, metric):
def _get_players(self, metric): # no longer used
"""Helper function that returns a dictionary containing players for the
given metric (away and home).
"""
stats = getattr(self, "stats", [])
for statistic in stats:
if metric in statistic.keys():
return statistic[metric]

if metric == statistic['identifier']:
# # merge home and away player lists and sort in descending order
# return sorted(statistic['a'] + statistic['h'], key=lambda x: x['value'], reverse=True)
print(metric, statistic)
return statistic
return {}

def get_goalscorers(self):
"""Returns all players who scored in the fixture.

:rtype: dict
"""
if not getattr(self, "finished", False):
return {}

return self._get_players("goals_scored")
try:
return self.stats["goals_scored"]
except KeyError:
return {'a': [], 'h': []}

def get_assisters(self):
"""Returns all players who made an assist in the fixture.

:rtype: dict
"""
if not getattr(self, "finished", False):
return {}

return self._get_players("assists")
try:
return self.stats["assists"]
except KeyError:
return {'a': [], 'h': []}

def get_own_goalscorers(self):
"""Returns all players who scored an own goal in the fixture.

:rtype: dict
"""
if not getattr(self, "finished", False):
return {}

return self._get_players("own_goals")
try:
return self.stats["own_goals"]
except KeyError:
return {'a': [], 'h': []}

def get_yellow_cards(self):
"""Returns all players who received a yellow card in the fixture.

:rtype: dict
"""
if not getattr(self, "finished", False):
return {}

return self._get_players("yellow_cards")
try:
return self.stats["yellow_cards"]
except KeyError:
return {'a': [], 'h': []}

def get_red_cards(self):
"""Returns all players who received a red card in the fixture.

:rtype: dict
"""
if not getattr(self, "finished", False):
return {}

return self._get_players("red_cards")
try:
return self.stats["red_cards"]
except KeyError:
return {'a': [], 'h': []}

def get_penalty_saves(self):
"""Returns all players who saved a penalty in the fixture.

:rtype: dict
"""
if not getattr(self, "finished", False):
return {}

return self._get_players("penalties_saved")
try:
return self.stats["penalties_saved"]
except KeyError:
return {'a': [], 'h': []}

def get_penalty_misses(self):
"""Returns all players who missed a penalty in the fixture.

:rtype: dict
"""
if not getattr(self, "finished", False):
return {}

return self._get_players("penalties_missed")
try:
return self.stats["penalties_missed"]
except KeyError:
return {'a': [], 'h': []}

def get_saves(self):
"""Returns all players who made a save in the fixture.

:rtype: dict
"""
if not getattr(self, "finished", False):
return {}
try:
return self.stats["saves"]
except KeyError:
return {'a': [], 'h': []}

return self._get_players("saves")

@@ -137,12 +144,12 @@ def get_bps(self):

:rtype: dict
"""
if not getattr(self, "finished", False):
return {}

return self._get_players("bps")
try:
return self.stats["bps"]
except KeyError:
return {'a': [], 'h': []}

def __str__(self):
return (f"{team_converter(self.team_h)} vs. "
f"{team_converter(self.team_a)} - "
f"{self.deadline_time_formatted}")
f"{self.kickoff_time}")