tpcp.misc.TypedIterator#

class tpcp.misc.TypedIterator(data_type: type[DataclassT], aggregations: Sequence[tuple[str, Callable[[list, list], Any]]] = cf([]))[source]#

Helper to iterate over data and collect results.

Parameters:
data_type

A dataclass that defines the result type you expect from each iteration.

aggregations

An optional list of aggregations to apply to the results. This has the form [(result_name, aggregation_function), ...]. If a result-name is in the list, the aggregation will be applied to it, when accessing the respective result attribute (i.e. {result_name}_). If no aggregation is defined for a result, a simple list of all results will be returned.

NULL_VALUE

(Class attribute) The value that is used to initialize the result dataclass and will remain in the results, if no result was for a specific attribute in one or more iterations.

Attributes:
inputs_

List of all input elements that were iterated over.

raw_results_

List of all results as dataclass instances. The attribute of the dataclass instance will have the value of _NOT_SET if no result was set. To check for this, you can use isinstance(val, TypedIterator.NULL_VALUE).

{result_name}_

The aggregated results for the respective result name.

done_

True, if the iterator is done. If the iterator is not done, but you try to access the results, a warning will be raised.

Methods

clone()

Create a new instance of the class with all parameters copied over.

get_params([deep])

Get parameters for this algorithm.

iterate(iterable)

Iterate over the given iterable and yield the input and a new empty result object for each iteration.

set_params(**params)

Set the parameters of this Algorithm.

clone() Self[source]#

Create a new instance of the class with all parameters copied over.

This will create a new instance of the class itself and all nested objects

get_params(deep: bool = True) dict[str, Any][source]#

Get parameters for this algorithm.

Parameters:
deep

Only relevant if object contains nested algorithm objects. If this is the case and deep is True, the params of these nested objects are included in the output using a prefix like nested_object_name__ (Note the two “_” at the end)

Returns:
params

Parameter names mapped to their values.

iterate(iterable: Iterable[T]) Iterator[tuple[T, DataclassT]][source]#

Iterate over the given iterable and yield the input and a new empty result object for each iteration.

Parameters:
iterable

The iterable to iterate over.

Yields:
input, result_object

The input and a new empty result object. The result object is a dataclass instance of the type defined in self.data_type. All values of the result object are set to TypedIterator.NULL_VALUE by default.

set_params(**params: Any) Self[source]#

Set the parameters of this Algorithm.

To set parameters of nested objects use nested_object_name__para_name=.