Skip to content

base_collections

base_collections

BaseDict

Bases: RootModel[dict[str, T]]


              flowchart TD
              stochas.base_collections.BaseDict[BaseDict]

              

              click stochas.base_collections.BaseDict href "" "stochas.base_collections.BaseDict"
            

__getitem__

Python
__getitem__(key: str) -> T

Get an item in the dictionary with the specified key.

Source code in src/stochas/base_collections.py
Python
def __getitem__(self, key: str) -> T:
    """Get an item in the dictionary with the specified key."""
    if key not in self.root:
        msg = f"'{key}' not found."
        logger.error(msg)
        raise KeyError(msg)
    return self.root[key]

__setitem__

Python
__setitem__(key: str, value: T) -> None

Set the value of a single key-value pair.

Source code in src/stochas/base_collections.py
Python
def __setitem__(self, key: str, value: T) -> None:
    """Set the value of a single key-value pair."""
    name = getattr(value, "name", None)
    if key != name:
        msg = f"Key '{key}' must match name '{name}'"
        logger.error(msg)
        raise ValueError(msg)
    self.update(value)

__iter__

Python
__iter__() -> Iterator[str]

Create an iterable object.

Source code in src/stochas/base_collections.py
Python
def __iter__(self) -> Iterator[str]:  # pyright: ignore[reportIncompatibleMethodOverride]
    """Create an iterable object."""
    return self.root.__iter__()

__len__

Python
__len__() -> int

Returns the number of elements in the dictionary.

Source code in src/stochas/base_collections.py
Python
def __len__(self) -> int:
    """Returns the number of elements in the dictionary."""
    return self.root.__len__()

__contains__

Python
__contains__(key: object) -> bool

Specifies if a key already exists in the dictionary.

Source code in src/stochas/base_collections.py
Python
def __contains__(self, key: object) -> bool:
    """Specifies if a key already exists in the dictionary."""
    return self.root.__contains__(key)

keys

Python
keys()

Returns the keys in the dictionary.

Source code in src/stochas/base_collections.py
Python
def keys(self):
    """Returns the keys in the dictionary."""
    return self.root.keys()

values

Python
values()

Returns the values in the dictionary.

Source code in src/stochas/base_collections.py
Python
def values(self):
    """Returns the values in the dictionary."""
    return self.root.values()

items

Python
items()

Returns the key-value pairs in the dictionary as tuples.

Source code in src/stochas/base_collections.py
Python
def items(self):
    """Returns the key-value pairs in the dictionary as tuples."""
    return self.root.items()

update

Python
update(value: T) -> None

Add a new dictionary key value pair. The key cannot already exist in the dictionary.

Source code in src/stochas/base_collections.py
Python
def update(self, value: T) -> None:
    """Add a new dictionary key value pair. The key cannot already exist in the dictionary."""
    name = getattr(value, "name", None)
    if name in self.root:
        msg = f"{name} has already been registered."
        logger.error(msg)
        raise KeyError(msg)

    self.force_update(value=value, warn=False)

update_many

Python
update_many(values: Iterable[T]) -> None

Add many new dictionary key value pair. The keys cannot already exist in the dictionary.

Source code in src/stochas/base_collections.py
Python
def update_many(self, values: Iterable[T]) -> None:
    """Add many new dictionary key value pair. The keys cannot already exist in the dictionary."""
    for value in values:
        self.update(value=value)

force_update

Python
force_update(value: T, warn: bool = True) -> None

Forces a key-value pair into the dictionary. Overwrites existing key if it exists.

Source code in src/stochas/base_collections.py
Python
def force_update(self, value: T, warn: bool = True) -> None:
    """Forces a key-value pair into the dictionary. Overwrites existing key if it exists."""
    name = getattr(value, "name")
    if warn:
        logger.warning(f"Forcing {name} into {self.__class__.__name__}.")
    self.root[name] = value

force_update_many

Python
force_update_many(
    values: Iterable[T], warn: bool = True
) -> None

Forces adding many new dictionary key value pair. Overwrites existing keys if they exist.

Source code in src/stochas/base_collections.py
Python
def force_update_many(self, values: Iterable[T], warn: bool = True) -> None:
    """Forces adding many new dictionary key value pair. Overwrites existing keys if they exist."""
    for value in values:
        self.force_update(value=value, warn=warn)

BaseList

Bases: RootModel[list[T]]


              flowchart TD
              stochas.base_collections.BaseList[BaseList]

              

              click stochas.base_collections.BaseList href "" "stochas.base_collections.BaseList"
            

__len__

Python
__len__() -> int

Returns the number of elements in the list.

Source code in src/stochas/base_collections.py
Python
def __len__(self) -> int:
    """Returns the number of elements in the list."""
    return self.root.__len__()

__iter__

Python
__iter__() -> Iterator[T]

Create an iterable object.

Source code in src/stochas/base_collections.py
Python
def __iter__(self) -> Iterator[T]:  # pyright: ignore[reportIncompatibleMethodOverride]
    """Create an iterable object."""
    return self.root.__iter__()

__getitem__

Python
__getitem__(index: int) -> T
Python
__getitem__(index: slice) -> list[T]
Python
__getitem__(index: int | slice) -> T | list[T]

Gets the items in the specified index/indices.

Source code in src/stochas/base_collections.py
Python
def __getitem__(self, index: int | slice) -> T | list[T]:
    """Gets the items in the specified index/indices."""
    return self.root[index]

__setitem__

Python
__setitem__(index: int, value: T) -> None

Set the object at an index.

Source code in src/stochas/base_collections.py
Python
def __setitem__(self, index: int, value: T) -> None:
    """Set the object at an index."""
    self.root[index] = value

__delitem__

Python
__delitem__(index: int) -> None

Delete an element from the list.

Source code in src/stochas/base_collections.py
Python
def __delitem__(self, index: int) -> None:
    """Delete an element from the list."""
    del self.root[index]

append

Python
append(value: T) -> None

Append an item to the list.

Source code in src/stochas/base_collections.py
Python
def append(self, value: T) -> None:
    """Append an item to the list."""
    self.root.append(value)

extend

Python
extend(values: Iterable[T]) -> None

Append many items to the list.

Source code in src/stochas/base_collections.py
Python
def extend(self, values: Iterable[T]) -> None:
    """Append many items to the list."""
    self.root.extend(values)

pop

Python
pop(index: int = -1) -> T

Remove and return item at index (default last).

Source code in src/stochas/base_collections.py
Python
def pop(self, index: int = -1) -> T:
    """Remove and return item at index (default last)."""
    return self.root.pop(index)

find_by_name

Python
find_by_name(name: str) -> T

Utility to find a specific named value within the list.

Source code in src/stochas/base_collections.py
Python
def find_by_name(self, name: str) -> T:
    """Utility to find a specific named value within the list."""
    for item in self.root:
        item_name = getattr(item, "name", None)
        if item_name == name:
            return item
    msg = f"Item '{name}' not found in list."
    logger.error(msg)
    raise KeyError(msg)