Skip to content

Leader Category

Leader Category Class

Represents a category of statistical leaders for a given season.

The LeaderCategory class is responsible for storing and managing information about a specific leader category, such as the category's name, abbreviation, and the athletes who are the statistical leaders in that category for the specified season. The data is loaded from a given JSON object, and the class provides methods to represent and interact with this data.

Attributes:

Name Type Description
leader_cat_json dict

The JSON data containing information about the leader category.

espn_instance object

The instance of the ESPN-related class for interacting with ESPN data.

athletes dict

A dictionary holding athletes (as instances of the Leader class) for each season.

season str or int

The season for which the leader category data is relevant.

abbreviation str

The abbreviation of the leader category.

name str

The name of the leader category.

display_name str

The display name of the leader category.

Methods:

Name Description
__repr__

Returns a string representation of the LeaderCategory instance.

_load_leaders_data

Loads the leader data from the provided JSON and initializes the class attributes.

Source code in pyespn/classes/stat.py
@validate_json("leader_cat_json")
class LeaderCategory:
    """
    Represents a category of statistical leaders for a given season.

    The LeaderCategory class is responsible for storing and managing information
    about a specific leader category, such as the category's name, abbreviation,
    and the athletes who are the statistical leaders in that category for the
    specified season. The data is loaded from a given JSON object, and the class
    provides methods to represent and interact with this data.

    Attributes:
        leader_cat_json (dict): The JSON data containing information about the leader category.
        espn_instance (object): The instance of the ESPN-related class for interacting with ESPN data.
        athletes (dict): A dictionary holding athletes (as instances of the Leader class) for each season.
        season (str or int): The season for which the leader category data is relevant.
        abbreviation (str): The abbreviation of the leader category.
        name (str): The name of the leader category.
        display_name (str): The display name of the leader category.

    Methods:
        __repr__(): Returns a string representation of the LeaderCategory instance.
        _load_leaders_data(): Loads the leader data from the provided JSON and initializes the class attributes.
    """

    def __init__(self, leader_cat_json, espn_instance, season):
        """
        Initializes a LeaderCategory instance with the given data.

        Args:
            leader_cat_json (dict): The JSON data for the leader category.
            espn_instance (object): An instance of the ESPN class for interacting with ESPN data.
            season (str or int): The season the leader category is related to.
        """
        self.leader_cat_json = leader_cat_json
        self._espn_instance = espn_instance
        self.athletes = {}
        self.season = season
        self._load_leaders_data()

    def __repr__(self) -> str:
        """
        Returns a string representation of the Leader Category instance.

        Returns:
            str: A formatted string with the leader info.
        """
        return f"<LeaderCategory | {self.season}-{self.display_name}>"

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

    def _load_leaders_data(self):
        """
        Loads the leaders' data from the provided JSON.

        This method extracts relevant information (such as abbreviation, name,
        display name, and athletes) from the `leader_cat_json` and populates
        the corresponding attributes. It also creates instances of the `Leader`
        class for each athlete and stores them in the `athletes` dictionary,
        indexed by season.
        """
        self.abbreviation = self.leader_cat_json.get('abbreviation')
        self.name = self.leader_cat_json.get('name')
        self.abbreviation = self.leader_cat_json.get('abbreviation')
        self.display_name = self.leader_cat_json.get('displayName')
        all_athletes = []
        rank = 1
        for ath in self.leader_cat_json.get('leaders', []):
            all_athletes.append(Leader(leader_json=ath,
                                       espn_instance=self._espn_instance,
                                       season=self.season,
                                       rank=rank))
            rank += 1
        self.athletes[self.season] = all_athletes

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

        Returns:
            dict: The leader category's raw JSON data.
        """
        return self.leader_cat_json

espn_instance property

PYESPN: the espn client instance associated with the class

__init__(leader_cat_json, espn_instance, season)

Initializes a LeaderCategory instance with the given data.

Parameters:

Name Type Description Default
leader_cat_json dict

The JSON data for the leader category.

required
espn_instance object

An instance of the ESPN class for interacting with ESPN data.

required
season str or int

The season the leader category is related to.

required
Source code in pyespn/classes/stat.py
def __init__(self, leader_cat_json, espn_instance, season):
    """
    Initializes a LeaderCategory instance with the given data.

    Args:
        leader_cat_json (dict): The JSON data for the leader category.
        espn_instance (object): An instance of the ESPN class for interacting with ESPN data.
        season (str or int): The season the leader category is related to.
    """
    self.leader_cat_json = leader_cat_json
    self._espn_instance = espn_instance
    self.athletes = {}
    self.season = season
    self._load_leaders_data()

__repr__()

Returns a string representation of the Leader Category instance.

Returns:

Name Type Description
str str

A formatted string with the leader info.

Source code in pyespn/classes/stat.py
def __repr__(self) -> str:
    """
    Returns a string representation of the Leader Category instance.

    Returns:
        str: A formatted string with the leader info.
    """
    return f"<LeaderCategory | {self.season}-{self.display_name}>"

to_dict()

Converts the LeaderCategory instance to its original JSON dictionary.

Returns:

Name Type Description
dict dict

The leader category's raw JSON data.

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

    Returns:
        dict: The leader category's raw JSON data.
    """
    return self.leader_cat_json