| Conditions | 14 |
| Total Lines | 55 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 osm_poi_matchmaker.dataproviders.hu_dm.hu_dm.process() 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 -*- |
||
| 48 | def process(self): |
||
| 49 | try: |
||
| 50 | soup = save_downloaded_soup('{}'.format(self.link), os.path.join(self.download_cache, self.filename), |
||
| 51 | self.filetype) |
||
| 52 | if soup is not None: |
||
| 53 | text = json.loads(soup) |
||
| 54 | for poi_data in text['stores']: |
||
| 55 | try: |
||
| 56 | if poi_data.get('localeCountry').strip().upper() == 'HU': |
||
| 57 | self.data.name = 'dm' |
||
| 58 | self.data.code = 'hudmche' |
||
| 59 | self.data.postcode = poi_data.get('address')[ |
||
| 60 | 'zip'].strip() |
||
| 61 | street_tmp = poi_data.get( |
||
| 62 | 'address')['street'].split(',')[0] |
||
| 63 | self.data.city = clean_city( |
||
| 64 | poi_data.get('address')['city']) |
||
| 65 | self.data.website = 'https://www.dm.hu{}'.format( |
||
| 66 | poi_data.get('storeUrlPath')) |
||
| 67 | self.data.original = poi_data.get('address')[ |
||
| 68 | 'street'] |
||
| 69 | self.data.lat, self.data.lon = \ |
||
| 70 | check_hu_boundary(poi_data.get('location')[ |
||
| 71 | 'lat'], poi_data.get('location')['lon']) |
||
| 72 | self.data.street, self.data.housenumber, self.data.conscriptionnumber = \ |
||
| 73 | extract_street_housenumber_better_2( |
||
| 74 | street_tmp.title()) |
||
| 75 | if poi_data.get('phone') is not None and poi_data.get('phone') != '': |
||
| 76 | self.data.phone = clean_phone_to_str( |
||
| 77 | poi_data.get('phone')) |
||
| 78 | if poi_data.get('storeNumber') is not None and poi_data.get('storeNumber') != '': |
||
| 79 | self.data.ref = poi_data.get( |
||
| 80 | 'storeNumber').strip() |
||
| 81 | opening = poi_data.get('openingDays') |
||
| 82 | try: |
||
| 83 | for i, d in enumerate(opening): |
||
| 84 | if d.get('weekDay') is not None and 1 <= d.get('weekDay') <= 7: |
||
| 85 | day = d.get('weekDay') |
||
| 86 | self.data.day_open( |
||
| 87 | day - 1, d.get('timeSlices')[0].get('opening')) |
||
| 88 | self.data.day_close( |
||
| 89 | day - 1, d.get('timeSlices')[0].get('closing')) |
||
| 90 | except (IndexError, KeyError): |
||
| 91 | logging.warning( |
||
| 92 | 'Exception occurred during opening hours processing') |
||
| 93 | self.data.public_holiday_open = False |
||
| 94 | self.data.add() |
||
| 95 | except Exception as e: |
||
| 96 | logging.error(e) |
||
| 97 | logging.error(poi_data) |
||
| 98 | logging.exception('Exception occurred') |
||
| 99 | |||
| 100 | except Exception as e: |
||
| 101 | logging.error(e) |
||
| 102 | logging.exception('Exception occurred') |
||
| 103 |