Conditions | 31 |
Total Lines | 116 |
Lines | 38 |
Ratio | 32.76 % |
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.__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 | #!/usr/bin/env python |
||
126 | def __preemptive_digipeat(self, frame, recv_port, recv_port_name): |
||
127 | # can't digipeat packets if we are in the expended path |
||
128 | if apex.routing.has_seen(self.port_map, frame): |
||
129 | return |
||
130 | |||
131 | selected_hop = {} |
||
132 | node = None |
||
133 | ssid = 0 |
||
134 | for hop_index in reversed(range(0, len(frame['path']))): |
||
135 | hop = frame['path'][hop_index] |
||
136 | # If this is the last node before a spent node, or a spent node itself, we are done |
||
137 | if apex.routing.is_hop_consumed(hop) or apex.routing.is_hop_consumed(frame['path'][hop_index-1]): |
||
138 | break |
||
139 | split_hop = hop.split('-') |
||
140 | node = split_hop[0].upper() |
||
141 | if len(split_hop) >= 2 and split_hop[1]: |
||
142 | ssid = int(split_hop[1]) |
||
143 | else: |
||
144 | continue |
||
145 | |||
146 | band_path = None |
||
147 | band_path_net = None |
||
148 | band_match = self.BAND_PATH_REGEX.match(node) |
||
149 | if band_match is not None: |
||
150 | band_path = band_match.group(1) |
||
151 | band_path_net = band_match.group(2) |
||
152 | |||
153 | if not band_path: |
||
154 | continue |
||
155 | |||
156 | for port_name in self.port_map.keys(): |
||
157 | port = self.port_map[port_name] |
||
158 | if band_path_net and node == port['net']: |
||
159 | # only when a ssid is present should it be treated preemptively if it is a band path |
||
160 | if not selected_hop: |
||
161 | selected_hop['index'] = hop_index |
||
162 | selected_hop['hop'] = hop |
||
163 | selected_hop['node'] = node |
||
164 | selected_hop['ssid'] = ssid |
||
165 | selected_hop['port_name'] = port_name |
||
166 | selected_hop['port'] = port |
||
167 | selected_hop['band_path'] = band_path |
||
168 | selected_hop['band_path_net'] = band_path_net |
||
169 | elif ssid > selected_hop['ssid']: |
||
170 | selected_hop['index'] = hop_index |
||
171 | selected_hop['hop'] = hop |
||
172 | selected_hop['node'] = node |
||
173 | selected_hop['ssid'] = ssid |
||
174 | selected_hop['port_name'] = port_name |
||
175 | selected_hop['port'] = port |
||
176 | selected_hop['band_path'] = band_path |
||
177 | selected_hop['band_path_net'] = band_path_net |
||
178 | elif not band_path_net and port['net'].startswith(band_path): |
||
179 | # only when a ssid is present should it be treated preemptively if it is a band path |
||
180 | if not selected_hop: |
||
181 | selected_hop['index'] = hop_index |
||
182 | selected_hop['hop'] = hop |
||
183 | selected_hop['node'] = node |
||
184 | selected_hop['ssid'] = ssid |
||
185 | selected_hop['port_name'] = port_name |
||
186 | selected_hop['port'] = port |
||
187 | selected_hop['band_path'] = band_path |
||
188 | View Code Duplication | selected_hop['band_path_net'] = band_path_net |
|
189 | elif ssid > selected_hop['ssid']: |
||
190 | selected_hop['index'] = hop_index |
||
191 | selected_hop['hop'] = hop |
||
192 | selected_hop['node'] = node |
||
193 | selected_hop['ssid'] = ssid |
||
194 | selected_hop['port_name'] = port_name |
||
195 | selected_hop['port'] = port |
||
196 | selected_hop['band_path'] = band_path |
||
197 | selected_hop['band_path_net'] = band_path_net |
||
198 | for hop_index in reversed(range(0, len(frame['path']))): |
||
199 | hop = frame['path'][hop_index] |
||
200 | # If this is the last node before a spent node, or a spent node itself, we are done |
||
201 | if apex.routing.is_hop_consumed(hop) or apex.routing.is_hop_consumed(frame['path'][hop_index-1]): |
||
202 | break |
||
203 | elif selected_hop and selected_hop['index'] <= hop_index: |
||
204 | break |
||
205 | |||
206 | View Code Duplication | for port_name in self.port_map.keys(): |
|
207 | port = self.port_map[port_name] |
||
208 | |||
209 | # since the callsign specifically was specified in the path after the band-path the callsign takes |
||
210 | # precedence |
||
211 | if port['identifier'] == hop: |
||
212 | selected_hop['index'] = hop_index |
||
213 | selected_hop['hop'] = hop |
||
214 | selected_hop['node'] = node |
||
215 | selected_hop['ssid'] = ssid |
||
216 | selected_hop['port_name'] = port_name |
||
217 | selected_hop['port'] = port |
||
218 | selected_hop['band_path'] = None |
||
219 | selected_hop['band_path_net'] = None |
||
220 | |||
221 | if not selected_hop: |
||
222 | return |
||
223 | |||
224 | # now lets digipeat this packet |
||
225 | new_path = [] |
||
226 | for hop_index in range(0, len(frame['path'])): |
||
227 | hop = frame['path'][hop_index] |
||
228 | if not apex.routing.is_hop_consumed(hop): |
||
229 | if hop_index == selected_hop['index']: |
||
230 | if selected_hop['band_path'] is None: |
||
231 | new_path += [hop + '*'] |
||
232 | else: |
||
233 | new_path += [selected_hop['port']['identifier'] + '*'] + [hop + '*'] |
||
234 | elif hop_index > selected_hop['index']: |
||
235 | new_path += [hop] |
||
236 | else: |
||
237 | new_path += [hop] |
||
238 | frame['path'] = new_path |
||
239 | port['tnc'].write(frame, port['tnc_port']) |
||
240 | self.aprsis.write(frame) |
||
241 | return |
||
242 | |||
252 |