Conditions | 25 |
Total Lines | 99 |
Lines | 50 |
Ratio | 50.51 % |
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 CrabAdresSchemaNode.validator() 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 | # -*- coding: utf-8 -*- |
||
188 | def validator(self, node, adres): |
||
189 | if not adres: |
||
190 | return |
||
191 | request = self.bindings['request'] |
||
192 | crab_gateway = request.crab_gateway() |
||
193 | if 'land' in adres: |
||
194 | land = adres.get('land') |
||
195 | try: |
||
196 | try: |
||
197 | pycountry.countries.get(alpha2=land) |
||
198 | except KeyError: |
||
199 | pycountry.countries.get(alpha_2=land) |
||
200 | except KeyError: |
||
201 | raise colander.Invalid( |
||
202 | node, |
||
203 | 'ongeldige landcode %s, dit is geen ISO 3166 code' % |
||
204 | land |
||
205 | ) |
||
206 | if land == 'BE': |
||
207 | gemeente = adres.get('gemeente', None) |
||
208 | gemeente_id = adres.get('gemeente_id', None) |
||
209 | straat_id = adres.get('straat_id', None) |
||
210 | huisnummer_id = adres.get('huisnummer_id', None) |
||
211 | postcode = adres.get('postcode', None) |
||
212 | subadres_id = adres.get('subadres_id', None) |
||
213 | if gemeente_id is None: |
||
214 | raise colander.Invalid( |
||
215 | node, |
||
216 | 'geen correcte gemeente_id gevonden voor de gemeente {0}'.format(gemeente) |
||
217 | ) |
||
218 | if gemeente is None: |
||
219 | raise colander.Invalid( |
||
220 | node, |
||
221 | 'ongeldig gemeente_id {0}'.format(gemeente_id) |
||
222 | ) |
||
223 | View Code Duplication | if straat_id is not None: |
|
|
|||
224 | gemeente = crab_gateway.get_gemeente_by_id(gemeente_id) |
||
225 | try: |
||
226 | straat = crab_gateway.get_straat_by_id(straat_id) |
||
227 | except (GatewayRuntimeException, GatewayResourceNotFoundException, AttributeError): |
||
228 | raise colander.Invalid( |
||
229 | node, |
||
230 | 'ongeldig straat_id' |
||
231 | ) |
||
232 | if straat.gemeente_id != gemeente_id: |
||
233 | raise colander.Invalid( |
||
234 | node, |
||
235 | 'de straat %s met id %s ligt niet in gemeente %s' % |
||
236 | (adres.get('straat', ''), straat_id, gemeente.naam) |
||
237 | ) |
||
238 | if huisnummer_id is not None: |
||
239 | try: |
||
240 | huisnummer = crab_gateway.get_huisnummer_by_id(huisnummer_id) |
||
241 | except (GatewayRuntimeException, GatewayResourceNotFoundException, AttributeError): |
||
242 | raise colander.Invalid( |
||
243 | node, |
||
244 | 'ongeldig huisnummer_id' |
||
245 | ) |
||
246 | if huisnummer.straat_id != straat_id: |
||
247 | raise colander.Invalid( |
||
248 | node, |
||
249 | 'het huisnummer %s met id %s ligt niet in straat %s' % |
||
250 | (adres.get('huisnummer', ''), huisnummer_id, straat.label) |
||
251 | ) |
||
252 | if postcode is not None: |
||
253 | postkanton = crab_gateway.get_postkanton_by_huisnummer(huisnummer_id) |
||
254 | if postcode != str(postkanton.id): |
||
255 | raise colander.Invalid( |
||
256 | node, |
||
257 | 'postcode %s is niet correct voor dit adres, mogelijke postcode is %s' % |
||
258 | (postcode, postkanton.id) |
||
259 | ) |
||
260 | if subadres_id is not None: |
||
261 | try: |
||
262 | subadres = crab_gateway.get_subadres_by_id(subadres_id) |
||
263 | except (GatewayRuntimeException, GatewayResourceNotFoundException, AttributeError): |
||
264 | raise colander.Invalid( |
||
265 | node, |
||
266 | 'ongeldig subadres_id' |
||
267 | ) |
||
268 | if subadres.huisnummer_id != huisnummer_id: |
||
269 | raise colander.Invalid( |
||
270 | node, |
||
271 | 'het subadres %s met id %s ligt niet op huisnummer %s' % |
||
272 | (adres.get('subadres', ''), subadres_id, huisnummer.huisnummer) |
||
273 | ) |
||
274 | if straat_id is None and huisnummer_id is not None: |
||
275 | raise colander.Invalid( |
||
276 | node, |
||
277 | 'als er een huisnummer_id wordt gegeven, moet men ook het straat_id invullen' |
||
278 | ) |
||
279 | if huisnummer_id is None and postcode is not None: |
||
280 | postkantons = crab_gateway.list_postkantons_by_gemeente(gemeente_id) |
||
281 | postkantons = [str(pk.id) for pk in postkantons] |
||
282 | if postcode not in postkantons: |
||
283 | raise colander.Invalid( |
||
284 | node, |
||
285 | 'postcode %s is niet correct voor dit adres, mogelijke postcode(s) zijn %s' % |
||
286 | (postcode, postkantons) |
||
287 | ) |
||
289 |