Conditions | 36 |
Total Lines | 124 |
Lines | 38 |
Ratio | 30.65 % |
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.__preemptive_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 |
||
155 | def __preemptive_digipeat(self, frame, recv_port, recv_port_name): |
||
156 | # Can't digipeat anything when you are the source |
||
157 | for port in self.port_map.values(): |
||
158 | if frame['source'] == port['identifier']: |
||
159 | return |
||
160 | |||
161 | # can't digipeat things we already digipeated. |
||
162 | for hop in frame['path']: |
||
163 | if hop.startswith('WI2ARD') and hop.endswith('*'): |
||
164 | return |
||
165 | |||
166 | selected_hop = {} |
||
167 | for hop_index in reversed(range(0, len(frame['path']))): |
||
168 | hop = frame['path'][hop_index] |
||
169 | # If this is the last node before a spent node, or a spent node itself, we are done |
||
170 | if hop[-1] == '*' or frame['path'][hop_index-1][-1] == '*': |
||
171 | break |
||
172 | split_hop = hop.split('-') |
||
173 | node = split_hop[0].upper() |
||
174 | if len(split_hop) >= 2 and split_hop[1]: |
||
175 | ssid = int(split_hop[1]) |
||
176 | else: |
||
177 | continue |
||
178 | |||
179 | band_path = None |
||
180 | band_path_net = None |
||
181 | band_match = self.BAND_PATH_REGEX.match(node) |
||
182 | if band_match is not None: |
||
183 | band_path = band_match.group(1) |
||
184 | band_path_net = band_match.group(2) |
||
185 | |||
186 | if not band_path: |
||
187 | continue |
||
188 | View Code Duplication | ||
189 | for port_name in self.port_map.keys(): |
||
190 | port = self.port_map[port_name] |
||
191 | if band_path_net and node == port['net']: |
||
192 | # only when a ssid is present should it be treated preemptively if it is a band path |
||
193 | if not selected_hop: |
||
194 | selected_hop['index'] = hop_index |
||
195 | selected_hop['hop'] = hop |
||
196 | selected_hop['node'] = node |
||
197 | selected_hop['ssid'] = ssid |
||
198 | selected_hop['port_name'] = port_name |
||
199 | selected_hop['port'] = port |
||
200 | selected_hop['band_path'] = band_path |
||
201 | selected_hop['band_path_net'] = band_path_net |
||
202 | elif ssid > selected_hop['ssid']: |
||
203 | selected_hop['index'] = hop_index |
||
204 | selected_hop['hop'] = hop |
||
205 | selected_hop['node'] = node |
||
206 | View Code Duplication | selected_hop['ssid'] = ssid |
|
207 | selected_hop['port_name'] = port_name |
||
208 | selected_hop['port'] = port |
||
209 | selected_hop['band_path'] = band_path |
||
210 | selected_hop['band_path_net'] = band_path_net |
||
211 | elif not band_path_net and port['net'].startswith(band_path): |
||
212 | # only when a ssid is present should it be treated preemptively if it is a band path |
||
213 | if not selected_hop: |
||
214 | selected_hop['index'] = hop_index |
||
215 | selected_hop['hop'] = hop |
||
216 | selected_hop['node'] = node |
||
217 | selected_hop['ssid'] = ssid |
||
218 | selected_hop['port_name'] = port_name |
||
219 | selected_hop['port'] = port |
||
220 | selected_hop['band_path'] = band_path |
||
221 | selected_hop['band_path_net'] = band_path_net |
||
222 | elif ssid > selected_hop['ssid']: |
||
223 | selected_hop['index'] = hop_index |
||
224 | selected_hop['hop'] = hop |
||
225 | selected_hop['node'] = node |
||
226 | selected_hop['ssid'] = ssid |
||
227 | selected_hop['port_name'] = port_name |
||
228 | selected_hop['port'] = port |
||
229 | selected_hop['band_path'] = band_path |
||
230 | selected_hop['band_path_net'] = band_path_net |
||
231 | for hop_index in reversed(range(0, len(frame['path']))): |
||
232 | hop = frame['path'][hop_index] |
||
233 | # If this is the last node before a spent node, or a spent node itself, we are done |
||
234 | if hop[-1] == '*' or frame['path'][hop_index-1][-1] == '*': |
||
235 | break |
||
236 | elif selected_hop and selected_hop['index'] <= hop_index: |
||
237 | break |
||
238 | |||
239 | for port_name in self.port_map.keys(): |
||
240 | port = self.port_map[port_name] |
||
241 | |||
242 | # since the callsign specifically was specified in the path after the band-path the callsign takes |
||
243 | # precedence |
||
244 | if port['identifier'] == hop: |
||
245 | selected_hop['index'] = hop_index |
||
246 | selected_hop['hop'] = hop |
||
247 | selected_hop['node'] = node |
||
248 | selected_hop['ssid'] = ssid |
||
249 | selected_hop['port_name'] = port_name |
||
250 | selected_hop['port'] = port |
||
251 | selected_hop['band_path'] = None |
||
252 | selected_hop['band_path_net'] = None |
||
253 | |||
254 | if not selected_hop: |
||
255 | return |
||
256 | |||
257 | # now lets digipeat this packet |
||
258 | new_path = [] |
||
259 | for hop_index in range(0, len(frame['path'])): |
||
260 | hop = frame['path'][hop_index] |
||
261 | if hop[-1] != '*': |
||
262 | if hop_index == selected_hop['index']: |
||
263 | if selected_hop['band_path'] is None: |
||
264 | new_path += [hop + '*'] |
||
265 | else: |
||
266 | new_path += [selected_hop['port']['identifier'] + '*'] + [hop + '*'] |
||
267 | elif hop_index > selected_hop['index']: |
||
268 | new_path += [hop] |
||
269 | else: |
||
270 | new_path += [hop] |
||
271 | frame['path'] = new_path |
||
272 | frame_hash = apex.aprs.util.hash_frame(frame) |
||
273 | if frame_hash not in self.packet_cache.values(): |
||
274 | self.packet_cache[str(frame_hash)] = frame_hash |
||
275 | selected_hop['port']['tnc'].write(frame, selected_hop['port']['tnc_port']) |
||
276 | self.aprsis.send(frame) |
||
277 | click.echo(selected_hop['port_name'] + ' >> ' + apex.aprs.util.format_aprs_frame(frame)) |
||
278 | return |
||
279 | |||
289 |