Skip to content

Index

kloppy.domain.TimeContainer

TimeContainer()

Bases: Generic[T]

Source code in kloppy/domain/models/time.py
def __init__(self):
    self.items: SortedDict = SortedDict()

items instance-attribute

items = SortedDict()

set

set(time, item)
Source code in kloppy/domain/models/time.py
def set(self, time: Time, item: Optional[T]):
    self.items[time] = item  # Pair(key=time, item=item)

value_at

value_at(time)
Source code in kloppy/domain/models/time.py
def value_at(self, time: Time) -> Optional[T]:
    idx = self.items.bisect_right(time) - 1
    if idx < 0:
        raise KeyError("Not found")
    return self.items.values()[idx]

ranges

ranges()
Source code in kloppy/domain/models/time.py
def ranges(self) -> list[tuple[Time, Time, T]]:
    items = list(self.items)
    if not items:
        return []

    # When last item isn't None (meaning 'end'), make sure to add it
    if self.items[items[-1]] is not None:
        current_period = items[0].period
        while current_period.next_period:
            current_period = current_period.next_period
        items.append(current_period.end_time)

    if len(items) < 2:
        raise ValueError("Cannot create ranges when length < 2")

    ranges_ = []
    for start_time, end_time in zip(items[:-1], items[1:]):
        ranges_.append((start_time, end_time, self.items[start_time]))
    return ranges_

last

last(include_time=False, default=SENTINEL)
Source code in kloppy/domain/models/time.py
def last(self, include_time: bool = False, default=SENTINEL):
    if not len(self.items):
        if default == SENTINEL:
            raise KeyError
        else:
            return default

    time = self.items.keys()[-1]
    if include_time:
        return time, self.items[time]
    else:
        return self.items[time]

at_start

at_start()

Return the value at the beginning of the match

Source code in kloppy/domain/models/time.py
def at_start(self):
    """Return the value at the beginning of the match"""
    if not self.items:
        raise KeyError

    first_item: Time = self.items.keys()[0]

    tmp_period = first_item.period
    while tmp_period.prev_period:
        tmp_period = tmp_period.prev_period

    return self.value_at(Time.from_period(tmp_period, "start"))

reset

reset()

Clears all items from the container.

Source code in kloppy/domain/models/time.py
def reset(self):
    """Clears all items from the container."""
    self.items.clear()