Files
f1-circuits/models/circuit.py
T
jschoubben 11c17cb9ad Match a race's circuit on Jolpica's circuitId, not its name
Jolpica renamed Losail to Lusail International Circuit, so the name match
found no circuit and every Qatar Grand Prix poster, thumb and track image
answered 404 (reported by Brad Johnston for 2025 and 2026; every season
since 2021 failed). Names drift, ids do not: each circuit in circuits.json
now carries its ergastId, the lookup keys on it, and the poster's zoom
fallback resolves our circuit slug the same way. The name match also sent
Caesars Palace (1981-82) to the Las Vegas Strip circuit; by id it resolves
to nothing, as there is no layout of it.

tests/ checks every circuit has one unique ergastId and that a race
resolves by id; the merge check runs it.

Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
2026-11-27 09:40:27 +01:00

51 lines
1.5 KiB
Python

from dataclasses import dataclass
from typing import Optional, TYPE_CHECKING
from .track_layout import TrackLayout
if TYPE_CHECKING:
from .locality import Locality
@dataclass
class URLs:
wikipedia: str
fandom: str
@dataclass
class Circuit:
slug: str
name: str
urls: URLs
locality: 'Locality'
layouts: dict[str, TrackLayout]
# Jolpica's (Ergast's) circuitId: the stable key a race's circuit is matched on, never its name
ergast_id: Optional[str] = None
def to_dict(self) -> dict:
return {
"slug": self.slug,
"name": self.name,
"ergastId": self.ergast_id,
"urls": {"wikipedia": self.urls.wikipedia, "fandom": self.urls.fandom},
"layouts": {k: v.to_dict() for k, v in self.layouts.items()},
}
@classmethod
def from_dict(cls, locality: 'Locality', slug: str, data: dict) -> 'Circuit':
circuit = cls(
slug=slug,
name=data["name"],
ergast_id=data.get("ergastId"),
urls=URLs(**data["urls"]),
locality=locality,
layouts={}
)
circuit.layouts = {layout_slug: TrackLayout.from_dict(circuit, layout_slug, layout_data) for
layout_slug, layout_data in data["layouts"].items()}
return circuit
def load_geo_json_data(self):
"""Load data for all layouts"""
for track_layout in self.layouts.values():
track_layout.load_geo_json_data()