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

Refactored with classes and objects #23

Merged
merged 2 commits into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions f1pystats/constructor_results.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
'''Contains all functions used by constructor_standings()'''


def get_constructor_positions(constructor_info):
'''Returns the constructor positions'''
return [i["position"] for i in constructor_info]


def get_constructor_names(constructor_info):
'''Returns the constructor names'''
return [i["Constructor"]["name"] for i in constructor_info]


def get_constructor_points(constructor_info):
'''Returns the constructor points'''
return [i["points"] for i in constructor_info]


def get_constructor_wins(constructor_info):
'''Returns the constructor wins'''
return [i["wins"] for i in constructor_info]


def get_constructor_nationality(constructor_info):
'''Returns the constructor nationality'''
return [i["Constructor"]["nationality"] for i in constructor_info]
class ConstructorResults:
'''Contains all methods used to get constructor results'''
def __init__(self, results):
self.results = results

def get_constructor_positions(self):
'''Returns the constructor positions'''
return [i["position"] for i in self.results]

def get_constructor_names(self):
'''Returns the constructor names'''
return [i["Constructor"]["name"] for i in self.results]

def get_constructor_points(self):
'''Returns the constructor points'''
return [i["points"] for i in self.results]

def get_constructor_wins(self):
'''Returns the constructor wins'''
return [i["wins"] for i in self.results]

def get_constructor_nationality(self):
'''Returns the constructor nationality'''
return [i["Constructor"]["nationality"] for i in self.results]
58 changes: 29 additions & 29 deletions f1pystats/driver_results.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
'''Contains all functions used by driver_standings()'''


def get_driver_positions(driver_info):
'''Returns the driver positions'''
return [i["position"] for i in driver_info]


def get_driver_names(driver_info):
'''Returns the driver names'''
return [" ".join([i["Driver"]["givenName"],
i["Driver"]["familyName"]]) for i in driver_info]


def get_driver_points(driver_info):
'''Returns the points obtained by each driver'''
return [i["points"] for i in driver_info]


def get_driver_teams(driver_info):
'''Returns the driver's team name'''
return [i["Constructors"][0]["name"] for i in driver_info]


def get_driver_nationality(driver_info):
'''Returns the driver's nationality'''
return [i["Driver"]["nationality"] for i in driver_info]


def get_driver_wins(driver_info):
'''Returns the total wins for each driver'''
return [i["wins"] for i in driver_info]
class DriverResults:
'''Contains all methods used to get driver results'''
def __init__(self, results):
self.results = results

def get_driver_positions(self):
'''Returns the driver positions'''
return [i["position"] for i in self.results]

def get_driver_names(self):
'''Returns the driver names'''
return [" ".join([i["Driver"]["givenName"],
i["Driver"]["familyName"]]) for i in self.results]

def get_driver_points(self):
'''Returns the points obtained by each driver'''
return [i["points"] for i in self.results]

def get_driver_teams(self):
'''Returns the driver's team name'''
return [i["Constructors"][0]["name"] for i in self.results]

def get_driver_nationality(self):
'''Returns the driver's nationality'''
return [i["Driver"]["nationality"] for i in self.results]

def get_driver_wins(self):
'''Returns the total wins for each driver'''
return [i["wins"] for i in self.results]
113 changes: 47 additions & 66 deletions f1pystats/f1pystats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,13 @@
import requests
import pandas as pd

from .driver_results import (
get_driver_positions,
get_driver_names,
get_driver_points,
get_driver_teams,
get_driver_wins,
get_driver_nationality,
)

from .constructor_results import (
get_constructor_names,
get_constructor_nationality,
get_constructor_points,
get_constructor_positions,
get_constructor_wins,
)

from .race_winners import (
get_grands_prix,
get_race_dates,
get_race_winners,
get_winning_constructors,
get_race_laps,
get_winner_start_positions,
get_race_times,
)

from .race_schedule import (
get_race_round,
get_race_names,
get_race_circuits,
get_race_schedule_dates,
get_race_schedule_times,
get_race_locality,
get_race_countries,
)
from .driver_results import DriverResults

from .constructor_results import ConstructorResults

from .race_winners import RaceWinners

from .race_schedule import RaceSchedule


def driver_standings(year: int):
Expand All @@ -47,16 +18,19 @@ def driver_standings(year: int):

page = requests.get(link, timeout=15)
json_data = page.json()
standings = json_data["MRData"]["StandingsTable"]["StandingsLists"][0][
standings_json = json_data["MRData"]["StandingsTable"][
"StandingsLists"][0][
"DriverStandings"
]

positions = get_driver_positions(standings)
names = get_driver_names(standings)
teams = get_driver_teams(standings)
nationalities = get_driver_nationality(standings)
points = get_driver_points(standings)
wins = get_driver_wins(standings)
d_res = DriverResults(standings_json)

positions = d_res.get_driver_positions()
names = d_res.get_driver_names()
teams = d_res.get_driver_teams()
nationalities = d_res.get_driver_nationality()
points = d_res.get_driver_points()
wins = d_res.get_driver_wins()

wdc_df = pd.DataFrame(
list(zip(positions, names, nationalities, teams, points, wins)),
Expand All @@ -72,15 +46,18 @@ def constructor_standings(year: int):
page = requests.get(link, timeout=15)

json_data = page.json()
standings = json_data["MRData"]["StandingsTable"]["StandingsLists"][0][
standings_json = json_data["MRData"]["StandingsTable"][
"StandingsLists"][0][
"ConstructorStandings"
]

positions = get_constructor_positions(standings)
teams = get_constructor_names(standings)
nationality = get_constructor_nationality(standings)
points = get_constructor_points(standings)
wins = get_constructor_wins(standings)
c_res = ConstructorResults(standings_json)

positions = c_res.get_constructor_positions()
teams = c_res.get_constructor_names()
nationality = c_res.get_constructor_nationality()
points = c_res.get_constructor_points()
wins = c_res.get_constructor_wins()

wcc_df = pd.DataFrame(
list(zip(positions, teams, nationality, points, wins)),
Expand All @@ -96,15 +73,17 @@ def race_winners(year: int):
page = requests.get(link, timeout=15)

json_data = page.json()
results = json_data["MRData"]["RaceTable"]["Races"]
results_json = json_data["MRData"]["RaceTable"]["Races"]

r_winners = RaceWinners(results_json)

grands_prix = get_grands_prix(results)
race_dates = get_race_dates(results)
winners = get_race_winners(results)
winning_constructors = get_winning_constructors(results)
race_laps = get_race_laps(results)
race_times = get_race_times(results)
start_positions = get_winner_start_positions(results)
grands_prix = r_winners.get_grands_prix()
race_dates = r_winners.get_race_dates()
winners = r_winners.get_race_winners()
winning_constructors = r_winners.get_winning_constructors()
race_laps = r_winners.get_race_laps()
race_times = r_winners.get_race_times()
start_positions = r_winners.get_winner_start_positions()

wcc_df = pd.DataFrame(
list(
Expand Down Expand Up @@ -132,15 +111,17 @@ def race_table(year: int):
page = requests.get(link, timeout=15)

json_data = page.json()
r_schedule = json_data["MRData"]["RaceTable"]["Races"]

race_round = get_race_round(r_schedule)
race_name = get_race_names(r_schedule)
race_circuits = get_race_circuits(r_schedule)
race_schedule_date = get_race_schedule_dates(r_schedule)
race_schedule_time = get_race_schedule_times(r_schedule)
race_locality = get_race_locality(r_schedule)
race_country = get_race_countries(r_schedule)
schedule_json = json_data["MRData"]["RaceTable"]["Races"]

r_sched = RaceSchedule(schedule_json)

race_round = r_sched.get_race_round()
race_name = r_sched.get_race_names()
race_circuits = r_sched.get_race_circuits()
race_schedule_date = r_sched.get_race_schedule_dates()
race_schedule_time = r_sched.get_race_schedule_times()
race_locality = r_sched.get_race_locality()
race_country = r_sched.get_race_countries()

wcc_df = pd.DataFrame(
list(
Expand Down
65 changes: 32 additions & 33 deletions f1pystats/race_schedule.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
'''Contains all functions used by race_table()'''


def get_race_round(table):
'''Returns the round number for each race'''
return [i["round"] for i in table]


def get_race_names(table):
'''Returns the race names'''
return [i["raceName"] for i in table]


def get_race_schedule_dates(table):
'''Returns the race dates'''
return [i["date"] for i in table]


def get_race_circuits(table):
'''Returns the race circuit names'''
return [i["Circuit"]["circuitName"] for i in table]


def get_race_schedule_times(table):
'''Returns race times'''
return [i["time"] for i in table]


def get_race_countries(table):
'''Returns the host country of each race'''
return [i["Circuit"]["Location"]["country"] for i in table]


def get_race_locality(table):
'''Returns the local area hosting each race'''
return [i["Circuit"]["Location"]["locality"] for i in table]
class RaceSchedule:
'''Contains all methods used to get the race schedule'''
def __init__(self, schedule):
self.schedule = schedule

def get_race_round(self):
'''Returns the round number for each race'''
return [i["round"] for i in self.schedule]

def get_race_names(self):
'''Returns the race names'''
return [i["raceName"] for i in self.schedule]

def get_race_schedule_dates(self):
'''Returns the race dates'''
return [i["date"] for i in self.schedule]

def get_race_circuits(self):
'''Returns the race circuit names'''
return [i["Circuit"]["circuitName"] for i in self.schedule]

def get_race_schedule_times(self):
'''Returns race times'''
return [i["time"] for i in self.schedule]

def get_race_countries(self):
'''Returns the host country of each race'''
return [i["Circuit"]["Location"]["country"] for i in self.schedule]

def get_race_locality(self):
'''Returns the local area hosting each race'''
return [i["Circuit"]["Location"]["locality"] for i in self.schedule]
Loading