Skip to content

Commit

Permalink
Add __str__ to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
amosbastian committed Jun 9, 2018
1 parent cedc65e commit 797fca3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
18 changes: 12 additions & 6 deletions football/models/competition.py
@@ -1,6 +1,8 @@
import requests

from .fixture import Fixture
from .table import Table
from .team import Team
from ..utils import headers


Expand Down Expand Up @@ -29,18 +31,22 @@ def fixtures(self):
Returns all current fixtures of the competition.
"""
response = requests.get(self._fixtures_url, headers=headers()).json()
return [Fixture(fixture) for fixture in response]
return [Fixture(fixture) for fixture in response["fixtures"]]

def teams(self):
"""
Returns all teams currently participating in the competition.
"""
response = requests.get(self._teams_url, headers=headers())
return response.json()
response = requests.get(self._teams_url, headers=headers()).json()
return [Team(team) for team in response["teams"]]

def league_table(self):
def table(self):
"""
Returns the current league table of the competition.
"""
response = requests.get(self._league_table_url, headers=headers())
return response.json()
response = requests.get(
self._league_table_url, headers=headers()).json()
return Table(response)

def __str__(self):
return self.name
8 changes: 8 additions & 0 deletions football/models/fixture.py
Expand Up @@ -18,6 +18,7 @@ def __init__(self, fixture):
self.status = fixture["status"]
self.winner = self._winner()

# Get result depending if required data available
if "halfTime" in fixture["result"].keys():
self.away_goals_ht = fixture["result"]["halfTime"]["goalsAwayTeam"]
self.home_goals_ht = fixture["result"]["halfTime"]["goalsHomeTeam"]
Expand All @@ -34,3 +35,10 @@ def _winner(self):
elif self.away_goals > self.home_goals:
return self.away_team
return None

def __str__(self):
if self.winner:
return (f"{self.home_team} {self.home_goals}-{self.away_goals} "
f"{self.away_team} - {self.date}")
else:
return f"{self.home_team} vs. {self.away_team} - {self.date}"
3 changes: 3 additions & 0 deletions football/models/player.py
Expand Up @@ -35,3 +35,6 @@ def _team(self):
response = requests.get(
f"{self.API_URL}teams/{self.team_id}", headers=headers()).json()
return response["name"]

def __str__(self):
return (f"{self.number} - {self.name} - {self.position} - {self.team}")
15 changes: 6 additions & 9 deletions football/models/team.py
Expand Up @@ -28,18 +28,15 @@ def _players(self):
Returns a list of Player objects.
"""
response = requests.get(self.players_url, headers=headers()).json()
try:
return [Player(player, self.team_id, self.name)
for player in response["players"]]
except KeyError:
return []
return [Player(player, self.team_id, self.name)
for player in response["players"]]

def _fixtures(self):
"""
Returns a list of Fixture objects.
"""
response = requests.get(self.fixtures_url, headers=headers()).json()
try:
return [Fixture(fixture) for fixture in response["fixtures"]]
except KeyError:
return []
return [Fixture(fixture) for fixture in response["fixtures"]]

def __str__(self):
return self.name

0 comments on commit 797fca3

Please sign in to comment.