Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch attribute errors to detect empty dicts in staddress and stnumber #396

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions geocoder/geolytica.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
from geocoder.base import OneResult, MultipleResultsQuery


def _correct_empty_dict(obj, key, alt=''):
try:
k = obj.get(key, alt).strip()
except AttributeError:
k = alt
return k


class GeolyticaResult(OneResult):

def __init__(self, json_content):
Expand All @@ -18,35 +26,35 @@ def __init__(self, json_content):

@property
def lat(self):
lat = self.raw.get('latt', '').strip()
lat = _correct_empty_dict(self.raw, 'latt')
if lat:
return float(lat)

@property
def lng(self):
lng = self.raw.get('longt', '').strip()
lng = _correct_empty_dict(self.raw, 'longt')
if lng:
return float(lng)

@property
def postal(self):
return self.raw.get('postal', '').strip()
return _correct_empty_dict(self.raw, 'postal')

@property
def housenumber(self):
return self._standard.get('stnumber', '').strip()
return _correct_empty_dict(self._standard, 'stnumber')

@property
def street(self):
return self._standard.get('staddress', '').strip()
return _correct_empty_dict(self._standard, 'staddress')

@property
def city(self):
return self._standard.get('city', '').strip()
return _correct_empty_dict(self._standard, 'city')

@property
def state(self):
return self._standard.get('prov', '').strip()
return _correct_empty_dict(self._standard, 'prov')

@property
def address(self):
Expand Down