proper type hinting for tool functions
Browse files
tools.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
"""
|
2 |
-
|
3 |
|
4 |
Tools to implement
|
5 |
- driver info
|
@@ -25,8 +25,23 @@ from utils.constants import (
|
|
25 |
gp = Union[str, int]
|
26 |
session_type = Union[str, int, None]
|
27 |
|
|
|
|
|
28 |
def get_session(year: int, round: gp, session_type: session_type) -> Session:
|
29 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Check if session type is valid
|
32 |
if isinstance(session_type, str):
|
@@ -36,12 +51,28 @@ def get_session(year: int, round: gp, session_type: session_type) -> Session:
|
|
36 |
return fastf1.get_session(year, round, session_type)
|
37 |
|
38 |
def get_season_calendar(year: int) -> str:
|
39 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
season_calendar = fastf1.get_event_schedule(year)
|
41 |
return parser_utils.parse_season_calendar(season_calendar)
|
42 |
|
43 |
def get_event_info(year: int, round: gp, format: str) -> str:
|
44 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
event = fastf1.get_session(year, round, "race").event # Event object is the same for all sessions, so hardcode "race"
|
47 |
if format == "human":
|
@@ -53,12 +84,37 @@ def get_event_info(year: int, round: gp, format: str) -> str:
|
|
53 |
|
54 |
|
55 |
def get_constructor_standings(year: int) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
pass
|
57 |
|
58 |
-
def get_driver_standings(year: int) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
pass
|
60 |
|
61 |
def driver_championship_standings(year: int, driver_name: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
with open("assets/driver_abbreviations.json") as f:
|
64 |
driver_abbreviations = json.load(f)
|
@@ -72,6 +128,15 @@ def driver_championship_standings(year: int, driver_name: str) -> str:
|
|
72 |
return standings_string
|
73 |
|
74 |
def constructor_championship_standings(year: int, constructor_name: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
team_mapping = {
|
77 |
"McLaren": "McLaren",
|
@@ -95,7 +160,18 @@ def constructor_championship_standings(year: int, constructor_name: str) -> str:
|
|
95 |
standings_string = f"{constructor_name} are {constructor_standing['position'].iloc[0]}{suffix} with {constructor_standing['points'].iloc[0]} points and {constructor_standing['wins'].iloc[0]} wins"
|
96 |
return standings_string
|
97 |
|
98 |
-
def track_visualization(year: int, round: gp, visualization_type: str, driver_name: str) -> Image:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
session = get_session(year, round, "race")
|
101 |
session.load()
|
@@ -108,6 +184,19 @@ def track_visualization(year: int, round: gp, visualization_type: str, driver_na
|
|
108 |
return track_utils.create_track_gear_visualization(session)
|
109 |
|
110 |
def get_session_results(year: int, round: gp, session_type: session_type) -> pd.DataFrame:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
try:
|
113 |
session = get_session(year, round, session_type)
|
@@ -130,10 +219,23 @@ def get_session_results(year: int, round: gp, session_type: session_type) -> pd.
|
|
130 |
return df
|
131 |
|
132 |
def get_driver_info(driver_name: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
driver = DRIVER_DETAILS[driver_name]
|
134 |
driver_info_string = f"{driver_name} ({driver['birth_date']})\n{driver['team']} #{driver['number']}\n{driver['nationality']}\n\n{driver['summary']}"
|
135 |
return driver_info_string
|
136 |
|
|
|
|
|
|
|
|
|
137 |
|
138 |
|
139 |
if __name__ == "__main__":
|
|
|
1 |
"""
|
2 |
+
This module provides tools for interacting with Formula 1 data using the FastF1 library.
|
3 |
|
4 |
Tools to implement
|
5 |
- driver info
|
|
|
25 |
gp = Union[str, int]
|
26 |
session_type = Union[str, int, None]
|
27 |
|
28 |
+
### FastF1 tools ###
|
29 |
+
|
30 |
def get_session(year: int, round: gp, session_type: session_type) -> Session:
|
31 |
+
"""Retrieve a specific Formula 1 session.
|
32 |
+
|
33 |
+
Args:
|
34 |
+
year (int): The season year (e.g., 2024)
|
35 |
+
round (str | int): The race round number or name (e.g., 1 or 'Monaco')
|
36 |
+
session_type (str | int | None): Type of session (e.g., 'FP1', 'Q', 'R')
|
37 |
+
|
38 |
+
Returns:
|
39 |
+
Session: A FastF1 Session object for the specified parameters
|
40 |
+
|
41 |
+
Note:
|
42 |
+
If session_type is a string and not in AVAILABLE_SESSION_TYPES,
|
43 |
+
returns an error message string instead.
|
44 |
+
"""
|
45 |
|
46 |
# Check if session type is valid
|
47 |
if isinstance(session_type, str):
|
|
|
51 |
return fastf1.get_session(year, round, session_type)
|
52 |
|
53 |
def get_season_calendar(year: int) -> str:
|
54 |
+
"""Get the complete race calendar for a specific F1 season.
|
55 |
+
|
56 |
+
Args:
|
57 |
+
year (int): The season year to get the calendar for
|
58 |
+
|
59 |
+
Returns:
|
60 |
+
str: Formatted string containing the season calendar
|
61 |
+
"""
|
62 |
season_calendar = fastf1.get_event_schedule(year)
|
63 |
return parser_utils.parse_season_calendar(season_calendar)
|
64 |
|
65 |
def get_event_info(year: int, round: gp, format: str) -> str:
|
66 |
+
"""Retrieve information about a specific Formula 1 event.
|
67 |
+
|
68 |
+
Args:
|
69 |
+
year (int): The season year
|
70 |
+
round (str | int): The race round number or name
|
71 |
+
format (str): Output format ('human' for readable text, 'LLM' for structured data)
|
72 |
+
|
73 |
+
Returns:
|
74 |
+
str: Formatted event information based on the specified format
|
75 |
+
"""
|
76 |
|
77 |
event = fastf1.get_session(year, round, "race").event # Event object is the same for all sessions, so hardcode "race"
|
78 |
if format == "human":
|
|
|
84 |
|
85 |
|
86 |
def get_constructor_standings(year: int) -> str:
|
87 |
+
"""Retrieve constructor championship standings for a given year.
|
88 |
+
|
89 |
+
Args:
|
90 |
+
year (int): The season year
|
91 |
+
|
92 |
+
Returns:
|
93 |
+
str: Constructor championship standings
|
94 |
+
"""
|
95 |
pass
|
96 |
|
97 |
+
def get_driver_standings(year: int) -> str:
|
98 |
+
"""Retrieve driver championship standings for a given year.
|
99 |
+
|
100 |
+
Args:
|
101 |
+
year (int): The season year
|
102 |
+
|
103 |
+
Returns:
|
104 |
+
str: Driver championship standings
|
105 |
+
"""
|
106 |
pass
|
107 |
|
108 |
def driver_championship_standings(year: int, driver_name: str) -> str:
|
109 |
+
"""Get the championship standing for a specific driver in a given year.
|
110 |
+
|
111 |
+
Args:
|
112 |
+
year (int): The season year
|
113 |
+
driver_name (str): Full name of the driver (e.g., 'Lewis Hamilton')
|
114 |
+
|
115 |
+
Returns:
|
116 |
+
str: Formatted string with driver's position, points, and wins
|
117 |
+
"""
|
118 |
|
119 |
with open("assets/driver_abbreviations.json") as f:
|
120 |
driver_abbreviations = json.load(f)
|
|
|
128 |
return standings_string
|
129 |
|
130 |
def constructor_championship_standings(year: int, constructor_name: str) -> str:
|
131 |
+
"""Get the championship standing for a specific constructor in a given year.
|
132 |
+
|
133 |
+
Args:
|
134 |
+
year (int): The season year
|
135 |
+
constructor_name (str): Name of the constructor team (e.g., 'Mercedes')
|
136 |
+
|
137 |
+
Returns:
|
138 |
+
str: Formatted string with constructor's position, points, and wins
|
139 |
+
"""
|
140 |
|
141 |
team_mapping = {
|
142 |
"McLaren": "McLaren",
|
|
|
160 |
standings_string = f"{constructor_name} are {constructor_standing['position'].iloc[0]}{suffix} with {constructor_standing['points'].iloc[0]} points and {constructor_standing['wins'].iloc[0]} wins"
|
161 |
return standings_string
|
162 |
|
163 |
+
def track_visualization(year: int, round: gp, visualization_type: str, driver_name: str) -> Image.Image:
|
164 |
+
"""Generate a visualization of the track with specified data.
|
165 |
+
|
166 |
+
Args:
|
167 |
+
year (int): The season year
|
168 |
+
round (str | int): The race round number or name
|
169 |
+
visualization_type (str): Type of visualization ('speed', 'corners', or 'gear')
|
170 |
+
driver_name (str): Name of the driver for driver-specific visualizations
|
171 |
+
|
172 |
+
Returns:
|
173 |
+
Image.Image: A PIL Image object containing the visualization
|
174 |
+
"""
|
175 |
|
176 |
session = get_session(year, round, "race")
|
177 |
session.load()
|
|
|
184 |
return track_utils.create_track_gear_visualization(session)
|
185 |
|
186 |
def get_session_results(year: int, round: gp, session_type: session_type) -> pd.DataFrame:
|
187 |
+
"""Retrieve and format the results of a specific session.
|
188 |
+
|
189 |
+
Args:
|
190 |
+
year (int): The season year
|
191 |
+
round (str | int): The race round number or name
|
192 |
+
session_type (str | int | None): Type of session (e.g., 'Q', 'R', 'Sprint')
|
193 |
+
|
194 |
+
Returns:
|
195 |
+
pd.DataFrame: DataFrame containing the session results
|
196 |
+
|
197 |
+
Raises:
|
198 |
+
gr.Error: If the session type is not supported for the specified round
|
199 |
+
"""
|
200 |
|
201 |
try:
|
202 |
session = get_session(year, round, session_type)
|
|
|
219 |
return df
|
220 |
|
221 |
def get_driver_info(driver_name: str) -> str:
|
222 |
+
"""Retrieve detailed information about a specific driver.
|
223 |
+
|
224 |
+
Args:
|
225 |
+
driver_name (str): Full name of the driver (e.g., 'Max Verstappen')
|
226 |
+
|
227 |
+
Returns:
|
228 |
+
str: Formatted string with driver's details including name, team, number,
|
229 |
+
nationality, and a brief summary
|
230 |
+
"""
|
231 |
driver = DRIVER_DETAILS[driver_name]
|
232 |
driver_info_string = f"{driver_name} ({driver['birth_date']})\n{driver['team']} #{driver['number']}\n{driver['nationality']}\n\n{driver['summary']}"
|
233 |
return driver_info_string
|
234 |
|
235 |
+
### OpenF1 tools ###
|
236 |
+
|
237 |
+
|
238 |
+
|
239 |
|
240 |
|
241 |
if __name__ == "__main__":
|