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

Adding get_constructors() #60

Merged
merged 6 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 nine 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.get_constructors(year) | Returns information on constructors in a given year | Pandas DataFrame |
|fp.sprint_results(year,race_round) | Returns the sprint qualifying results for a specific race | Pandas DataFrame|


Expand Down
1 change: 1 addition & 0 deletions f1pystats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
pit_stops,
get_drivers,
finishing_status,
get_constructors,
sprint_results)
15 changes: 15 additions & 0 deletions f1pystats/constructor_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Contains all the functions used by get_constructors()"""


class ConstructorInfo:
"""This is the class which stores all the functions for get_constructors"""
def __init__(self, info):
self.info = info

def get_constructors_names(self):
"""Returns the list of constructor names"""
return [i["name"] for i in self.info]

def get_constructors_nationality(self):
"""Returns the list constructor nationalities"""
return [i["nationality"] for i in self.info]
22 changes: 22 additions & 0 deletions f1pystats/f1pystats.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

from .driver_info import DriverInfo

from .constructor_info import ConstructorInfo


def get_drivers(year: int, round_num: int = None):
"""Returns a list of drivers for a specified year"""
Expand Down Expand Up @@ -239,6 +241,26 @@ def sprint_results(year: int, race_round: int):
]
)

def get_constructors(year: int = None):
"""Returns a list of constructors for a specified year"""

if year is None:
url = "http://ergast.com/api/f1/constructors.json"
else:
url = f"http://ergast.com/api/f1/{year}/constructors.json"

json_data = _get_json_content_from_url(url)

constructors_info = ConstructorInfo(json_data["MRData"]["ConstructorTable"]["Constructors"])

constructors_names = constructors_info.get_constructors_names()
constructors_nationalities = constructors_info.get_constructors_nationality()

return pd.DataFrame(
zip(constructors_names, constructors_nationalities),
columns=["Constructor", "Nationality"],
)

def _get_json_content_from_url(url, *args, timeout: int = 15, **kwargs):
"""Returns JSON content from requestsm URL"""
return requests.get(url, *args, timeout=timeout, **kwargs).json()
29 changes: 29 additions & 0 deletions tests/test_constructor_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""This module is responsible for testing the methods in constructor_info.py"""

import json
from f1pystats.constructor_info import ConstructorInfo


class TestConstructorInfo:
"""Contains functions for testing the methods in ConstructorInfo"""

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

constructor = ConstructorInfo(data)

def test_constructors_names(self):
"""Test for the get_constructors_names methods of ConstructorInfo"""
assert self.constructor.get_constructors_names() == ['Ferrari',
'Force India',
'HRT',
'Lotus']

def test_constructors_nationalities(self):
"""Test for the get_constructors_nationality method of ConstructorInfo"""
assert self.constructor.get_constructors_nationality() == ['Italian',
'Indian',
'Spanish',
'Malaysian']
26 changes: 26 additions & 0 deletions tests/test_data/sample_constructor_info_2010.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"constructorId": "ferrari",
"url": "http:\/\/en.wikipedia.org\/wiki\/Scuderia_Ferrari",
"name": "Ferrari",
"nationality": "Italian"
},
{
"constructorId": "force_india",
"url": "http:\/\/en.wikipedia.org\/wiki\/Racing_Point_Force_India",
"name": "Force India",
"nationality": "Indian"
},
{
"constructorId": "hrt",
"url": "http:\/\/en.wikipedia.org\/wiki\/Hispania_Racing",
"name": "HRT",
"nationality": "Spanish"
},
{
"constructorId": "lotus_racing",
"url": "http:\/\/en.wikipedia.org\/wiki\/Lotus_Racing",
"name": "Lotus",
"nationality": "Malaysian"
}
]