Skip to content

Core

Provide the shared types, constants and building primitives.

BuildError

Bases: ValueError

Raised when a builder cannot turn its arguments into the object asked for.

format_event_time(event_time)

Render an event time as the whole-minute token used in column names.

Parameters:

Name Type Description Default
event_time Timedelta

A time delta, e.g. pd.Timedelta('60min').

required

Returns:

Type Description
str

The token, e.g. 60min.

Source code in src/sportsbet/core/_utils.py
11
12
13
14
15
16
17
18
19
20
21
def format_event_time(event_time: pd.Timedelta) -> str:
    """Render an event time as the whole-minute token used in column names.

    Args:
        event_time: A time delta, e.g. `pd.Timedelta('60min')`.

    Returns:
        The token, e.g. `60min`.
    """
    total_minutes = int(event_time.total_seconds() / 60)
    return f'{total_minutes}min'

load_object(reference)

Return the object a reference names.

Parameters:

Name Type Description Default
reference str

A string with a path to a Python file and the name of an object inside it, separated by a colon.

required

Returns:

Type Description
object

The object the reference names.

Raises:

Type Description
BuildError

If the reference is malformed, the file is missing or unreadable, or it has no such object.

Source code in src/sportsbet/core/_utils.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def load_object(reference: str) -> object:
    """Return the object a reference names.

    Args:
        reference: A string with a path to a Python file and the name of an object inside it, separated by a colon.

    Returns:
        The object the reference names.

    Raises:
        BuildError: If the reference is malformed, the file is missing or unreadable, or it has no such object.
    """
    path, _, name = reference.rpartition(':')
    if not name:
        msg = f'`{reference}` should name an object inside a Python file, as in `models.py:bettor`.'
        raise BuildError(msg)
    if not Path(path).exists():
        msg = f'The file `{path}` does not exist.'
        raise BuildError(msg)
    spec = spec_from_file_location('sportsbet_model', path)
    if spec is None or spec.loader is None:
        msg = f'The file `{path}` could not be read as Python.'
        raise BuildError(msg)
    mod = module_from_spec(spec)
    spec.loader.exec_module(mod)
    if not hasattr(mod, name):
        msg = f'The file `{path}` has no `{name}` in it.'
        raise BuildError(msg)
    return getattr(mod, name)

parse_event_time(token)

Read the whole-minute token used in column names back into a time delta.

Parameters:

Name Type Description Default
token str

A whole-minute token, e.g. 60min.

required

Returns:

Type Description
Timedelta

The time delta the token names.

Source code in src/sportsbet/core/_utils.py
24
25
26
27
28
29
30
31
32
33
def parse_event_time(token: str) -> pd.Timedelta:
    """Read the whole-minute token used in column names back into a time delta.

    Args:
        token: A whole-minute token, e.g. `60min`.

    Returns:
        The time delta the token names.
    """
    return pd.Timedelta(minutes=int(token[: -len('min')]))