Conditions | 9 |
Total Lines | 73 |
Lines | 0 |
Ratio | 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 | #!/usr/bin/env python |
||
36 | def main(metadata_path, output_path, print_source=False): |
||
37 | metadata_path = os.path.abspath(metadata_path) |
||
38 | metadata_dir = os.path.dirname(metadata_path) |
||
39 | |||
40 | meta_loader = MetaLoader() |
||
41 | data = meta_loader.load(metadata_path) |
||
42 | |||
43 | action_name = data['name'] |
||
44 | entry_point = data['entry_point'] |
||
45 | |||
46 | workflow_metadata_path = os.path.join(metadata_dir, entry_point) |
||
47 | chainspec = meta_loader.load(workflow_metadata_path) |
||
48 | |||
49 | chain_holder = ChainHolder(chainspec, 'workflow') |
||
50 | |||
51 | graph_label = '%s action-chain workflow visualization' % (action_name) |
||
52 | |||
53 | graph_attr = { |
||
54 | 'rankdir': 'TD', |
||
55 | 'labelloc': 't', |
||
56 | 'fontsize': '15', |
||
57 | 'label': graph_label |
||
58 | } |
||
59 | node_attr = {} |
||
60 | dot = Digraph(comment='Action chain work-flow visualization', |
||
61 | node_attr=node_attr, graph_attr=graph_attr, format='png') |
||
62 | # dot.body.extend(['rankdir=TD', 'size="10,5"']) |
||
63 | |||
64 | # Add all nodes |
||
65 | node = chain_holder.get_next_node() |
||
66 | while node: |
||
67 | dot.node(node.name, node.name) |
||
68 | node = chain_holder.get_next_node(curr_node_name=node.name) |
||
69 | |||
70 | # Add connections |
||
71 | node = chain_holder.get_next_node() |
||
72 | processed_nodes = sets.Set([node.name]) |
||
73 | nodes = [node] |
||
74 | while nodes: |
||
75 | previous_node = nodes.pop() |
||
76 | success_node = chain_holder.get_next_node(curr_node_name=previous_node.name, |
||
77 | condition='on-success') |
||
78 | failure_node = chain_holder.get_next_node(curr_node_name=previous_node.name, |
||
79 | condition='on-failure') |
||
80 | |||
81 | # Add success node (if any) |
||
82 | if success_node: |
||
83 | dot.edge(previous_node.name, success_node.name, constraint='true', |
||
84 | color='green', label='on success') |
||
85 | if success_node.name not in processed_nodes: |
||
86 | nodes.append(success_node) |
||
87 | processed_nodes.add(success_node.name) |
||
88 | |||
89 | # Add failure node (if any) |
||
90 | if failure_node: |
||
91 | dot.edge(previous_node.name, failure_node.name, constraint='true', |
||
92 | color='red', label='on failure') |
||
93 | if failure_node.name not in processed_nodes: |
||
94 | nodes.append(failure_node) |
||
95 | processed_nodes.add(failure_node.name) |
||
96 | |||
97 | if print_source: |
||
98 | print(dot.source) |
||
99 | |||
100 | if output_path: |
||
101 | output_path = os.path.join(output_path, action_name) |
||
102 | else: |
||
103 | output_path = output_path or os.path.join(os.getcwd(), action_name) |
||
104 | |||
105 | dot.format = 'png' |
||
106 | dot.render(output_path) |
||
107 | |||
108 | print('Graph saved at %s' % (output_path + '.png')) |
||
109 | |||
122 |