Generic utilities

Public functions

is_valid_against_reference

is_valid_against_reference(
    candidate: object | None,
    reference: object,
    validator: Callable[..., bool],
) -> bool

Check if 'reference' is falsy or whether it passes the 'validator' check against 'reference'.

parse_timestamp_str_if_set

parse_timestamp_str_if_set(
    timestamp_str: str,
) -> datetime | None

Parse an ISO format timestamp string to a datetime object if it is set.

Parameters:

  • timestamp_str
    (str) –

    The timestamp string in ISO format

Returns:

  • datetime | None –

    The parsed datetime object if the string is set, None otherwise

are_keys_in_collection

are_keys_in_collection(
    dict_: dict, collection: Collection
) -> bool

Check if all keys in a dict are in a collection.

Parameters:

  • dict_
    (dict) –

    The dict to check

  • collection
    (Collection) –

    The collection to check against

Returns:

  • bool –

    True if all keys in the dict are in the collection, False otherwise

are_keys_in_dataclass_fields

are_keys_in_dataclass_fields(
    dict_: dict, dataclass_: type
) -> bool

Check if all keys in a dict are fields in a dataclass.

Parameters:

  • dict_
    (dict) –

    The dict to check

  • dataclass_
    (type) –

    The dataclass to check against

Returns:

  • bool –

    True if all keys in the dict are fields in the dataclass, False otherwise

Public classes

TimeRange dataclass

TimeRange(
    start: datetime | None = None,
    end: datetime | None = None,
)

Dataclass representing a time range with optional start or end.

Methods:

__post_init__
__post_init__() -> None

Validate that at least one of start or end is set.

Raises:

from_str classmethod
from_str(start_str: str, end_str: str) -> TimeRange

Create a TimeRange instance from ISO format timestamp strings.

Parameters:

  • start_str
    (str) –

    The start timestamp string in ISO format

  • end_str
    (str) –

    The end timestamp string in ISO format

Returns:

  • TimeRange –

    The created TimeRange instance

is_timestamp_in_range
is_timestamp_in_range(timestamp: datetime) -> bool

Check if a datetime timestamp is within the time range.

Parameters:

  • timestamp
    (datetime) –

    The datetime timestamp to check

Returns:

  • bool –

    True if the timestamp is within the range, False otherwise

is_timestamp_str_in_range
is_timestamp_str_in_range(timestamp_str: str) -> bool

Check if a timestamp string is within the time range.

Parameters:

  • timestamp_str
    (str) –

    The timestamp string in ISO format to check

Returns:

  • bool –

    True if the timestamp string is within the time range, False otherwise