pysatl_tsp.core.processor.tee_handler
Module Contents
Classes
A handler that processes data through two parallel paths and combines the results. |
Data
API
- pysatl_tsp.core.processor.tee_handler.S = 'TypeVar(...)'
- class pysatl_tsp.core.processor.tee_handler.TeeHandler(processor: pysatl_tsp.core.Handler[pysatl_tsp.core.T, pysatl_tsp.core.processor.tee_handler.S], combine_func: Callable[[pysatl_tsp.core.T, pysatl_tsp.core.processor.tee_handler.S], pysatl_tsp.core.U])[source]
Bases:
pysatl_tsp.core.Handler[pysatl_tsp.core.T,pysatl_tsp.core.U]A handler that processes data through two parallel paths and combines the results.
This handler takes input data from a source, sends it through both the original path and a processing path simultaneously, and then combines each pair of outputs using a provided function. It’s useful for operations where you need to preserve the original data while also using a transformed version of it.
- Parameters:
processor – Handler that processes the tee’d data stream
combine_func – Function that combines original and processed values
- Example:
# Create a data source data_source = SimpleDataProvider([1, 2, 3, 4, 5]) # Define a processor that squares the values square_processor = MappingHandler(map_func=lambda x: x * x) # Define a function to combine original and processed values def combine(original, processed): return f"{original} squared is {processed}" # Create and use the tee handler tee_handler = TeeHandler(processor=square_processor, combine_func=combine) tee_handler.set_source(data_source) # Process the data for result in tee_handler: print(result) # Output: # 1 squared is 1 # 2 squared is 4 # 3 squared is 9 # 4 squared is 16 # 5 squared is 25
Initialization
Initialize a tee handler.
- Parameters:
processor – Handler that processes the tee’d data stream
combine_func – Function that combines original and processed values
- __iter__() collections.abc.Iterator[pysatl_tsp.core.U][source]
Create an iterator that yields combined results from original and processed data.
This method creates two identical iterators from the source, processes one through the processor, and then combines corresponding items from both streams using the combine function.
- Returns:
Iterator yielding combined results
- Raises:
ValueError – If no source has been set