Conditions | 35 |
Total Lines | 108 |
Lines | 30 |
Ratio | 27.78 % |
Changes | 3 | ||
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 | # These imports are for python3 compatability inside python2 |
||
46 | def __passive_digipeat(self, frame, recv_port, recv_port_name): |
||
47 | # Can't digipeat anything when you are the source |
||
48 | for port in self.port_map.values(): |
||
49 | if frame['source'] == port['identifier']: |
||
50 | return |
||
51 | |||
52 | # can't digipeat things we already digipeated. |
||
53 | for hop in frame['path']: |
||
54 | if hop.startswith('WI2ARD') and hop.endswith('*'): |
||
55 | return |
||
56 | |||
57 | for hop_index in range(0, len(frame['path'])): |
||
58 | hop = frame['path'][hop_index] |
||
59 | if hop[-1] is not '*': |
||
60 | split_hop = hop.split('-') |
||
61 | node = split_hop[0].upper() |
||
62 | if len(split_hop) >= 2 and split_hop[1]: |
||
63 | ssid = int(split_hop[1]) |
||
64 | else: |
||
65 | ssid = 0 |
||
66 | |||
67 | band_path = None |
||
68 | band_path_net = None |
||
69 | band_match = self.BAND_PATH_REGEX.match(node) |
||
70 | if band_match is not None: |
||
71 | band_path = band_match.group(1) |
||
72 | band_path_net = band_match.group(2) |
||
73 | |||
74 | for port_name in self.port_map.keys(): |
||
75 | port = self.port_map[port_name] |
||
76 | split_port_identifier = port['identifier'].split('-') |
||
77 | port_callsign = split_port_identifier[0].upper() |
||
78 | if len(split_port_identifier) >= 2 and split_port_identifier[1]: |
||
79 | port_ssid = int(split_port_identifier[1]) |
||
80 | else: |
||
81 | View Code Duplication | port_ssid = 0 |
|
|
|||
82 | |||
83 | if band_path: |
||
84 | if band_path_net: |
||
85 | if node == port['net']: |
||
86 | frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\ |
||
87 | [hop + '*'] + frame['path'][hop_index+1:] |
||
88 | frame_hash = apex.aprs.util.hash_frame(frame) |
||
89 | if frame_hash not in self.packet_cache.values(): |
||
90 | self.packet_cache[str(frame_hash)] = frame_hash |
||
91 | port['tnc'].write(frame, port['tnc_port']) |
||
92 | View Code Duplication | self.aprsis.send(frame) |
|
93 | click.echo(port_name + ' >> ' + apex.aprs.util.format_aprs_frame(frame)) |
||
94 | return |
||
95 | else: |
||
96 | if port['net'].startswith(node): |
||
97 | frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\ |
||
98 | [hop + '*'] + frame['path'][hop_index+1:] |
||
99 | frame_hash = apex.aprs.util.hash_frame(frame) |
||
100 | if frame_hash not in self.packet_cache.values(): |
||
101 | self.packet_cache[str(frame_hash)] = frame_hash |
||
102 | port['tnc'].write(frame, port['tnc_port']) |
||
103 | self.aprsis.send(frame) |
||
104 | click.echo(port_name + ' >> ' + apex.aprs.util.format_aprs_frame(frame)) |
||
105 | return |
||
106 | if node == port_callsign and ssid == port_ssid: |
||
107 | if ssid is 0: |
||
108 | frame['path'][hop_index] = port_callsign + '*' |
||
109 | else: |
||
110 | frame['path'][hop_index] = port['identifier'] + '*' |
||
111 | frame_hash = apex.aprs.util.hash_frame(frame) |
||
112 | if frame_hash not in self.packet_cache.values(): |
||
113 | self.packet_cache[str(frame_hash)] = frame_hash |
||
114 | View Code Duplication | port['tnc'].write(frame, port['tnc_port']) |
|
115 | self.aprsis.send(frame) |
||
116 | click.echo(port_name + ' >> ' + apex.aprs.util.format_aprs_frame(frame)) |
||
117 | return |
||
118 | elif node == 'GATE' and port['net'].startswith('2M'): |
||
119 | frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [node + '*'] +\ |
||
120 | frame['path'][hop_index+1:] |
||
121 | frame_hash = apex.aprs.util.hash_frame(frame) |
||
122 | if frame_hash not in self.packet_cache.values(): |
||
123 | self.packet_cache[str(frame_hash)] = frame_hash |
||
124 | port['tnc'].write(frame, port['tnc_port']) |
||
125 | self.aprsis.send(frame) |
||
126 | click.echo(port_name + ' >> ' + apex.aprs.util.format_aprs_frame(frame)) |
||
127 | return |
||
128 | if node.startswith('WIDE') and ssid > 1: |
||
129 | frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] +\ |
||
130 | [node + '-' + str(ssid-1)] + frame['path'][hop_index+1:] |
||
131 | frame_hash = apex.aprs.util.hash_frame(frame) |
||
132 | if frame_hash not in self.packet_cache.values(): |
||
133 | self.packet_cache[str(frame_hash)] = frame_hash |
||
134 | recv_port['tnc'].write(frame, recv_port['tnc_port']) |
||
135 | self.aprsis.send(frame) |
||
136 | click.echo(recv_port_name + ' >> ' + apex.aprs.util.format_aprs_frame(frame)) |
||
137 | return |
||
138 | elif node.startswith('WIDE') and ssid is 1: |
||
139 | frame['path'] = frame['path'][:hop_index] + [recv_port['identifier'] + '*'] + [node + '*'] + frame['path'][hop_index+1:] |
||
140 | frame_hash = apex.aprs.util.hash_frame(frame) |
||
141 | if frame_hash not in self.packet_cache.values(): |
||
142 | self.packet_cache[str(frame_hash)] = frame_hash |
||
143 | recv_port['tnc'].write(frame, recv_port['tnc_port']) |
||
144 | self.aprsis.send(frame) |
||
145 | click.echo(recv_port_name + ' >> ' + apex.aprs.util.format_aprs_frame(frame)) |
||
146 | return |
||
147 | elif node.startswith('WIDE') and ssid is 0: |
||
148 | frame['path'][hop_index] = node + '*' |
||
149 | # no return |
||
150 | else: |
||
151 | # If we didnt digipeat it then we didn't modify the frame, send it to aprsis as-is |
||
152 | self.aprsis.send(frame) |
||
153 | return |
||
154 | |||
289 |