nanos.data module

nanos.data.EMPTY_VALUES: Final[list[Any]] = ['', None, [], {}]

List of values that are considered empty

nanos.data.chunker(seq: Sequence[T], size: int) list[collections.abc.Sequence[T]]

Splits provided iterable into list of chunks of given size

Parameters:
  • seq – iterable to split

  • size – chunk size

Returns:

list of chunks

nanos.data.idfy(obj: dict[Any, Any], id_field_name: str = DEFAULT_ID_ATTR_NAME) dict[Any, dict[Any, Any]]
nanos.data.idfy(obj: list[T], id_field_name: str = DEFAULT_ID_ATTR_NAME) dict[Any, T]
nanos.data.idfy(obj: set[T], id_field_name: str = DEFAULT_ID_ATTR_NAME) dict[Any, T]
nanos.data.idfy(obj: tuple[T, ...], id_field_name: str = DEFAULT_ID_ATTR_NAME) dict[Any, T]
nanos.data.idfy(obj: T, id_field_name: str = DEFAULT_ID_ATTR_NAME) dict[Any, T]

Converts given object into dict with id_field_name values as a key and actual object as a value.

If given object is a dict this function uses to get value of id_field_name key. If any other object - looks for id_field_name attribute.

Raises ValueError if there’s no appropriate id value found.

Applied recursively if Iterable is given as an input.

Parameters:
  • obj (T) – object to convert to dictionary

  • id_field_name (str) – name of the field/attribute to use to get, defaults to “id”

Returns:

dict with id_field_name values as a key and actual object as a value

nanos.data.remove_empty_members(obj: dict[Any, Any], empty: list[Any] | None = None) dict[Any, Any] | None
nanos.data.remove_empty_members(obj: list[Any], empty: list[Any] | None = None) list[Any] | None
nanos.data.remove_empty_members(obj: T, empty: list[Any] | None = None) T | None

Removes empty members from given object.

Recursively goes through the given object and removes its members that are considered empty. If given object is a dict, removes keys that have empty values. If given object is a list removes empty items from it.

Parameters:
  • obj – object to remove empty members from

  • empty – list of values that are considered empty. If not given, defaults to EMPTY_VALUES

Returns:

object with empty members removed, or None if the object itself is empty