| Conditions | 14 |
| Total Lines | 58 |
| Code Lines | 36 |
| 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 elodie.geolocation.place_name() 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 | """Look up geolocation information for media objects.""" |
||
| 144 | def place_name(lat, lon): |
||
| 145 | lookup_place_name_default = {'default': __DEFAULT_LOCATION__} |
||
| 146 | if(lat is None or lon is None): |
||
| 147 | return lookup_place_name_default |
||
| 148 | |||
| 149 | # Convert lat/lon to floats |
||
| 150 | if(not isinstance(lat, float)): |
||
| 151 | lat = float(lat) |
||
| 152 | if(not isinstance(lon, float)): |
||
| 153 | lon = float(lon) |
||
| 154 | |||
| 155 | localdb_response = closestgeoname.query_closest_city(path.join(getcwd(),'elodie', 'closestgeoname','geonames.sqlite'), lat, lon) |
||
| 156 | |||
| 157 | if localdb_response is not None: |
||
| 158 | return {"city": localdb_response[0], |
||
| 159 | "default": localdb_response[0], |
||
| 160 | "state": localdb_response[1], |
||
| 161 | "country": localdb_response[2]} |
||
| 162 | |||
| 163 | else: |
||
| 164 | # This should only occur if the script is searching for a point |
||
| 165 | # outside the maximum distance on earth. So this would mean |
||
| 166 | # there is likely an error with the GNSS coordinate. But we can |
||
| 167 | # continue to see if other services (MapQuest) has a way to handle it. |
||
| 168 | print("lat/long likely to be incorrect... continuing with other service") |
||
| 169 | print("lat: {}, lon: {}".format(lat, lon)) |
||
| 170 | pass |
||
| 171 | |||
| 172 | # Try to get cached location first |
||
| 173 | db = Db() |
||
| 174 | # 3km distace radious for a match |
||
| 175 | cached_place_name = db.get_location_name(lat, lon, 3000) |
||
| 176 | # We check that it's a dict to coerce an upgrade of the location |
||
| 177 | # db from a string location to a dictionary. See gh-160. |
||
| 178 | if(isinstance(cached_place_name, dict)): |
||
| 179 | return cached_place_name |
||
| 180 | |||
| 181 | lookup_place_name = {} |
||
| 182 | geolocation_info = lookup(lat=lat, lon=lon) |
||
| 183 | if(geolocation_info is not None and 'address' in geolocation_info): |
||
| 184 | address = geolocation_info['address'] |
||
| 185 | for loc in ['city', 'state', 'country']: |
||
| 186 | if(loc in address): |
||
| 187 | lookup_place_name[loc] = address[loc] |
||
| 188 | # In many cases the desired key is not available so we |
||
| 189 | # set the most specific as the default. |
||
| 190 | if('default' not in lookup_place_name): |
||
| 191 | lookup_place_name['default'] = address[loc] |
||
| 192 | |||
| 193 | if(lookup_place_name): |
||
| 194 | db.add_location(lat, lon, lookup_place_name) |
||
| 195 | # TODO: Maybe this should only be done on exit and not for every write. |
||
| 196 | db.update_location_db() |
||
| 197 | |||
| 198 | if('default' not in lookup_place_name): |
||
| 199 | lookup_place_name = lookup_place_name_default |
||
| 200 | |||
| 201 | return lookup_place_name |
||
| 202 | |||
| 259 |