Decorator pipeline example using List
Decorator pipeline example using List#
from typing import Callable, List
Define a callable type
First [None] indicates input argument for the callable function
Second None indicates output argument for the callable function
Pipeline_Step = Callable[[None], None]
Create a pipeline variable to hold a list of step
This list represents the collection of all the steps in the pipeline
pipeline: List[Pipeline_Step] = []
Create a function to register callables (i.e. functions) via a decorator
def register_step(step: Pipeline_Step) -> None:
print(f"Added {step.__name__} to pipeline.")
pipeline.append(step)
Decorate callables using the register_step function
The callable is added to the pipeline when the callable is defined
@register_step
def step_one() -> None:
print("Inside Step 1")
Added step_one to pipeline.
@register_step
def step_two() -> None:
print("Inside Step 2")
Added step_two to pipeline.
To execute all steps in the pipeline, simply loop thru the list and call each registered callable
for step in pipeline:
step()
Inside Step 1
Inside Step 2