Skip to content

Circuit

Circuit Class

A class to represent a Circuit.

This class takes a JSON response from an API and processes the data to create a Circuit object with relevant attributes. It provides details such as the circuit's name, address, type, laps, and fastest lap times, as well as any related diagrams and references to other objects like the fastest lap driver and track.

Attributes:

Name Type Description
api_ref str

The API reference URL for the circuit.

id str

The unique identifier of the circuit.

full_name str

The full name of the circuit.

city str

The city where the circuit is located.

country str

The country where the circuit is located.

type str

The type of the circuit (e.g., street, permanent).

length str

The length of the circuit.

distance str

The total distance of the race at the circuit.

laps int

The number of laps in the race at the circuit.

turns int

The number of turns in the circuit.

direction str

The racing direction (e.g., clockwise, counterclockwise).

established int

The year when the circuit was established.

fastest_lap_driver_ref Player

A reference to the fastest lap driver.

fastest_lap_time str

The time of the fastest lap at the circuit.

fastest_lap_year int

The year in which the fastest lap was recorded.

track_ref str

A reference to the track.

diagrams list

A list of diagrams related to the circuit.

diagram_urls list

A list of URLs pointing to the circuit diagrams.

Source code in pyespn/classes/venue.py
@validate_json("circuit_json")
class Circuit:
    """
    A class to represent a Circuit.

    This class takes a JSON response from an API and processes the data to create
    a Circuit object with relevant attributes. It provides details such as the
    circuit's name, address, type, laps, and fastest lap times, as well as any
    related diagrams and references to other objects like the fastest lap driver
    and track.

    Attributes:
        api_ref (str): The API reference URL for the circuit.
        id (str): The unique identifier of the circuit.
        full_name (str): The full name of the circuit.
        city (str): The city where the circuit is located.
        country (str): The country where the circuit is located.
        type (str): The type of the circuit (e.g., street, permanent).
        length (str): The length of the circuit.
        distance (str): The total distance of the race at the circuit.
        laps (int): The number of laps in the race at the circuit.
        turns (int): The number of turns in the circuit.
        direction (str): The racing direction (e.g., clockwise, counterclockwise).
        established (int): The year when the circuit was established.
        fastest_lap_driver_ref (Player): A reference to the fastest lap driver.
        fastest_lap_time (str): The time of the fastest lap at the circuit.
        fastest_lap_year (int): The year in which the fastest lap was recorded.
        track_ref (str): A reference to the track.
        diagrams (list): A list of diagrams related to the circuit.
        diagram_urls (list): A list of URLs pointing to the circuit diagrams.
    """

    def __init__(self, circuit_json, espn_instance):
        """
        Initializes a Circuit instance with data from a JSON response and an ESPN instance.

        Args:
            circuit_json (dict): The JSON data containing information about the circuit.
            espn_instance (object): The ESPN instance used for making API calls.

        This method also calls _load_circuit_data to load the data into attributes.
        """
        self.circuit_json = circuit_json
        self._espn_instance = espn_instance
        self._load_circuit_data()

    @property
    def espn_instance(self):
        """
            PYESPN: the espn client instance associated with the class
        """
        return self._espn_instance

    def _load_circuit_data(self):
        """
        Sets each attribute from the circuit_json to its own attribute.
        """
        self.api_ref = self.circuit_json.get('$ref')
        self.id = self.circuit_json.get('id')
        self.full_name = self.circuit_json.get('fullName')

        # Extracting nested 'address' data
        address = self.circuit_json.get('address', {})
        self.city = address.get('city')
        self.country = address.get('country')

        self.type = self.circuit_json.get('type')
        self.length = self.circuit_json.get('length')
        self.distance = self.circuit_json.get('distance')
        self.laps = self.circuit_json.get('laps')
        self.turns = self.circuit_json.get('turns')
        self.direction = self.circuit_json.get('direction')
        self.established = self.circuit_json.get('established')

        # Fastest lap driver and fastest lap time
        self.fastest_lap_driver_ref = Player(player_json=self.circuit_json.get('fastestLapDriver', {}).get('$ref'),
                                             espn_instance=self.espn_instance)
        self.fastest_lap_time = self.circuit_json.get('fastestLapTime')
        self.fastest_lap_year = self.circuit_json.get('fastestLapYear')

        # Track reference
        self.track_ref = self.circuit_json.get('track', {}).get('$ref')

        # Extracting diagrams list and storing it
        self.diagrams = self.circuit_json.get('diagrams', [])
        # You can store each diagram in a separate variable if needed, for example:
        self.diagram_urls = [diagram.get('href') for diagram in self.diagrams]

    def __repr__(self) -> str:
        """
        Returns a string representation of the Circuit instance.
        """
        return f"<Circuit | {self.full_name}, {self.city}, {self.country}>"

    def to_dict(self) -> dict:
        """
        Converts the Circuit instance to its original JSON dictionary.

        Returns:
            dict: The circuit's raw JSON data.
        """
        return self.circuit_json

espn_instance property

PYESPN: the espn client instance associated with the class

__init__(circuit_json, espn_instance)

Initializes a Circuit instance with data from a JSON response and an ESPN instance.

Parameters:

Name Type Description Default
circuit_json dict

The JSON data containing information about the circuit.

required
espn_instance object

The ESPN instance used for making API calls.

required

This method also calls _load_circuit_data to load the data into attributes.

Source code in pyespn/classes/venue.py
def __init__(self, circuit_json, espn_instance):
    """
    Initializes a Circuit instance with data from a JSON response and an ESPN instance.

    Args:
        circuit_json (dict): The JSON data containing information about the circuit.
        espn_instance (object): The ESPN instance used for making API calls.

    This method also calls _load_circuit_data to load the data into attributes.
    """
    self.circuit_json = circuit_json
    self._espn_instance = espn_instance
    self._load_circuit_data()

__repr__()

Returns a string representation of the Circuit instance.

Source code in pyespn/classes/venue.py
def __repr__(self) -> str:
    """
    Returns a string representation of the Circuit instance.
    """
    return f"<Circuit | {self.full_name}, {self.city}, {self.country}>"

to_dict()

Converts the Circuit instance to its original JSON dictionary.

Returns:

Name Type Description
dict dict

The circuit's raw JSON data.

Source code in pyespn/classes/venue.py
def to_dict(self) -> dict:
    """
    Converts the Circuit instance to its original JSON dictionary.

    Returns:
        dict: The circuit's raw JSON data.
    """
    return self.circuit_json