To make the fixed template order visible, have run() record each stage as it executes. Learners can then read back the exact sequence the skeleton enforces, the same for every subclass, no matter which steps were overridden.
The base Pipeline now initializes self.steps_run to an empty list in its constructor. Update run() so that, as it reaches each stage, it appends the stage name to self.steps_run in order: "extract", then "transform", then "load", then "after_load". The method must still return the loaded, hooked result exactly as before.
Because the order lives in the template method, every subclass records the identical sequence. That is the whole point of the pattern.
class Pipeline:
def __init__(self, source):
self.source = source
self.steps_run = []
def extract(self):
return list(self.source)
def transform(self, data):
return data
def load(self, data):
return data
def after_load(self, result):
return result
def run(self):
"""Template method that also records each stage name into self.steps_run."""
# TODO: append "extract", "transform", "load", "after_load" to
# self.steps_run as you reach each stage, keeping the fixed order.
data = self.extract()
data = self.transform(data)
result = self.load(data)
result = self.after_load(result)
return result
class UpperPipeline(Pipeline):
def transform(self, data):
return [s.upper() for s in data]
class SumPipeline(Pipeline):
def transform(self, data):
return [sum(data)]
class WrapPipeline(Pipeline):
def after_load(self, result):
return {"result": result, "count": len(result)}
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
To make the fixed template order visible, have run() record each stage as it executes. Learners can then read back the exact sequence the skeleton enforces, the same for every subclass, no matter which steps were overridden.
The base Pipeline now initializes self.steps_run to an empty list in its constructor. Update run() so that, as it reaches each stage, it appends the stage name to self.steps_run in order: "extract", then "transform", then "load", then "after_load". The method must still return the loaded, hooked result exactly as before.
Because the order lives in the template method, every subclass records the identical sequence. That is the whole point of the pattern.
Press Run Tests or ββ© to check your solution.