Document utilities

This commit is contained in:
Andras Schmelczer 2022-07-10 19:37:45 +02:00
parent 2b378114aa
commit 00568ca6d3
No known key found for this signature in database
GPG key ID: 0EA1BC97D0AB076E
21 changed files with 371 additions and 29 deletions

View file

@ -4,6 +4,22 @@ T = TypeVar("T")
def unchunk(chunks: Iterable[Optional[Iterable[T]]]) -> Iterable[T]:
"""Turn a stream of chunks of items into a stream of items (flatten operation).
The inverse operation of [chunk][great_ai.utilities.chunk.chunk].
Useful for parallel processing.
Examples:
>>> list(unchunk([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Args:
chunks: Stream of chunks to unpack.
Yields:
The next item in the flattened iterable.
"""
for chunk in chunks:
if chunk is not None:
yield from chunk