Skip to content

Commit

Permalink
Convert to f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mhostetter committed Nov 8, 2021
1 parent 23866df commit 55dd767
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 32 deletions.
2 changes: 1 addition & 1 deletion nhl/conference.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def from_key(cls, id):
return super().from_key(id)

def __repr__(self):
return "<nhl.Conference: {}, ID {}>".format(self.name, self.id)
return f"<nhl.Conference: {self.name}, ID {self.id}>"
2 changes: 1 addition & 1 deletion nhl/division.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def from_key(cls, id):
return super().from_key(id)

def __repr__(self):
return "<nhl.Division: {} Division, ID {}>".format(self.name, self.id)
return f"<nhl.Division: {self.name} Division, ID {self.id}>"
15 changes: 11 additions & 4 deletions nhl/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,27 @@ def name(self):

@property
def subname(self):
return " ({}{})".format(self.subtype, ", {}".format(self.valuename) if self.value else "") if self.subtype else ""
if self.subtype:
value = f", {self.valuename}" if self.value else ""
return f" ({self.subtype}{value})"
else:
return ""

@property
def valuename(self):
if self.type == "PENALTY":
return "{} min".format(self.value) if self.value else ""
return f"{self.value} min" if self.value else ""
elif self.type in ["BLOCKED_SHOT", "MISSED_SHOT", "SHOT", "SAVE", "GOAL", "GOAL_AGAINST"]:
return "{:1.0f} ft".format(self.value) if self.value else ""
return f"{self.value:1.0f} ft" if self.value else ""
else:
return ""

@property
def lead(self):
return self.score[0] - self.score[1] if self.score[0] is not None else None
if self.score[0] is not None:
return self.score[0] - self.score[1]
else:
return None

@property
def by_strength(self):
Expand Down
2 changes: 1 addition & 1 deletion nhl/franchise.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def from_key(cls, id):
return super().from_key(id)

def __repr__(self):
return "<nhl.Franchise: {}, ID {}>".format(self.name, self.id)
return f"<nhl.Franchise: {self.name}, ID {self.id}>"
2 changes: 1 addition & 1 deletion nhl/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def from_key(cls, id):
return super().from_key(id)

def __repr__(self):
return "<nhl.Game: {}, {} ({}) at ({}) {}, {}, ID {}>".format(self.info.description, self.away.abbreviation, self.info.score[1], self.info.score[0], self.home.abbreviation, self.info.date, self.info.id)
return f"<nhl.Game: {self.info.description}, {self.away.abbreviation} ({self.info.score[1]}) at ({self.info.score[0]}) {self.home.abbreviation}, {self.info.date}, ID {self.info.id}>"
# return "<nhl.Game: {} at {}, ID {}>".format(self.away.abbreviation, self.home.abbreviation, self.id)

@property
Expand Down
12 changes: 8 additions & 4 deletions nhl/gameinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def from_key(cls, id):
return super().from_key(id)

def __repr__(self):
return "<nhl.GameInfo: {}, {} ({}) at ({}) {}, {}, ID {}>".format(self.description, self.away_team.abbreviation, self.score[1], self.score[0], self.home_team.abbreviation, self.date, self.id)
return f"<nhl.GameInfo: {self.description}, {self.away_team.abbreviation} ({self.score[1]}) at ({self.score[0]}) {self.home_team.abbreviation}, {self.date}, ID {self.id}>"

@property
def home_score(self):
Expand Down Expand Up @@ -99,13 +99,17 @@ def description(self):
s = "RS"
elif self.season_type == 3:
if self.playoff_round in [1, 2]:
r = "R{}".format(self.playoff_round)
r = f"R{self.playoff_round}"
elif self.playoff_round == 3:
r = "ECF" if self.playoff_series == 1 else " WCF"
else:
r = "SCF"
s = "{} G{}".format(r, self.playoff_game)
return "{:04d}-{:02d} {}".format(self.season, self.season % 100 + 1, s)
s = f"{r} G{self.playoff_game}"

season_start = self.season
season_end = self.season % 100 + 1

return f"{season_start:04d}-{season_end:02d} {s}"

@property
def date(self):
Expand Down
5 changes: 3 additions & 2 deletions nhl/gametime.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def from_key(cls, period, period_sec):
return super().from_key(period, period_sec)

def __repr__(self):
return "<nhl.Gametime: {} {:02d}:{:02d}>".format(self.period_str, *self.period_min_sec)
minute, second = self.period_min_sec
return f"<nhl.Gametime: {self.period_str} {minute:02d}:{second:02d}>"

@property
def sec(self):
Expand All @@ -63,7 +64,7 @@ def period_str(self):
elif self.period == 3:
return "3rd"
else:
return "{}th".format(self.period)
return f"{self.period}th"

@property
def period_min_sec(self):
Expand Down
2 changes: 1 addition & 1 deletion nhl/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def from_key(cls, x, y):
return super().from_key(x, y)

def __repr__(self):
return "<nhl.Location: {:3.0f}, {:3.0f}>".format(self.x, self.y)
return f"<nhl.Location: {self.x:3.0f}, {self.y:3.0f}>"

def distance(self, other):
"""
Expand Down
2 changes: 1 addition & 1 deletion nhl/official.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def from_key(cls, id):
return super().from_key(id)

def __repr__(self):
return "<nhl.Official: {}, ID {}>".format(self.name, self.id)
return f"<nhl.Official: {self.name}, ID {self.id}>"

@property
def first_name(self):
Expand Down
26 changes: 13 additions & 13 deletions nhl/statsapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,42 @@


def conference(id):
json = requests.get("{}/conferences/{}".format(BASE, id)).json()
json = requests.get(f"{BASE}/conferences/{id}").json()
return parse.parse_conference(json["conferences"][0])


def conferences():
json = requests.get("{}/conferences/{}".format(BASE, "")).json()
json = requests.get(f"{BASE}/conferences/").json()
return List(parse.parse_conference(item) for item in json["conferences"])


def division(id):
json = requests.get("{}/divisions/{}".format(BASE, id)).json()
json = requests.get(f"{BASE}/divisions/{id}").json()
return parse.parse_division(json["divisions"][0])


def divisions():
json = requests.get("{}/divisions/{}".format(BASE, "")).json()
json = requests.get(f"{BASE}/divisions/").json()
return List(parse.parse_division(item) for item in json["divisions"])


def franchise(id):
json = requests.get("{}/franchises/{}".format(BASE, id)).json()
json = requests.get(f"{BASE}/franchises/{id}").json()
return parse.parse_franchise(json["franchises"][0])


def franchises():
json = requests.get("{}/franchises/{}".format(BASE, "")).json()
json = requests.get(f"{BASE}/franchises/").json()
return List(parse.parse_franchise(item) for item in json["franchises"])


def game(id):
json = requests.get("{}/game/{}/feed/live".format(BASE, id)).json()
json = requests.get(f"{BASE}/game/{id}/feed/live").json()
return parse.parse_game(json)


def player(id):
json = requests.get("{}/people/{}".format(BASE, id)).json()
json = requests.get(f"{BASE}/people/{id}").json()
return parse.parse_player(json["people"][0])


Expand All @@ -50,25 +50,25 @@ def players(ids):


def team(id):
json = requests.get("{}/teams/{}".format(BASE, id)).json()
json = requests.get(f"{BASE}/teams/{id}").json()
return parse.parse_team(json["teams"][0])


def teams(ids=None):
if isinstance(ids, list):
suffix = ",".join(map(str, ids))
json = requests.get("{}/teams/?teamId={}".format(BASE, suffix)).json()
json = requests.get(f"{BASE}/teams/?teamId={suffix}")).json()
return List(parse.parse_team(item) for item in json["teams"])
else:
json = requests.get("{}/teams/{}".format(BASE, "")).json()
json = requests.get(f"{BASE}/teams/").json()
return List(parse.parse_team(item) for item in json["teams"])


def venue(id):
json = requests.get("{}/venues/{}".format(BASE, id)).json()
json = requests.get(f"{BASE}/venues/{id}").json()
return parse.parse_venue(json["venues"][0])


def venues():
json = requests.get("{}/venues/{}".format(BASE, "")).json()
json = requests.get(f"{BASE}/venues/").json()
return List(parse.parse_venue(item) for item in json["venues"])
17 changes: 17 additions & 0 deletions nhl/statsapi/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ..team import Team
from ..venue import Venue


def _fetch_shifts(game_id, side):
season_id = game_id // 1000000
lower_game_id = game_id % 1000000
Expand All @@ -29,20 +30,24 @@ def _fetch_shifts(game_id, side):
print(result)
return result.text


def _parse_date(date_str):
year = int(date_str.split("-")[0])
month = int(date_str.split("-")[1])
day = int(date_str.split("-")[2])
return datetime.date(year, month, day)


def _parse_height(height_str):
feet = int(height_str.split("\' ")[0])
inches = int(height_str.split("\' ")[1].split("\"")[0])
return feet*12 + inches


def _parse_gametime(period_time):
return 60*int(period_time.split(":")[0]) + int(period_time.split(":")[1])


def parse_conference(json):
id = json["id"]
if Conference.has_key(id): return Conference.from_key(id)
Expand All @@ -53,9 +58,11 @@ def parse_conference(json):
abbreviation = json.get("abbreviation", name[0])
return Conference(id, name, name_short, abbreviation)


def parse_conferences(json):
return List([parse_conference(j) for j in json])


def parse_division(json):
id = json["id"]
if Division.has_key(id): return Division.from_key(id)
Expand All @@ -64,6 +71,7 @@ def parse_division(json):
abbreviation = json["abbreviation"]
return Division(id, name, name_short, abbreviation)


def parse_events(json, info, home_score, away_score, home_shifts, away_shifts, flip_sides):
id = json["about"]["eventIdx"]*10
game_id = info.id
Expand Down Expand Up @@ -181,12 +189,14 @@ def parse_events(json, info, home_score, away_score, home_shifts, away_shifts, f
return events
# return Event(game_id, id, type, subtype, gametime, location, value, score, by_player, with_players, on_player, by_team, on_team, by_players_on_ice, on_players_on_ice)


def parse_franchise(json):
id = json["franchiseId"]
if Franchise.has_key(id): return Franchise.from_key(id)
name = json["teamName"]
return Franchise(id, name)


def parse_game(json):
id = json["gamePk"]
if Game.has_key(id): return Game.from_key(id)
Expand Down Expand Up @@ -273,6 +283,7 @@ def parse_game(json):

return Game(info, home_team, away_team, player_stats, events)


def parse_location(json, gametime, flip_sides):
if "x" in json:
x = int(json["x"])
Expand All @@ -287,12 +298,14 @@ def parse_location(json, gametime, flip_sides):
else:
return None


def parse_official(json):
id = json["id"]
if Official.has_key(id): return Official.from_key(id)
name = json["fullName"]
return Official(id, name)


def parse_player(json):
id = json["id"]
if Player.has_key(id): return Player.from_key(id)
Expand All @@ -310,6 +323,7 @@ def parse_player(json):
return Player(id, name, number, position, height, weight, shoots_catches,
birth_date, birth_city, birth_country)


def parse_shifts(game_id, team_id, player_id, player_number, html):
shifts = List()
tree = BeautifulSoup(html, features="html.parser")
Expand Down Expand Up @@ -337,6 +351,7 @@ def parse_shifts(game_id, team_id, player_id, player_number, html):
break
return shifts


def parse_team(json):
id = json["id"]
if Team.has_key(id): return Team.from_key(id)
Expand All @@ -349,9 +364,11 @@ def parse_team(json):
franchise = parse_franchise(json["franchise"])
return Team(id, location, name, abbreviation, first_year, division, conference, franchise)


def parse_teams(json):
return List([parse_team(j) for j in json])


def parse_venue(json):
id = json["id"]
if Venue.has_key(id): return Venue.from_key(id)
Expand Down
4 changes: 2 additions & 2 deletions nhl/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def from_key(cls, id):
return super().from_key(id)

def __repr__(self):
return "<nhl.Team: {}, {} Division, {} Conference, ID {}>".format(self.name, self.division.name, self.conference.name, self.id)
return f"<nhl.Team: {self.name}, {self.division.name} Division, {self.conference.name} Conference, ID {self.id}>"

@property
def full_name(self):
"""str: Team's full name"""
return "{} {}".format(self.location, self.name)
return f"{self.location} {self.name}"
2 changes: 1 addition & 1 deletion nhl/venue.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def from_key(cls, id):
return super().from_key(id)

def __repr__(self):
return "<nhl.Venue: {}, ID {}>".format(self.name, self.id)
return f"<nhl.Venue: {self.name}, ID {self.id}>"
24 changes: 24 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,27 @@ tests_require =

[bdist_wheel]
universal = 0

[pylint]
disable =
missing-module-docstring,
missing-function-docstring,
line-too-long,
too-many-lines,
bad-whitespace,
invalid-name,
redefined-builtin,
no-else-return,
fixme,
too-many-arguments,
too-many-locals,
too-many-instance-attributes,
too-many-ancestors,
consider-using-enumerate,
unneeded-not,
global-statement,
invalid-unary-operand-type,
eval-used,
too-many-branches,
protected-access
min-similarity-lines = 100

0 comments on commit 55dd767

Please sign in to comment.