pysatl_tsp.core.processor.combine_handler
Module Contents
Classes
A handler that combines outputs from multiple handlers processing the same data. |
API
- class pysatl_tsp.core.processor.combine_handler.CombineHandler(combine_func: Callable[[list[Any]], pysatl_tsp.core.U], *handlers: pysatl_tsp.core.Handler[pysatl_tsp.core.T, Any], continue_on_partial: bool = True)[source]
Bases:
pysatl_tsp.core.Handler[pysatl_tsp.core.T,pysatl_tsp.core.U]A handler that combines outputs from multiple handlers processing the same data.
This handler feeds the same source data to multiple handlers in parallel and combines their outputs using a user-provided function. It’s useful for scenarios where you need to process the same data in different ways and then merge the results, such as feature extraction, multi-model predictions, or parallel transformations.
- Parameters:
combine_func – Function that combines values from all handlers into a single output
handlers – Variable number of handlers whose outputs will be combined
continue_on_partial – Whether to continue when some handlers are exhausted, defaults to True
- Example:
# Create a data source data_source = SimpleDataProvider([1, 2, 3, 4, 5]) # Define handlers for different transformations square_handler = MappingHandler(map_func=lambda x: x * x) double_handler = MappingHandler(map_func=lambda x: 2 * x) str_handler = MappingHandler(map_func=lambda x: f"Value: {x}") # Function to combine outputs from all handlers def combine_results(values): return { "original^2": values[0], "original*2": values[1], "string": values[2] } # Create and use the combine handler combine = CombineHandler( combine_func=combine_results, square_handler, double_handler, str_handler ) combine.set_source(data_source) # Process the data for result in combine: print(result) # Output: # {'original^2': 1, 'original*2': 2, 'string': 'Value: 1'} # {'original^2': 4, 'original*2': 4, 'string': 'Value: 2'} # {'original^2': 9, 'original*2': 6, 'string': 'Value: 3'} # {'original^2': 16, 'original*2': 8, 'string': 'Value: 4'} # {'original^2': 25, 'original*2': 10, 'string': 'Value: 5'}
Initialization
Initialize a combine handler.
- Parameters:
combine_func – Function that combines values from all handlers into a single output
handlers – Variable number of handlers whose outputs will be combined
continue_on_partial – Whether to continue when some handlers are exhausted, defaults to True
- __iter__() collections.abc.Iterator[pysatl_tsp.core.U][source]
Create an iterator that yields combined results from multiple handlers.
This method processes the source data through each handler in parallel and combines their outputs using the combine function. If continue_on_partial is True, it will continue producing outputs even after some handlers are exhausted (using None for exhausted handlers). If False, it will stop when any handler is exhausted.
- Returns:
Iterator yielding combined results
- Raises:
ValueError – If no source has been set