Skip to content

Stats

Stats Class

Represents a statistical record for a player within a given season.

Attributes:

Name Type Description
stat_json dict

The raw JSON data containing the statistical information.

espn_instance PYESPN

The ESPN API instance used for retrieving additional data.

category str

The category of the stat (e.g., 'batting', 'pitching').

season int

The season in which the stats were recorded.

player_id str

The unique ID of the player.

stat_value float | int

The value of the stat.

stat_type_abbreviation str

Abbreviation of the stat type.

description str

A description of the stat.

name str

The name of the stat (e.g., 'home runs', 'strikeouts').

type str

The type of stat (e.g., 'single', 'accumulated').

per_game_value float | None

The value per game, if available.

rank int | None

The player's rank in the stat category.

Methods:

Name Description
__repr__

Returns a string representation of the Stat instance, including the stat name, season, and value.

_set_stats_data

Extracts and sets the statistical attributes from the provided JSON data.

Source code in pyespn/classes/stat.py
@validate_json("stat_json")
class Stat:
    """
    Represents a statistical record for a player within a given season.

    Attributes:
        stat_json (dict): The raw JSON data containing the statistical information.
        espn_instance (PYESPN): The ESPN API instance used for retrieving additional data.
        category (str): The category of the stat (e.g., 'batting', 'pitching').
        season (int): The season in which the stats were recorded.
        player_id (str): The unique ID of the player.
        stat_value (float | int): The value of the stat.
        stat_type_abbreviation (str): Abbreviation of the stat type.
        description (str): A description of the stat.
        name (str): The name of the stat (e.g., 'home runs', 'strikeouts').
        type (str): The type of stat (e.g., 'single', 'accumulated').
        per_game_value (float | None): The value per game, if available.
        rank (int | None): The player's rank in the stat category.

    Methods:
        __repr__() -> str:
            Returns a string representation of the Stat instance, including the stat name, season, and value.

        _set_stats_data() -> None:
            Extracts and sets the statistical attributes from the provided JSON data.
    """

    def __init__(self, stat_json, espn_instance):
        """
        Initializes a Stat instance.

        Args:
            stat_json (dict): The JSON object containing the stat data.
            espn_instance (PYESPN): An instance of the ESPN API client.
        """
        self.stat_json = stat_json
        self._espn_instance = espn_instance
        self._set_stats_data()

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

        Returns:
            str: A formatted string with the stat name, value, and season.
        """
        return f"<Stat | {self.season}-{self.name}: {self.stat_value}>"

    def _set_stats_data(self):
        """
        Extracts and sets the statistical attributes from the provided JSON data.
        """
        self.category = self.stat_json.get('category')
        self.season = self.stat_json.get('season')
        self.player_id = self.stat_json.get('player_id')
        self.stat_value = self.stat_json.get('stat_value')
        if not self.stat_value:
            self.stat_value = self.stat_json.get('value')

        self.stat_type_abbreviation = self.stat_json.get('stat_type_abbreviation')
        if not self.stat_type_abbreviation:
            self.stat_type_abbreviation = self.stat_json.get('abbreviation')
        self.description = self.stat_json.get('description')
        self.name = self.stat_json.get('name')
        if not self.name:
            self.name = self.stat_json.get("displayName")
        self.type = self.stat_json.get('type')
        self.per_game_value = self.stat_json.get('perGameValue')
        self.rank = self.stat_json.get('rank')

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

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

        Returns:
            dict: The stats's raw JSON data.
        """
        return self.stat_json

espn_instance property

PYESPN: the espn client instance associated with the class

__init__(stat_json, espn_instance)

Initializes a Stat instance.

Parameters:

Name Type Description Default
stat_json dict

The JSON object containing the stat data.

required
espn_instance PYESPN

An instance of the ESPN API client.

required
Source code in pyespn/classes/stat.py
def __init__(self, stat_json, espn_instance):
    """
    Initializes a Stat instance.

    Args:
        stat_json (dict): The JSON object containing the stat data.
        espn_instance (PYESPN): An instance of the ESPN API client.
    """
    self.stat_json = stat_json
    self._espn_instance = espn_instance
    self._set_stats_data()

__repr__()

Returns a string representation of the Stat instance.

Returns:

Name Type Description
str str

A formatted string with the stat name, value, and season.

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

    Returns:
        str: A formatted string with the stat name, value, and season.
    """
    return f"<Stat | {self.season}-{self.name}: {self.stat_value}>"

to_dict()

Converts the Stat instance to its original JSON dictionary.

Returns:

Name Type Description
dict dict

The stats's raw JSON data.

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

    Returns:
        dict: The stats's raw JSON data.
    """
    return self.stat_json