Conditions | 5 |
Total Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | from __future__ import ( |
||
56 | def _run(self, events, workflowExecution): |
||
57 | # Run the statemachine on the events |
||
58 | results = self.statemachine.eval(events) |
||
59 | |||
60 | # Now we can do 4 things: |
||
61 | # - Complete the workflow |
||
62 | # - Fail the workflow |
||
63 | # - Schedule more activities |
||
64 | # - Nothing |
||
65 | decisions = swf.Layer1Decisions() |
||
66 | |||
67 | if self.statemachine.is_succeeded: |
||
68 | self.sqs.send_message(self.output_queue, json.dumps({ |
||
69 | 'time': time.time(), |
||
70 | 'type': 'WORKFLOW_COMPLETED', |
||
71 | 'data': { |
||
72 | 'workflow': workflowExecution |
||
73 | } |
||
74 | })) |
||
75 | decisions.complete_workflow_execution(result=None) |
||
76 | return decisions |
||
77 | |||
78 | elif self.statemachine.is_failed: |
||
79 | # FIXME: Improve error reporting |
||
80 | self.sqs.send_message(self.output_queue, json.dumps({ |
||
81 | 'time': time.time(), |
||
82 | 'type': 'WORKFLOW_FAILED', |
||
83 | 'data': { |
||
84 | 'workflow': workflowExecution |
||
85 | } |
||
86 | })) |
||
87 | decisions.fail_workflow_execution(reason='State machine aborted') |
||
88 | return decisions |
||
89 | |||
90 | # We are still going, start any ready activity |
||
91 | for next_step in results: |
||
92 | |||
93 | activity = next_step.activity |
||
94 | # FIXME: We are assuming JSON activity input here |
||
95 | activity_input = ( |
||
96 | json.dumps(next_step.activity_input) |
||
97 | if next_step.activity_input is not None |
||
98 | else None |
||
99 | ) |
||
100 | |||
101 | self.sqs.send_message(self.output_queue, json.dumps({ |
||
102 | 'time': time.time(), |
||
103 | 'type': 'ACTIVITY_SCHEDULED', |
||
104 | 'data': { |
||
105 | 'workflow': workflowExecution, |
||
106 | 'activity': { |
||
107 | 'activityId': next_step.name, |
||
108 | 'activityType': activity.name, |
||
109 | 'activityVersion': activity.version |
||
110 | } |
||
111 | } |
||
112 | })) |
||
113 | decisions.schedule_activity_task( |
||
114 | activity_id=next_step.name, |
||
115 | activity_type_name=activity.name, |
||
116 | activity_type_version=activity.version, |
||
117 | task_list=activity.task_list, |
||
118 | control=None, # FIXME: Do we want to pass context data? |
||
119 | heartbeat_timeout=activity.heartbeat_timeout, |
||
120 | schedule_to_close_timeout=activity.schedule_to_close_timeout, |
||
121 | schedule_to_start_timeout=activity.schedule_to_start_timeout, |
||
122 | start_to_close_timeout=activity.start_to_close_timeout, |
||
123 | input=activity_input, |
||
124 | ) |
||
125 | |||
126 | return decisions |
||
127 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.