Features

Helper function for processing data from collections

This module provides functions to iterate over collections in specialized manner not currently supported by standard Python library.

class colltools.MembersDescriptor

Attribute descriptor for variable __member__ of NameRegistry.

Returns a dictionary of <name:instance> of all instances provided by that class and all subclasses.

class colltools.NameRegistry(name: str)

Instance registry of enumerated values.

Subclasses of NameRegistry create one instance of itself for each value in class attribute __values__. Instances can be returned using ResourceType.get() method which makes sure to return the same instance for the same name.

All subclasses of NameRegistry also provide access to instance of all their subclasses.

Goal of this class is to provide alternative enumeration implementation compatible with SQLAlchemy Enum type.

classmethod get(name: str) → colltools.NameRegistry

Returns instance with specified name.

Raises KeyError when instance with given name doesn’t exist.

colltools.batch(iterable: Iterable[Any], step: int) → Iterator[List[Any]]

Iterate over iterable and group items into batches size of step.

This method is intended to be used in cases where you need to split a stream of data into batches of smaller size. For example when storing huge amount to database in batches of 1000 items. It will create an iterator that gives you a list of items in each iteration of size step. Last iteration will yield list of all remaining items.

Example:
>>> for i in batch(range(8), 3): print(i)
[0, 1, 2]
[3, 4, 5]
[6, 7]