Conditions | 16 |
Total Lines | 63 |
Code Lines | 55 |
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_tesco.hu_tesco.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 -*- |
||
79 | def process(self): |
||
80 | try: |
||
81 | soup = save_downloaded_soup('{}'.format(self.link), os.path.join(self.download_cache, self.filename), |
||
82 | self.filetype) |
||
83 | if soup is not None: |
||
84 | # parse the html using beautiful soap and store in variable `soup` |
||
85 | # script = soup.find('div', attrs={'data-stores':True}) |
||
86 | text = json.loads(str(soup)) |
||
87 | for poi_data in text.get('stores'): |
||
88 | try: |
||
89 | # Assign: code, postcode, city, name, branch, website, original, street, housenumber, |
||
90 | # conscriptionnumber, ref, geom |
||
91 | self.data.branch = poi_data.get('store_name') |
||
92 | self.data.ref = poi_data.get('goldid') |
||
93 | self.data.website = 'https://tesco.hu/aruhazak/aruhaz/{}/'.format( |
||
94 | poi_data.get('urlname')) |
||
95 | opening = json.loads(poi_data.get('opening')) |
||
96 | for i in range(0, 7): |
||
97 | ind = str(i + 1) if i != 6 else '0' |
||
98 | if ind in opening: |
||
99 | self.data.day_open(i, opening[ind][0]) |
||
100 | self.data.day_close(i, opening[ind][1]) |
||
101 | self.data.lat, self.data.lon = check_hu_boundary( |
||
102 | poi_data.get('gpslat'), poi_data.get('gpslng')) |
||
103 | self.data.street, self.data.housenumber, self.data.conscriptionnumber = \ |
||
104 | extract_street_housenumber_better_2( |
||
105 | poi_data.get('address')) |
||
106 | self.data.postcode = poi_data.get('zipcode').strip() |
||
107 | self.data.city = clean_city(query_osm_city_name_gpd( |
||
108 | self.session, self.data.lat, self.data.lon)) |
||
109 | if 'xpres' in poi_data.get('name'): |
||
110 | if self.data.city not in ['Győr', 'Sopron', 'Mosonmagyaróvár', 'Levél']: |
||
111 | self.data.name = 'Tesco Expressz' |
||
112 | self.data.code = 'hutescoexp' |
||
113 | else: |
||
114 | self.data.name = 'S-Market' |
||
115 | self.data.code = 'husmrktexp' |
||
116 | elif 'xtra' in poi_data.get('name'): |
||
117 | self.data.name = 'Tesco Extra' |
||
118 | self.data.code = 'hutescoext' |
||
119 | else: |
||
120 | if self.data.city not in ['Levél']: |
||
121 | self.data.name = 'Tesco' |
||
122 | self.data.code = 'hutescosup' |
||
123 | else: |
||
124 | self.data.name = 'S-Market' |
||
125 | self.data.code = 'husmrktsup' |
||
126 | self.data.original = poi_data.get('address') |
||
127 | if poi_data.get('phone') is not None and poi_data.get('phone') != '': |
||
128 | self.data.phone = clean_phone_to_str( |
||
129 | poi_data.get('phone')) |
||
130 | if poi_data.get('goldid') is not None and poi_data.get('goldid') != '': |
||
131 | self.data.ref = poi_data.get('goldid').strip() |
||
132 | self.data.public_holiday_open = False |
||
133 | self.data.add() |
||
134 | except Exception as e: |
||
135 | logging.error(e) |
||
136 | logging.error(poi_data) |
||
137 | logging.exception('Exception occurred') |
||
138 | |||
139 | except Exception as e: |
||
140 | logging.error(e) |
||
141 | logging.exception('Exception occurred') |
||
142 |