Add OpenF1 model classes
This commit is contained in:
0
formula10/openf1/__init__.py
Normal file
0
formula10/openf1/__init__.py
Normal file
0
formula10/openf1/model/__init__.py
Normal file
0
formula10/openf1/model/__init__.py
Normal file
45
formula10/openf1/model/api_driver.py
Normal file
45
formula10/openf1/model/api_driver.py
Normal file
@ -0,0 +1,45 @@
|
||||
from typing import Any, Callable, Dict
|
||||
|
||||
|
||||
class ApiDriver():
|
||||
__type_conversion_map__: Dict[str, Callable[[Any], Any]] = {
|
||||
"session_key": int,
|
||||
"meeting_key": int,
|
||||
"full_name": str,
|
||||
"first_name": str,
|
||||
"last_name": str,
|
||||
"name_acronym": str,
|
||||
"broadcast_name": str,
|
||||
"country_code": str,
|
||||
"headshot_url": str,
|
||||
"driver_number": int,
|
||||
"team_colour": str,
|
||||
"team_name": str
|
||||
}
|
||||
|
||||
def __init__(self, response: dict[str, str]):
|
||||
for key in response:
|
||||
if not hasattr(self, key):
|
||||
raise Exception(f"Mismatch between response data and {type(self).__name__} (key={key})")
|
||||
|
||||
if not key in self.__type_conversion_map__:
|
||||
raise Exception(f"Mismatch between response data and {type(self).__name__}.__type_map__ (key={key})")
|
||||
|
||||
setattr(self, key, self.__type_conversion_map__[key](response[key]))
|
||||
|
||||
print("ApiDriver:", self.__dict__)
|
||||
|
||||
# Set all members to None so hasattr works above
|
||||
|
||||
session_key: int = None # type: ignore
|
||||
meeting_key: int = None # type: ignore
|
||||
full_name: str = None # type: ignore
|
||||
first_name: str = None # type: ignore
|
||||
last_name: str = None # type: ignore
|
||||
name_acronym: str = None # type: ignore
|
||||
broadcast_name: str = None # type: ignore
|
||||
country_code: str = None # type: ignore
|
||||
headshot_url: str = None # type: ignore
|
||||
driver_number: int = None # type: ignore
|
||||
team_colour: str = None # type: ignore
|
||||
team_name: str = None # type: ignore
|
30
formula10/openf1/model/api_position.py
Normal file
30
formula10/openf1/model/api_position.py
Normal file
@ -0,0 +1,30 @@
|
||||
from datetime import datetime
|
||||
from typing import Any, Callable, Dict
|
||||
|
||||
|
||||
class ApiPosition():
|
||||
__type_conversion_map__: Dict[str, Callable[[Any], Any]] = {
|
||||
"session_key": int,
|
||||
"meeting_key": int,
|
||||
"driver_number": int,
|
||||
"date": lambda date: datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%f"),
|
||||
"position": int
|
||||
}
|
||||
|
||||
def __init__(self, response: dict[str, str]):
|
||||
for key in response:
|
||||
if not hasattr(self, key):
|
||||
raise Exception(f"Mismatch between response data and {type(self).__name__} (key={key})")
|
||||
|
||||
if not key in self.__type_conversion_map__:
|
||||
raise Exception(f"Mismatch between response data and {type(self).__name__}.__type_map__ (key={key})")
|
||||
|
||||
setattr(self, key, self.__type_conversion_map__[key](response[key]))
|
||||
|
||||
print("ApiPosition:", self.__dict__)
|
||||
|
||||
session_key: int = None # type: ignore
|
||||
meeting_key: int = None # type: ignore
|
||||
driver_number: int = None # type: ignore
|
||||
date: datetime = None # type: ignore
|
||||
position: int = None # type: ignore
|
48
formula10/openf1/model/api_session.py
Normal file
48
formula10/openf1/model/api_session.py
Normal file
@ -0,0 +1,48 @@
|
||||
from datetime import datetime, time
|
||||
from typing import Any, Callable, Dict
|
||||
|
||||
|
||||
class ApiSession():
|
||||
__type_conversion_map__: Dict[str, Callable[[Any], Any]] = {
|
||||
"location": str,
|
||||
"country_key": int,
|
||||
"country_code": str,
|
||||
"country_name": str,
|
||||
"circuit_key": int,
|
||||
"circuit_short_name": str,
|
||||
"session_type": str,
|
||||
"session_name": str,
|
||||
"date_start": lambda date: datetime.strptime(date, "%Y-%m-%dT%H:%M:%S"),
|
||||
"date_end": lambda date: datetime.strptime(date, "%Y-%m-%dT%H:%M:%S"),
|
||||
"gmt_offset": lambda time: datetime.strptime(time, "%H:%M:%S").time(),
|
||||
"session_key": int,
|
||||
"meeting_key": int,
|
||||
"year": int
|
||||
}
|
||||
|
||||
def __init__(self, response: dict[str, str]):
|
||||
for key in response:
|
||||
if not hasattr(self, key):
|
||||
raise Exception(f"Mismatch between response data and {type(self).__name__} (key={key})")
|
||||
|
||||
if not key in self.__type_conversion_map__:
|
||||
raise Exception(f"Mismatch between response data and {type(self).__name__}.__type_map__ (key={key})")
|
||||
|
||||
setattr(self, key, self.__type_conversion_map__[key](response[key]))
|
||||
|
||||
print("ApiSession:", self.__dict__)
|
||||
|
||||
location: str = None # type: ignore
|
||||
country_key: int = None # type: ignore
|
||||
country_code: str = None # type: ignore
|
||||
country_name: str = None # type: ignore
|
||||
circuit_key: int = None # type: ignore
|
||||
circuit_short_name: str = None # type: ignore
|
||||
session_type: str = None # type: ignore
|
||||
session_name: str = None # type: ignore
|
||||
date_start: datetime = None # type: ignore
|
||||
date_end: datetime = None # type: ignore
|
||||
gmt_offset: time = None # type: ignore
|
||||
session_key: int = None # type: ignore
|
||||
meeting_key: int = None # type: ignore
|
||||
year: int = None # type: ignore
|
Reference in New Issue
Block a user