utils.grouping#

Utility Functions for grouping iterables.

This module provides a collection of utility functions designed to assist with common tasks related to manipulating and transforming iterables in Python.

These utilities are generic and work with any iterable types. They’re particularly useful for data processing tasks, batching operations, and other scenarios where dividing data into specific groupings is necessary.

Note: While these utilities are designed for flexibility and ease-of-use, they may not be optimized for extremely large datasets or performance-critical applications.

Module Contents#

Functions#

pairwise

Return pairs of consecutive items from the input iterable.

split_by_chunk_size

Split an iterable into chunks of the specified size.

split_into_n_chunks

Split an iterable into a specified number of chunks.

Data#

T

API#

utils.grouping.T#

‘TypeVar(…)’

utils.grouping.pairwise(
iterable: collections.abc.Iterable[utils.grouping.T],
) collections.abc.Iterable[tuple[utils.grouping.T, utils.grouping.T]]#

Return pairs of consecutive items from the input iterable.

Args: iterable (Iterable[T]): The input iterable.

Returns: Iterable[tuple[T, T]]: Pairs of consecutive items.

utils.grouping.split_by_chunk_size(
iterable: collections.abc.Iterable[utils.grouping.T],
chunk_size: int,
custom_size_func: Callable[[utils.grouping.T], int] = lambda x: ...,
*,
drop_incomplete_chunk: bool = False,
) collections.abc.Generator[list[utils.grouping.T], None, None]#

Split an iterable into chunks of the specified size.

Args: iterable (Iterable[T]): The input iterable to be split. chunk_size (int): Size of each chunk. custom_size_func (typing.Callable): function drop_incomplete_chunk (bool, optional): If True, drops the last chunk if its size is less than the specified chunk size. Defaults to False.

Yields:

  • Generator[list[T], None, None]: Chunks of the input iterable.

utils.grouping.split_into_n_chunks(
iterable: collections.abc.Iterable[utils.grouping.T],
num_chunks: int,
) collections.abc.Generator[list[utils.grouping.T], None, None]#

Split an iterable into a specified number of chunks.

Args: iterable (Iterable[T]): The input iterable to be split. num_chunks (int): The desired number of chunks.

Yields:

  • Generator[list[T], None, None]: Chunks of the input iterable.