| Total Complexity | 42 |
| Total Lines | 190 |
| Duplicated Lines | 12.63 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like osm_poi_matchmaker.dao.data_handlers 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 -*- |
||
| 2 | |||
| 3 | try: |
||
| 4 | import traceback |
||
| 5 | import logging |
||
| 6 | import sys |
||
| 7 | import hashlib |
||
| 8 | from osm_poi_matchmaker.dao.data_structure import City, POI_common, POI_address, Street_type |
||
| 9 | from osm_poi_matchmaker.libs import address |
||
| 10 | from osm_poi_matchmaker.dao import poi_array_structure |
||
| 11 | except ImportError as err: |
||
| 12 | logging.error('Error %s import module: %s', __name__, err) |
||
| 13 | logging.error(traceback.print_exc()) |
||
| 14 | sys.exit(128) |
||
| 15 | |||
| 16 | POI_COLS = poi_array_structure.POI_COLS |
||
| 17 | |||
| 18 | |||
| 19 | def get_or_create(session, model, **kwargs): |
||
| 20 | instance = session.query(model).filter_by(**kwargs).first() |
||
| 21 | if instance: |
||
| 22 | logging.debug('Already added: %s' ,instance) |
||
| 23 | return instance |
||
| 24 | else: |
||
| 25 | try: |
||
| 26 | instance = model(**kwargs) |
||
| 27 | session.add(instance) |
||
| 28 | return instance |
||
| 29 | except Exception as e: |
||
| 30 | logging.error('Can't add to database. (%s)', e) |
||
|
|
|||
| 31 | logging.error(traceback.print_exc()) |
||
| 32 | raise (e) |
||
| 33 | |||
| 34 | |||
| 35 | def get_or_create_poi(session, model, **kwargs): |
||
| 36 | if kwargs['poi_common_id'] is not None: |
||
| 37 | if kwargs['poi_common_id'] is not None and kwargs['poi_addr_city'] is not None and ( |
||
| 38 | (kwargs['poi_addr_street'] and kwargs['poi_addr_housenumber'] is not None) or ( |
||
| 39 | kwargs['poi_conscriptionnumber'] is not None)): |
||
| 40 | logging.debug('Fully filled basic data record') |
||
| 41 | else: |
||
| 42 | logging.warning('Missing record data: %s', kwargs) |
||
| 43 | instance = session.query(model)\ |
||
| 44 | .filter_by(poi_common_id=kwargs['poi_common_id'])\ |
||
| 45 | .filter_by(poi_addr_city=kwargs['poi_addr_city'])\ |
||
| 46 | .filter_by(poi_addr_street=kwargs['poi_addr_street'])\ |
||
| 47 | .filter_by(poi_addr_housenumber=kwargs['poi_addr_housenumber'])\ |
||
| 48 | .filter_by(poi_conscriptionnumber=kwargs['poi_conscriptionnumber'])\ |
||
| 49 | .filter_by(poi_branch=kwargs['poi_branch'])\ |
||
| 50 | .first() |
||
| 51 | if instance: |
||
| 52 | logging.debug('Already added: %s', instance) |
||
| 53 | return instance |
||
| 54 | else: |
||
| 55 | try: |
||
| 56 | instance = model(**kwargs) |
||
| 57 | session.add(instance) |
||
| 58 | return instance |
||
| 59 | except Exception as e: |
||
| 60 | logging.error('Can't add to database. (%s)', e) |
||
| 61 | logging.error(traceback.print_exc()) |
||
| 62 | raise (e) |
||
| 63 | |||
| 64 | |||
| 65 | def insert_city_dataframe(session, city_df): |
||
| 66 | city_df.columns = ['city_post_code', 'city_name'] |
||
| 67 | try: |
||
| 68 | for index, city_data in city_df.iterrows(): |
||
| 69 | get_or_create(session, City, city_post_code=city_data['city_post_code'], |
||
| 70 | city_name=address.clean_city(city_data['city_name'])) |
||
| 71 | except Exception as e: |
||
| 72 | |||
| 73 | logging.error('Rolled back: %s.', e) |
||
| 74 | logging.error(city_data) |
||
| 75 | logging.error(traceback.print_exc()) |
||
| 76 | session.rollback() |
||
| 77 | else: |
||
| 78 | logging.info('Successfully added %s city items to the dataset.', len(city_df)) |
||
| 79 | session.commit() |
||
| 80 | |||
| 81 | |||
| 82 | def insert_street_type_dataframe(session, city_df): |
||
| 83 | city_df.columns = ['street_type'] |
||
| 84 | try: |
||
| 85 | for index, city_data in city_df.iterrows(): |
||
| 86 | get_or_create(session, Street_type, street_type=city_data['street_type']) |
||
| 87 | except Exception as e: |
||
| 88 | logging.error('Rolled back: %s.', e) |
||
| 89 | logging.error(city_data) |
||
| 90 | logging.error(traceback.print_exc()) |
||
| 91 | session.rollback() |
||
| 92 | else: |
||
| 93 | logging.info('Successfully added %s street type items to the dataset.', len(city_df)) |
||
| 94 | session.commit() |
||
| 95 | |||
| 96 | |||
| 97 | def insert_common_dataframe(session, common_df): |
||
| 98 | common_df.columns = ['poi_name', 'poi_tags', 'poi_url_base', 'poi_code'] |
||
| 99 | try: |
||
| 100 | for index, poi_common_data in common_df.iterrows(): |
||
| 101 | get_or_create(session, POI_common, **poi_common_data) |
||
| 102 | except Exception as e: |
||
| 103 | logging.error('Rolled back: %s.', e) |
||
| 104 | logging.error(poi_common_data) |
||
| 105 | logging.error(traceback.print_exc()) |
||
| 106 | session.rollback() |
||
| 107 | else: |
||
| 108 | logging.info('Successfully added %s common items to the dataset.', len(common_df)) |
||
| 109 | session.commit() |
||
| 110 | |||
| 111 | |||
| 112 | def search_for_postcode(session, city_name): |
||
| 113 | city_col = session.query(City.city_post_code).filter(City.city_name == city_name).all() |
||
| 114 | if len(city_col) == 1: |
||
| 115 | return city_col |
||
| 116 | else: |
||
| 117 | logging.info('Cannot determine the post code from city name (%s).', city_name) |
||
| 118 | return None |
||
| 119 | |||
| 120 | |||
| 121 | def insert_poi_dataframe(session, poi_df): |
||
| 122 | poi_df.columns = POI_COLS |
||
| 123 | poi_df[['poi_postcode']] = poi_df[['poi_postcode']].fillna('0000') |
||
| 124 | poi_df[['poi_postcode']] = poi_df[['poi_postcode']].astype(int) |
||
| 125 | poi_dict = poi_df.to_dict('records') |
||
| 126 | try: |
||
| 127 | for poi_data in poi_dict: |
||
| 128 | city_col = session.query(City.city_id).filter(City.city_name == poi_data['poi_city']).filter( |
||
| 129 | City.city_post_code == poi_data['poi_postcode']).first() |
||
| 130 | common_col = session.query(POI_common.pc_id).filter(POI_common.poi_code == poi_data['poi_code']).first() |
||
| 131 | poi_data['poi_addr_city'] = city_col |
||
| 132 | poi_data['poi_common_id'] = common_col |
||
| 133 | if 'poi_name' in poi_data: del poi_data['poi_name'] |
||
| 134 | if 'poi_code' in poi_data: del poi_data['poi_code'] |
||
| 135 | get_or_create_poi(session, POI_address, **poi_data) |
||
| 136 | except Exception as e: |
||
| 137 | logging.error('Rolled back: %s.', e) |
||
| 138 | logging.error(poi_data) |
||
| 139 | logging.error(traceback.print_exc()) |
||
| 140 | session.rollback() |
||
| 141 | raise (e) |
||
| 142 | else: |
||
| 143 | try: |
||
| 144 | session.commit() |
||
| 145 | logging.info('Successfully added %s POI items to the dataset.', len(poi_dict)) |
||
| 146 | except Exception as e: |
||
| 147 | logging.error('Unsuccessfull commit: %s.', e) |
||
| 148 | logging.error(traceback.print_exc()) |
||
| 149 | |||
| 150 | |||
| 151 | def insert_type(session, type_data): |
||
| 152 | try: |
||
| 153 | for i in type_data: |
||
| 154 | get_or_create(session, POI_common, **i) |
||
| 155 | except Exception as e: |
||
| 156 | logging.error('Rolled back: %s.', e) |
||
| 157 | logging.error(i) |
||
| 158 | logging.error(traceback.print_exc()) |
||
| 159 | session.rollback() |
||
| 160 | else: |
||
| 161 | logging.info('Successfully added %s type items to the dataset.', len(type_data)) |
||
| 162 | session.commit() |
||
| 163 | |||
| 164 | |||
| 165 | def insert(session, **kwargs): |
||
| 166 | try: |
||
| 167 | city_col = session.query(City.city_id).filter(City.city_name == kwargs['poi_city']).filter( |
||
| 168 | City.city_post_code == kwargs['poi_postcode']).first() |
||
| 169 | common_col = session.query(POI_common.pc_id).filter(POI_common.poi_code == kwargs['poi_code']).first() |
||
| 170 | kwargs['poi_addr_city'] = city_col |
||
| 171 | kwargs['poi_common_id'] = common_col |
||
| 172 | kwargs['poi_hash'] = hashlib.sha512( |
||
| 173 | '{}{}{}{}{}{}'.format(kwargs['poi_code'], kwargs['poi_postcode'], kwargs['poi_city'], |
||
| 174 | kwargs['poi_addr_street'], kwargs['poi_addr_housenumber'], |
||
| 175 | kwargs['poi_conscriptionnumber']).lower().replace(' ', '').encode( |
||
| 176 | 'utf-8')).hexdigest() |
||
| 177 | if 'poi_name' in kwargs: del kwargs['poi_name'] |
||
| 178 | if 'poi_code' in kwargs: del kwargs['poi_code'] |
||
| 179 | get_or_create(session, POI_address, **kwargs) |
||
| 180 | except Exception as e: |
||
| 181 | logging.error('Rolled back: %s.', e) |
||
| 182 | logging.error(kwargs) |
||
| 183 | logging.error(traceback.print_exc()) |
||
| 184 | session.rollback() |
||
| 185 | else: |
||
| 186 | logging.debug('Successfully added the item to the dataset.') |
||
| 187 | session.commit() |
||
| 188 | finally: |
||
| 189 | session.close() |
||
| 190 |