|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from dotflow import DotFlow, action |
| 4 | + |
| 5 | + |
| 6 | +@action(retry=5) |
| 7 | +class StepX: |
| 8 | + |
| 9 | + @action |
| 10 | + def first_function(self): |
| 11 | + return {"foo": "bar"} |
| 12 | + |
| 13 | + @action |
| 14 | + def second_function(self): |
| 15 | + return True |
| 16 | + |
| 17 | + |
| 18 | +@action(retry=5) |
| 19 | +class StepY: |
| 20 | + |
| 21 | + def __init__(self, initial_context): |
| 22 | + print(initial_context.storage, "__init__") |
| 23 | + assert initial_context.storage |
| 24 | + self.variable = True |
| 25 | + |
| 26 | + def auxiliary_function(self): |
| 27 | + """This function will not be executed, because |
| 28 | + it does not have an 'action' decorator. |
| 29 | + """ |
| 30 | + |
| 31 | + @action |
| 32 | + def first_function(self, initial_context): |
| 33 | + print(initial_context.storage, "first_function") |
| 34 | + assert initial_context.storage |
| 35 | + assert self.variable |
| 36 | + |
| 37 | + return {"foo": "bar"} |
| 38 | + |
| 39 | + @action |
| 40 | + def second_function(self, previous_context): |
| 41 | + print(previous_context.storage, "second_function") |
| 42 | + assert previous_context.storage |
| 43 | + |
| 44 | + return True |
| 45 | + |
| 46 | + |
| 47 | +@action |
| 48 | +def simple_step(): |
| 49 | + return "ok" |
| 50 | + |
| 51 | + |
| 52 | +@action |
| 53 | +def extract_task_x(): |
| 54 | + return "extract" |
| 55 | + |
| 56 | + |
| 57 | +@action |
| 58 | +def transform_task_x(initial_context, previous_context): |
| 59 | + print(initial_context.storage, "initial_context") |
| 60 | + print(previous_context.storage, "previous_context") |
| 61 | + |
| 62 | + assert initial_context.storage, {"foo": True} |
| 63 | + assert previous_context.storage, "extract" |
| 64 | + |
| 65 | + return "transform" |
| 66 | + |
| 67 | + |
| 68 | +@action |
| 69 | +def load_task_x(): |
| 70 | + return "load" |
| 71 | + |
| 72 | + |
| 73 | +@action |
| 74 | +def extract_task_y(): |
| 75 | + print("extract") |
| 76 | + return "extract" |
| 77 | + |
| 78 | + |
| 79 | +@action |
| 80 | +def transform_task_y(previous_context): |
| 81 | + print(previous_context.storage, "transform") |
| 82 | + assert previous_context.storage, "extract" |
| 83 | + |
| 84 | + return "transform" |
| 85 | + |
| 86 | + |
| 87 | +@action |
| 88 | +def load_task_y(previous_context): |
| 89 | + print(previous_context.storage, "load") |
| 90 | + assert previous_context.storage, "transform" |
| 91 | + |
| 92 | + return "load" |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + workflow = DotFlow() |
| 97 | + |
| 98 | + workflow.task.add(step=StepX) |
| 99 | + workflow.task.add(step=StepY, initial_context={"foo": "bar"}) |
| 100 | + workflow.task.add(step=simple_step) |
| 101 | + workflow.task.add(step=simple_step) |
| 102 | + workflow.task.add(step=simple_step) |
| 103 | + workflow.task.add(step=simple_step) |
| 104 | + workflow.task.add(step=simple_step) |
| 105 | + workflow.task.add(step=simple_step) |
| 106 | + workflow.task.add(step=simple_step) |
| 107 | + workflow.task.add(step=simple_step) |
| 108 | + workflow.task.add(step=extract_task_x) |
| 109 | + workflow.task.add(step=transform_task_x, initial_context={"foo": True}) |
| 110 | + workflow.task.add(step=load_task_x) |
| 111 | + workflow.task.add(step=extract_task_y) |
| 112 | + workflow.task.add(step=transform_task_y) |
| 113 | + workflow.task.add(step=load_task_y) |
| 114 | + workflow.start() |
| 115 | + |
| 116 | + return workflow |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments