Skip to content

Leader

Leader Class

Represents a statistical leader in a specific category for a given season.

The Leader class encapsulates information about an athlete (Player) and their team in the context of a statistical category. It fetches relevant data from the provided JSON, stores the athlete and team information, and tracks the leader's rank and statistical value.

Attributes:

Name Type Description
leader_json dict

The JSON data for the leader.

espn_instance object

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

rank int

The rank of the athlete in the leader category.

athlete Player or None

The Player instance representing the athlete who is the leader.

team Team or None

The Team instance representing the team of the leader.

value float

The statistical value of the leader in the category.

rel dict or None

The relationship data in the leader JSON, which may contain references to athlete and team data.

Methods:

Name Description
__repr__

Returns a string representation of the Leader instance.

_load_leader_data

Loads the leader data from the provided JSON, initializing athlete, team, and value.

Source code in pyespn/classes/stat.py
@validate_json("leader_json")
class Leader:
    """
    Represents a statistical leader in a specific category for a given season.

    The Leader class encapsulates information about an athlete (Player) and their
    team in the context of a statistical category. It fetches relevant data from
    the provided JSON, stores the athlete and team information, and tracks the
    leader's rank and statistical value.

    Attributes:
        leader_json (dict): The JSON data for the leader.
        espn_instance (object): The instance of the ESPN-related class for interacting with ESPN data.
        rank (int): The rank of the athlete in the leader category.
        athlete (Player or None): The Player instance representing the athlete who is the leader.
        team (Team or None): The Team instance representing the team of the leader.
        value (float): The statistical value of the leader in the category.
        rel (dict or None): The relationship data in the leader JSON, which may contain references to athlete and team data.

    Methods:
        __repr__(): Returns a string representation of the Leader instance.
        _load_leader_data(): Loads the leader data from the provided JSON, initializing athlete, team, and value.
    """

    def __init__(self, leader_json, espn_instance, season, rank):
        """
        Initializes a Leader instance with the given leader data.

        Args:
            leader_json (dict): The JSON data representing the leader's information.
            espn_instance (object): An instance of the ESPN class for interacting with ESPN data.
            rank (int): The rank of the athlete in the leader category.
        """
        self.leader_json = leader_json
        self._espn_instance = espn_instance
        self.rank = rank
        self.season = season
        self.athlete = None
        self.team = None
        self._load_leader_data()

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

        Returns:
            str: A formatted string with the Leader Info.
        """

        return f"<Leader - {self.rank} | {self.athlete.full_name}-{self.value}: {self.team.name}>"

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

    def _load_leader_data(self):
        """
        Loads the leader's data from the provided JSON.

        This method extracts the statistical value, the athlete reference, and
        the team reference from the leader's JSON data. It fetches the athlete
        information by calling `fetch_espn_data` if an athlete reference exists,
        and it fetches the team information by calling `get_team_id` and
        `get_team_by_id` if a team reference exists.

        This method initializes the athlete and team attributes, as well as the
        statistical value and rank.

        """
        from pyespn.classes.player import Player
        self.value = self.leader_json.get('value', 0)
        self.rel = self.leader_json.get('rel')

        if 'team' in self.leader_json:
            team_id = get_team_id(self.leader_json.get('team', {}).get('$ref'))
            self.team = self._espn_instance.get_team_by_id(team_id=team_id)

        if 'athlete' in self.rel:
            try:
                athlete_id = get_athlete_id(self.leader_json.get('athlete', {}).get('$ref'))
                self.athlete = self.team.get_player_by_season_id(season=self.season, player_id=athlete_id)
            except Exception as e:
                print(e)
            finally:
                if not self.athlete:
                    athlete_content = fetch_espn_data(self.leader_json.get('athlete', {}).get('$ref'))
                    self.athlete = Player(player_json=athlete_content,
                                          espn_instance=self._espn_instance)

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

        Returns:
            dict: The leaders's raw JSON data.
        """
        return self.leader_json

espn_instance property

PYESPN: the espn client instance associated with the class

__init__(leader_json, espn_instance, season, rank)

Initializes a Leader instance with the given leader data.

Parameters:

Name Type Description Default
leader_json dict

The JSON data representing the leader's information.

required
espn_instance object

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

required
rank int

The rank of the athlete in the leader category.

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

    Args:
        leader_json (dict): The JSON data representing the leader's information.
        espn_instance (object): An instance of the ESPN class for interacting with ESPN data.
        rank (int): The rank of the athlete in the leader category.
    """
    self.leader_json = leader_json
    self._espn_instance = espn_instance
    self.rank = rank
    self.season = season
    self.athlete = None
    self.team = None
    self._load_leader_data()

__repr__()

Returns a string representation of the Leader 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 instance.

    Returns:
        str: A formatted string with the Leader Info.
    """

    return f"<Leader - {self.rank} | {self.athlete.full_name}-{self.value}: {self.team.name}>"

to_dict()

Converts the Leader instance to its original JSON dictionary.

Returns:

Name Type Description
dict dict

The leaders's raw JSON data.

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

    Returns:
        dict: The leaders's raw JSON data.
    """
    return self.leader_json