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

Added Sprint Races #59

Merged
merged 5 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The package currently contains eight functions
| fp.pit_stops(year, round, stop_number) | Returns the pit stop info for a specific race/pit stop | Pandas DataFrame |
| fp.finishing_status(year,race_round) | Returns the finishing status of races in a year (or a particular race if specified) | Pandas DataFrame |
|fp.get_drivers(year, race_round) | Returns information on drivers in a given year (or a particular race if specified) | Pandas DataFrame |
|fp.sprint_results(year,race_round) | Returns the sprint qualifying results for a specific year and round | Pandas DataFrame|


# Contributions
Expand Down
3 changes: 2 additions & 1 deletion f1pystats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
lap_times,
pit_stops,
get_drivers,
finishing_status)
finishing_status,
sprint_results)
30 changes: 30 additions & 0 deletions f1pystats/f1pystats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import requests
import pandas as pd

from .sprint_results import SprintResults

from .pit_stops import PitStops

from .driver_results import DriverResults
Expand All @@ -14,6 +16,7 @@
from .race_schedule import RaceSchedule

from .lap_times import LapTimes

from .finishing_status import FinishingStatus

from .driver_info import DriverInfo
Expand Down Expand Up @@ -208,6 +211,33 @@ def finishing_status(year: int, race_round: int = 0):
zip(status_id, status_info, status_count), columns=["StatusId", "Status", "Count"]
)

def sprint_results(year: int, race_round: int):
"""Returns the sprint qualifying results for a year and round"""
json_data=_get_json_content_from_url(
f"http://ergast.com/api/f1/{year}/{race_round}/sprint.json"
)
if len(json_data["MRData"]["RaceTable"]["Races"])==0 :
raise Exception("ERR: No Sprint Race found for this Grand Prix")
s_results=SprintResults(json_data["MRData"]["RaceTable"]["Races"][0]["SprintResults"])
driver_pos=s_results.get_driver_pos()
driver_name=s_results.get_driver_name()
driver_team=s_results.get_driver_team()
driver_status=s_results.get_driver_status()
driver_number=s_results.get_driver_number()
driver_laps=s_results.get_laps()
driver_grid=s_results.get_driver_grid()
driver_time=s_results.get_driver_time()
driver_points=s_results.get_driver_points()
return pd.DataFrame(
zip(
driver_pos,driver_name, driver_number,driver_team,
driver_laps, driver_grid,driver_status, driver_time, driver_points
),
columns=[
"Position", "Name", "Driver Number", "Constructor",
"Laps", "Grid","Status", "Time", "Points"
]
)

def _get_json_content_from_url(url, *args, timeout: int = 15, **kwargs):
"""Returns JSON content from requestsm URL"""
Expand Down
38 changes: 38 additions & 0 deletions f1pystats/sprint_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Contains all functions used by sprint_results()"""


class SprintResults():
"""Contains all the methods used to get sprint qualifying results"""

def __init__(self, results):
self.results = results
def get_driver_pos(self):
"""Returns driver position"""
return [i["position"] for i in self.results]
def get_driver_name(self):
"""Returns driver name"""
return [i["Driver"]["givenName"]+" "+i["Driver"]["familyName"] for i in self.results]
def get_driver_team(self):
"""Returns the driver team name"""
return [i["Constructor"]["name"] for i in self.results]
def get_driver_status(self):
"""Returns the driver sprint status"""
return [i["status"] for i in self.results]
def get_driver_number(self):
"""Returns the driver number"""
return [i["Driver"]["permanentNumber"] for i in self.results]
def get_laps(self):
"""Returns the driver sprint laps"""
return [i["laps"] for i in self.results]
def get_driver_grid(self):
"""Returns the driver sprint grid status"""
return [i["grid"] for i in self.results]
def get_driver_time(self):
"""Returns the driver sprint time"""
time=[]
for i in self.results:
time.append(i.get("Time",{"time":"DNF"})['time'])
alec-kr marked this conversation as resolved.
Show resolved Hide resolved
return time
def get_driver_points(self):
"""Returns the driver sprint points"""
return [i["points"] for i in self.results]
107 changes: 107 additions & 0 deletions tests/test_data/top_3_sprint_2022_11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
[
{
"number": "1",
"position": "1",
"positionText": "1",
"points": "8",
"Driver": {
"driverId": "max_verstappen",
"permanentNumber": "33",
"code": "VER",
"url": "http://en.wikipedia.org/wiki/Max_Verstappen",
"givenName": "Max",
"familyName": "Verstappen",
"dateOfBirth": "1997-09-30",
"nationality": "Dutch"
},
"Constructor": {
"constructorId": "red_bull",
"url": "http://en.wikipedia.org/wiki/Red_Bull_Racing",
"name": "Red Bull",
"nationality": "Austrian"
},
"grid": "1",
"laps": "23",
"status": "Finished",
"Time": {
"millis": "1590059",
"time": "26:30.059"
},
"FastestLap": {
"lap": "5",
"Time": {
"time": "1:08.455"
}
}
},
{
"number": "16",
"position": "2",
"positionText": "2",
"points": "7",
"Driver": {
"driverId": "leclerc",
"permanentNumber": "16",
"code": "LEC",
"url": "http://en.wikipedia.org/wiki/Charles_Leclerc",
"givenName": "Charles",
"familyName": "Leclerc",
"dateOfBirth": "1997-10-16",
"nationality": "Monegasque"
},
"Constructor": {
"constructorId": "ferrari",
"url": "http://en.wikipedia.org/wiki/Scuderia_Ferrari",
"name": "Ferrari",
"nationality": "Italian"
},
"grid": "2",
"laps": "23",
"status": "Finished",
"Time": {
"millis": "1591734",
"time": "+1.675"
},
"FastestLap": {
"lap": "4",
"Time": {
"time": "1:08.321"
}
}
},
{
"number": "55",
"position": "3",
"positionText": "3",
"points": "6",
"Driver": {
"driverId": "sainz",
"permanentNumber": "55",
"code": "SAI",
"url": "http://en.wikipedia.org/wiki/Carlos_Sainz_Jr.",
"givenName": "Carlos",
"familyName": "Sainz",
"dateOfBirth": "1994-09-01",
"nationality": "Spanish"
},
"Constructor": {
"constructorId": "ferrari",
"url": "http://en.wikipedia.org/wiki/Scuderia_Ferrari",
"name": "Ferrari",
"nationality": "Italian"
},
"grid": "3",
"laps": "23",
"status": "Finished",
"Time": {
"millis": "1595703",
"time": "+5.644"
},
"FastestLap": {
"lap": "4",
"Time": {
"time": "1:08.409"
}
}
}
]
94 changes: 94 additions & 0 deletions tests/test_sprint_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""This module is responsible for testing the methods in sprint_results.py"""

import json
from f1pystats.sprint_results import SprintResults


class TestSprintResults:
"""Contains functions for testing the methods in SprintResults"""

data = ""
with open("tests/test_data/top_3_sprint_2022_11.json", encoding='utf-8') as f:
data = json.load(f)
f.close()

s_result = SprintResults(data)

def test_get_driver_pos(self):
"""Test the driver position returned by get_driver_pos"""
driver_pos = self.s_result.get_driver_pos()
assert driver_pos == [
"1", "2", "3"
]

def test_get_driver_name(self):
"""Test the driver name returned by get_driver_name"""
name = self.s_result.get_driver_name()
assert name == [
"Max Verstappen",
"Charles Leclerc",
"Carlos Sainz"
]

def test_get_driver_team(self):
"""Test the driver team returned by get_driver_team"""
team = self.s_result.get_driver_team()
assert team == [
"Red Bull",
"Ferrari",
"Ferrari"
]

def test_get_driver_status(self):
"""Test the driver status returned by get_driver_status"""
status = self.s_result.get_driver_status()
assert status == [
"Finished",
"Finished",
"Finished"
]

def test_get_driver_number(self):
"""Test the driver number returned by get_driver_number"""
number = self.s_result.get_driver_number()
assert number == [
"33",
"16",
"55"
]

def test_get_laps(self):
"""Test the driver laps returned by get_laps"""
laps = self.s_result.get_laps()
assert laps == [
"23",
"23",
"23"
]

def test_get_driver_grid(self):
"""Test the driver grid position returned by get_driver_grid"""
grid = self.s_result.get_driver_grid()
assert grid == [
"1",
"2",
"3"
]

def test_get_driver_time(self):
"""Test the driver time returned by get_driver_time"""
driver_time = self.s_result.get_driver_time()
assert driver_time == [
"26:30.059",
"+1.675",
"+5.644"
]

def test_get_driver_points(self):
"""Test the driver points returned by get_driver_points"""
points = self.s_result.get_driver_points()
assert points == [
"8",
"7",
"6"
]