rwskit.collections

Utilities for working with collections.

Attributes

log

Functions

is_iterable(→ bool)

Tests if the object is iterable.

is_generator(→ bool)

Checks if the object is a generator.

get_first_non_null_value(→ Optional[Any])

Recursively try to get the first non-null value in a collection.

recursive_sort(→ Any)

Attempts to sort an object recursively according to the following rules:

remove_none_from_dict(→ dict[str, Any])

Recursively remove None values from a (nested) dictionary.

Module Contents

rwskit.collections_.log[source]
rwskit.collections_.is_iterable(obj: Any, consider_string_iterable: bool = False) bool[source]

Tests if the object is iterable.

Parameters:
  • obj (any) – An object.

  • consider_string_iterable (bool, default = False) – Whether to consider strings iterable or not.

Returns:

True if obj is iterable.

Return type:

bool

rwskit.collections_.is_generator(obj: Any) bool[source]

Checks if the object is a generator.

Parameters:

obj (any) – An object.

Returns:

True if the object is a generator.

Return type:

bool

rwskit.collections_.get_first_non_null_value(collection: Iterable) Any | None[source]

Recursively try to get the first non-null value in a collection.

This method will recursively traverse the collection until it finds a non-iterable value that is not None.

Parameters:

collection (Iterable) – The collection to retrieve the value from.

Returns:

The first non-null value in the series, if one exists, otherwise return None.

Return type:

Any, optional

rwskit.collections_.recursive_sort(obj: Any) Any[source]

Attempts to sort an object recursively according to the following rules:

  • If the object is a dictionary, it will be sorted by its keys and its values will be sorted recursively.

  • If the object is a list or tuple, it will be sorted by its values. Typically, a value is a primitive, but lists, tuples, and dictionaries are also valid. In these cases, the collections are compared after sorting.

  • Any other value is returned unchanged.

Parameters:

obj (Any) – The input object

Returns:

The returned object sorted.

Return type:

Any

rwskit.collections_.remove_none_from_dict(d: dict[str, Any]) dict[str, Any][source]

Recursively remove None values from a (nested) dictionary.