Skip to content

Line

Line Class Documentation

Represents a betting line within the ESPN API framework.

This class stores details about a specific betting line, including the associated team or athlete.

Attributes:

Name Type Description
espn_instance PYESPN

The ESPN API instance for fetching additional data.

provider_instance Provider

The provider offering this betting line.

book_json dict

The raw JSON data representing the betting line.

athlete Player or None

The athlete associated with the betting line, if applicable.

team Team or None

The team associated with the betting line, if applicable.

ref str

The API reference URL for the athlete or team.

value float or None

The betting odds or value.

Methods:

Name Description
_set_line_data

Parses and stores betting line details.

__repr__

Returns a string representation of the Betting Line instance.

Source code in pyespn/classes/betting.py
@validate_json("book_json")
class Line:
    """
    Represents a betting line within the ESPN API framework.

    This class stores details about a specific betting line, including the associated team
    or athlete.

    Attributes:
        espn_instance (PYESPN): The ESPN API instance for fetching additional data.
        provider_instance (Provider): The provider offering this betting line.
        book_json (dict): The raw JSON data representing the betting line.
        athlete (Player or None): The athlete associated with the betting line, if applicable.
        team (Team or None): The team associated with the betting line, if applicable.
        ref (str): The API reference URL for the athlete or team.
        value (float or None): The betting odds or value.

    Methods:
        _set_line_data():
            Parses and stores betting line details.

        __repr__() -> str:
            Returns a string representation of the Betting Line instance.
    """

    def __init__(self, espn_instance, provider_instance: Provider, book_json: dict):
        """
        Initializes a Line instance.

        Args:
            espn_instance (PYESPN): The ESPN API instance for API interaction.
            provider_instance (Provider): The betting provider for this line.
            book_json (dict): The JSON data containing betting line details.
        """
        self._espn_instance = espn_instance
        self.provider_instance = provider_instance
        self.book_json = book_json
        self.athlete = None
        self.team = None
        self.ref = None
        self._set_line_data()

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

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

        Returns:
            str: A formatted string with the bettings line information .
        """

        msg = ''

        if self.team:
            msg += f'{self.team.name} | {self.value}'

        if self.athlete:
            msg += f'{self.athlete.full_name} | {self.value}'

        return f"<Betting Line: {msg}>"

    def _set_line_data(self):
        """
        Private method to parse and store betting line details, including associated teams or athletes.
        """
        from pyespn.classes.player import Player
        from pyespn.classes.team import Team
        try:
            if 'athlete' in self.book_json:
                athlete_id = get_athlete_id(self.book_json.get('athlete', {}).get('$ref'))
                self.athlete = self._espn_instance.check_teams_for_player_by_season(season=self.provider_instance.betting_instance.season,
                                                                                   player_id=athlete_id)
                if not self.athlete:

                    self.ref = self.book_json.get('athlete').get('$ref')
                    content = fetch_espn_data(self.ref)

                    self.athlete = Player(espn_instance=self._espn_instance,
                                          player_json=content)

            if 'team' in self.book_json:
                self.ref = self.book_json.get('team').get('$ref')
                content = fetch_espn_data(self.ref)

                self.team = Team(espn_instance=self._espn_instance,
                                 team_json=content)

            self.value = self.book_json.get('value')
        except API400Error as e:
            print(f'api error {e}')
        except JSONNotProvidedError as e:
            print(f'json error {e}')

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

        Returns:
            dict: The lines's raw JSON data.
        """
        return self.book_json

espn_instance property

PYESPN: the espn client instance associated with the class

__init__(espn_instance, provider_instance, book_json)

Initializes a Line instance.

Parameters:

Name Type Description Default
espn_instance PYESPN

The ESPN API instance for API interaction.

required
provider_instance Provider

The betting provider for this line.

required
book_json dict

The JSON data containing betting line details.

required
Source code in pyespn/classes/betting.py
def __init__(self, espn_instance, provider_instance: Provider, book_json: dict):
    """
    Initializes a Line instance.

    Args:
        espn_instance (PYESPN): The ESPN API instance for API interaction.
        provider_instance (Provider): The betting provider for this line.
        book_json (dict): The JSON data containing betting line details.
    """
    self._espn_instance = espn_instance
    self.provider_instance = provider_instance
    self.book_json = book_json
    self.athlete = None
    self.team = None
    self.ref = None
    self._set_line_data()

__repr__()

Returns a string representation of the Betting Line instance.

Returns:

Name Type Description
str str

A formatted string with the bettings line information .

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

    Returns:
        str: A formatted string with the bettings line information .
    """

    msg = ''

    if self.team:
        msg += f'{self.team.name} | {self.value}'

    if self.athlete:
        msg += f'{self.athlete.full_name} | {self.value}'

    return f"<Betting Line: {msg}>"

to_dict()

Converts the Line instance to its original JSON dictionary.

Returns:

Name Type Description
dict dict

The lines's raw JSON data.

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

    Returns:
        dict: The lines's raw JSON data.
    """
    return self.book_json