From 56290b513063432b4ddda2cea84070a26b2f40cd Mon Sep 17 00:00:00 2001 From: Brandon Kuczenski Date: Thu, 15 Aug 2019 13:26:35 -0700 Subject: [PATCH 1/2] Catch attribute errors to detect empty dicts in staddress and stnumber --- geocoder/geolytica.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/geocoder/geolytica.py b/geocoder/geolytica.py index 7822090a..11278ec9 100644 --- a/geocoder/geolytica.py +++ b/geocoder/geolytica.py @@ -34,11 +34,17 @@ def postal(self): @property def housenumber(self): - return self._standard.get('stnumber', '').strip() + try: + return self._standard.get('stnumber', '').strip() + except AttributeError: + return '' @property def street(self): - return self._standard.get('staddress', '').strip() + try: + return self._standard.get('staddress', '').strip() + except AttributeError: + return '' @property def city(self): From 5746295ccf82f65aef5a24d9846bc5ea6bad0449 Mon Sep 17 00:00:00 2001 From: Brandon Kuczenski Date: Thu, 26 Aug 2021 16:24:33 -0700 Subject: [PATCH 2/2] Generalize geolytica workaround to all builtin properties --- geocoder/geolytica.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/geocoder/geolytica.py b/geocoder/geolytica.py index 11278ec9..b2b2df75 100644 --- a/geocoder/geolytica.py +++ b/geocoder/geolytica.py @@ -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): @@ -18,41 +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): - try: - return self._standard.get('stnumber', '').strip() - except AttributeError: - return '' + return _correct_empty_dict(self._standard, 'stnumber') @property def street(self): - try: - return self._standard.get('staddress', '').strip() - except AttributeError: - return '' + 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):