| Conditions | 25 |
| Total Lines | 78 |
| Lines | 18 |
| Ratio | 23.08 % |
| Changes | 4 | ||
| 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:
Complex classes like ApexParadigmPlugin.__passive_digipeat() 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 | #!/usr/bin/env python |
||
| 47 | def __passive_digipeat(self, frame, recv_port, recv_port_name): |
||
| 48 | # can't digipeat packets if we are in the expended path |
||
| 49 | if apex.routing.has_seen(self.port_map, frame): |
||
| 50 | return |
||
| 51 | |||
| 52 | for hop_index in range(0, len(frame['path'])): |
||
| 53 | hop = frame['path'][hop_index] |
||
| 54 | if hop[-1] is not '*': |
||
| 55 | split_hop = hop.split('-') |
||
| 56 | node = split_hop[0].upper() |
||
| 57 | if len(split_hop) >= 2 and split_hop[1]: |
||
| 58 | ssid = int(split_hop[1]) |
||
| 59 | else: |
||
| 60 | ssid = 0 |
||
| 61 | |||
| 62 | band_path = None |
||
| 63 | band_path_net = None |
||
| 64 | band_match = self.BAND_PATH_REGEX.match(node) |
||
| 65 | if band_match is not None: |
||
| 66 | band_path = band_match.group(1) |
||
| 67 | band_path_net = band_match.group(2) |
||
| 68 | |||
| 69 | for port_name in self.port_map.keys(): |
||
| 70 | port = self.port_map[port_name] |
||
| 71 | split_port_identifier = port['identifier'].split('-') |
||
| 72 | port_callsign = split_port_identifier[0].upper() |
||
| 73 | if len(split_port_identifier) >= 2 and split_port_identifier[1]: |
||
| 74 | port_ssid = int(split_port_identifier[1]) |
||
| 75 | else: |
||
| 76 | port_ssid = 0 |
||
| 77 | |||
| 78 | if band_path: |
||
| 79 | if band_path_net: |
||
| 80 | if node == port['net']: |
||
| 81 | frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\ |
||
| 82 | View Code Duplication | [hop + '*'] + frame['path'][hop_index+1:] |
|
|
|
|||
| 83 | port['tnc'].write(frame, port['tnc_port']) |
||
| 84 | self.aprsis.write(frame) |
||
| 85 | return |
||
| 86 | else: |
||
| 87 | if port['net'].startswith(node): |
||
| 88 | frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\ |
||
| 89 | View Code Duplication | [hop + '*'] + frame['path'][hop_index+1:] |
|
| 90 | port['tnc'].write(frame, port['tnc_port']) |
||
| 91 | self.aprsis.write(frame) |
||
| 92 | return |
||
| 93 | if node == port_callsign and ssid == port_ssid: |
||
| 94 | if ssid is 0: |
||
| 95 | frame['path'][hop_index] = port_callsign + '*' |
||
| 96 | else: |
||
| 97 | frame['path'][hop_index] = port['identifier'] + '*' |
||
| 98 | port['tnc'].write(frame, port['tnc_port']) |
||
| 99 | self.aprsis.write(frame) |
||
| 100 | return |
||
| 101 | elif node == 'GATE' and port['net'].startswith('2M'): |
||
| 102 | frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [node + '*'] +\ |
||
| 103 | View Code Duplication | frame['path'][hop_index+1:] |
|
| 104 | port['tnc'].write(frame, port['tnc_port']) |
||
| 105 | self.aprsis.write(frame) |
||
| 106 | return |
||
| 107 | if node.startswith('WIDE') and ssid > 1: |
||
| 108 | frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\ |
||
| 109 | [node + '-' + str(ssid-1)] + frame['path'][hop_index+1:] |
||
| 110 | port['tnc'].write(frame, port['tnc_port']) |
||
| 111 | self.aprsis.write(frame) |
||
| 112 | return |
||
| 113 | elif node.startswith('WIDE') and ssid is 1: |
||
| 114 | frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [node + '*'] + frame['path'][hop_index+1:] |
||
| 115 | port['tnc'].write(frame, port['tnc_port']) |
||
| 116 | self.aprsis.write(frame) |
||
| 117 | return |
||
| 118 | elif node.startswith('WIDE') and ssid is 0: |
||
| 119 | frame['path'][hop_index] = node + '*' |
||
| 120 | # no return |
||
| 121 | else: |
||
| 122 | # If we didnt digipeat it then we didn't modify the frame, send it to aprsis as-is |
||
| 123 | self.aprsis.write(frame) |
||
| 124 | return |
||
| 125 | |||
| 252 |