Skip to content

Utils

utils

Additional utilities that fit nowhere else.

DateTimeOrRange: TypeAlias = dt.datetime | dt.date | pnd.Interval[pnd.DateTime] | pnd.Interval[pnd.Date] module-attribute

A type alias for various date, date time and interval representations.

KT = TypeVar('KT') module-attribute

PT = TypeVar('PT', bound=BaseModel) module-attribute

T = TypeVar('T') module-attribute

VT = TypeVar('VT') module-attribute

SList

A list that holds often only a single element.

item() -> T

dict_diff(dct1: Mapping[KT, VT], dct2: Mapping[KT, VT]) -> tuple[list[KT], list[KT], dict[KT, tuple[VT, VT]]]

Returns the added keys, removed keys and keys of changed values of both dictionaries.

dict_diff_str(dct1: Mapping[KT, VT], dct2: Mapping[KT, VT]) -> tuple[list[str], list[str], list[str]]

Returns the added keys, removed keys and keys of changed values of both dictionaries.

display_html(html: str, *, raw: bool = False) -> None

Render HTML in the active Jupyter frontend.

Wraps IPython's untyped display_html so its missing annotations stay contained at this single boundary instead of leaking to every call site.

display_markdown(markdown: str, *, raw: bool = False) -> None

Render Markdown in the active Jupyter frontend.

Wraps IPython's untyped display_markdown so its missing annotations stay contained at this single boundary instead of leaking to every call site.

find_index(elem: Any, lst: list[Any]) -> int | None

Find the index of the element in the list or return None.

find_indices(elements: NDArray[np.int_] | Sequence[Any], total_set: NDArray[np.int_] | Sequence[Any]) -> NDArray[np.int_]

Finds the indices of the elements in the total set.

is_notebook() -> bool

Determine if we are running within a Jupyter notebook.

is_stable_release() -> bool

Return whether the current version is a stable release.

is_stable_version(version_str: str) -> bool

Return whether the given version is a stable release.

parse_dt_str(dt_str: str) -> DateTimeOrRange

Parse typical Notion date/datetime/interval strings to pendulum objects.

If no timezone is provided assume local timezone and convert everything else to UTC for consistency.

pydantic_apply(obj: PT, func: Callable[[Any, Any], Any]) -> PT

Apply a function to all fields, i.e. (name, value), of a Pydantic model recursively.

The transformed model is returned as a copy, leaving the original model unchanged.

pydantic_to_toml(model: BaseModel) -> str

Convert a Pydantic model to a TOML string.

rank(arr: NDArray[np.int_]) -> NDArray[np.int_]

Returns the rank of the elements in the array and gives the same rank to equal elements.

rec_apply(func: Callable[[Any], Any], obj: Any) -> Any

Recursively applies a function func to all elements in a nested structure.

  • Applies func to every non-container element.
  • Recurses into lists, tuples and dicts.
  • Strings are treated as atomic elements and are not considered containers.

Example

rows = [[1, 2], [3, [4, 5]]] result = recursive_apply(rows, lambda x: x * 2) print(result) # [[2, 4], [6, [8, 10]]]

safe_list_get(lst: Sequence[T], idx: int, *, default: T | None = None) -> T | None

Get the element at the index of the list or return the default value.

set_attr_none(obj: PT, attr_paths: str | Sequence[str] | None, *, inplace: bool = False, missing_ok: bool = False) -> PT

Set the attributes given by a potentially nested path to None.

None attributes will be removed during serialization by default.

str_hash(*args: str, n_chars: int = 16) -> str

Hashes string arguments to a n-character string.

temp_attr(obj: object, **kwargs: Any) -> Generator[None, None, None]

Temporarily sets multiple attributes of an object to specified values, and restores their original values after the context exits.

Parameters:

Name Type Description Default
obj object

The object whose attributes will be modified.

required
**kwargs Any

The attributes and their temporary values to modify.

{}

temp_timezone(tz: str | pnd.Timezone) -> Iterator[None]

Temporarily set the local timezone to the given timezone. Mostly used by unit tests.

to_pendulum(dt_spec: str | DateTimeOrRange) -> DateTimeOrRange

Convert a datetime or date object to a pendulum object.