Conditions | 17 |
Total Lines | 57 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Tests | 31 |
CRAP Score | 17.7744 |
Changes | 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:
Complex classes like build.automate.Automate.find_circuits() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """Automate circuit traces.""" |
||
22 | 1 | def find_circuits(self): |
|
23 | """Discover all circuits in a topology. |
||
24 | |||
25 | Using the list of flows per switch, run control plane |
||
26 | traces to find a list of circuits.""" |
||
27 | 1 | all_flows = {} |
|
28 | 1 | circuits = [] |
|
29 | |||
30 | 1 | for switch in self._tracer.controller.switches.values(): |
|
31 | 1 | all_flows[switch] = [] |
|
32 | 1 | if switch.ofp_version == '0x01': |
|
33 | controller_port = Port10.OFPP_CONTROLLER |
||
34 | else: |
||
35 | 1 | controller_port = Port13.OFPP_CONTROLLER |
|
36 | |||
37 | 1 | try: |
|
38 | 1 | for flow in switch.generic_flows: |
|
39 | 1 | action_ok = False |
|
40 | 1 | in_port_ok = False |
|
41 | 1 | if 'in_port' in flow.match and flow.match['in_port'] != 0: |
|
42 | 1 | in_port_ok = True |
|
43 | 1 | if in_port_ok: |
|
44 | 1 | for action in flow.actions: |
|
45 | 1 | if action.action_type == 'output' \ |
|
46 | and action.port != controller_port: |
||
47 | 1 | action_ok = True |
|
48 | 1 | if action_ok: |
|
49 | 1 | all_flows[switch].append(flow) |
|
50 | except AttributeError: |
||
51 | pass |
||
52 | |||
53 | 1 | for switch, flows in all_flows.items(): |
|
54 | 1 | for flow in flows: |
|
55 | 1 | in_port = flow.match['in_port'] |
|
56 | 1 | vlan = None |
|
57 | 1 | if 'vlan_vid' in flow.match: |
|
58 | vlan = flow.match['vlan_vid'] |
||
59 | 1 | if switch.ofp_version == '0x04': |
|
60 | 1 | in_port = in_port.value |
|
61 | 1 | if vlan: |
|
62 | vlan = vlan.value |
||
63 | 1 | entries = { |
|
64 | 'trace': { |
||
65 | 'switch': { |
||
66 | 'dpid': switch.dpid, |
||
67 | 'in_port': in_port |
||
68 | }, |
||
69 | 'eth': { |
||
70 | 'dl_vlan': vlan |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | 1 | result = self._tracer.tracepath(entries) |
|
75 | 1 | circuits.append({'circuit': format_result(result), |
|
76 | 'entries': entries}) |
||
77 | |||
78 | 1 | self._circuits = clean_circuits(circuits, self._tracer.controller) |
|
79 | |||
172 |