Skip to content

Odds

Odds Class Documentation

Represents betting odds for a specific team within a sporting event.

This class parses and stores betting-related data such as money lines, spreads, and associated odds types (open, current, and optionally close).

Attributes:

Name Type Description
odds_json dict

Raw JSON data for the odds entry.

espn_instance PYESPN

The main ESPN API instance used for lookups.

event_instance Event

The event this odds entry is associated with.

gameodds_instance GameOdds

The higher-level odds grouping instance.

favorite str

The name of the favorite team, if available.

underdog str

The name of the underdog team, if available.

money_line int or None

The money line value for this odds.

spread_odds int or None

The spread odds value.

team Team

The team associated with these odds.

open OddsType

The opening odds.

current OddsType

The most recent odds.

close OddsType or None

The closing odds, if provided (e.g., for ESPN BET).

Source code in pyespn/classes/betting.py
class Odds:
    """
    Represents betting odds for a specific team within a sporting event.

    This class parses and stores betting-related data such as money lines,
    spreads, and associated odds types (open, current, and optionally close).

    Attributes:
        odds_json (dict): Raw JSON data for the odds entry.
        espn_instance (PYESPN): The main ESPN API instance used for lookups.
        event_instance (Event): The event this odds entry is associated with.
        gameodds_instance (GameOdds): The higher-level odds grouping instance.
        favorite (str): The name of the favorite team, if available.
        underdog (str): The name of the underdog team, if available.
        money_line (int or None): The money line value for this odds.
        spread_odds (int or None): The spread odds value.
        team (Team): The team associated with these odds.
        open (OddsType): The opening odds.
        current (OddsType): The most recent odds.
        close (OddsType or None): The closing odds, if provided (e.g., for ESPN BET).
    """

    def __init__(self, odds_json, espn_instance, event_instance, gameodds_instance):
        """
        Initializes an Odds instance from provided JSON data.

        Args:
            odds_json (dict): The raw JSON containing odds data for a team.
            espn_instance (PYESPN): The parent API instance.
            event_instance (Event): The sporting event this odds data belongs to.
            gameodds_instance (GameOdds): The containing game odds context.
        """
        self.odds_json = odds_json
        self._espn_instance = espn_instance
        self.event_instance = event_instance
        self.gameodds_instance = gameodds_instance
        self._load_odds_json()

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

        Returns:
            str: A string identifying the associated team.
        """
        return f"<Odds | {self.team.name}>"

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

    def _load_odds_json(self):
        """
        Parses the JSON data and assigns odds-related attributes.

        Extracts key betting values such as favorite, underdog, money line,
        and odds type objects (open/current/close). Also resolves and assigns
        the associated team using the ESPN API.
        """

        self.favorite = self.odds_json.get('favorite')
        self.underdog = self.odds_json.get('underdog')
        self.money_line = self.odds_json.get('moneyLine')
        self.spread_odds = self.odds_json.get('spreadOdds')
        team_id = get_team_id(self.odds_json.get('team', {}).get('$ref'))
        self.team = self._espn_instance.get_team_by_id(team_id=team_id)
        self.open = OddsType(odds_name='open',
                             odds_type_json=self.odds_json.get('open'),
                             espn_instance=self._espn_instance)
        self.current = OddsType(odds_name='current',
                                odds_type_json=self.odds_json.get('current'),
                                espn_instance=self._espn_instance)
        if self.gameodds_instance.provider == 'ESPN BET':
            self.close = OddsType(odds_name='close',
                                  odds_type_json=self.odds_json.get('close'),
                                  espn_instance=self._espn_instance)

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

        Returns:
            dict: The odds's raw JSON data.
        """
        return self.odds_json

espn_instance property

PYESPN: the espn client instance associated with the class

__init__(odds_json, espn_instance, event_instance, gameodds_instance)

Initializes an Odds instance from provided JSON data.

Parameters:

Name Type Description Default
odds_json dict

The raw JSON containing odds data for a team.

required
espn_instance PYESPN

The parent API instance.

required
event_instance Event

The sporting event this odds data belongs to.

required
gameodds_instance GameOdds

The containing game odds context.

required
Source code in pyespn/classes/betting.py
def __init__(self, odds_json, espn_instance, event_instance, gameodds_instance):
    """
    Initializes an Odds instance from provided JSON data.

    Args:
        odds_json (dict): The raw JSON containing odds data for a team.
        espn_instance (PYESPN): The parent API instance.
        event_instance (Event): The sporting event this odds data belongs to.
        gameodds_instance (GameOdds): The containing game odds context.
    """
    self.odds_json = odds_json
    self._espn_instance = espn_instance
    self.event_instance = event_instance
    self.gameodds_instance = gameodds_instance
    self._load_odds_json()

__repr__()

Returns a string representation of the Odds instance.

Returns:

Name Type Description
str str

A string identifying the associated team.

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

    Returns:
        str: A string identifying the associated team.
    """
    return f"<Odds | {self.team.name}>"

to_dict()

Converts the Odds instance to its original JSON dictionary.

Returns:

Name Type Description
dict dict

The odds's raw JSON data.

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

    Returns:
        dict: The odds's raw JSON data.
    """
    return self.odds_json