Skip to content

Commit 26ec92a

Browse files
committed
refactoring
1 parent 478da83 commit 26ec92a

5 files changed

Lines changed: 46 additions & 49 deletions

File tree

geolocation/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class BaseApi(object):
1010

11-
log = logging.Logger('google_api')
11+
log = logging.getLogger('google_api')
1212

1313
def __init__(self, api_key):
1414
self.api_key = "&key=%s" % api_key

geolocation/google_maps.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
class GoogleMaps(object):
1111
"""To find address use: GoogleMaps.search(location=full_address)."""
12-
geocode = GeocodeParser()
12+
geocode_parser = GeocodeParser()
1313
data = set()
1414

15-
log = logging.Logger('google_maps')
15+
log = logging.getLogger('google_maps')
1616

1717
def __init__(self, api_key):
18-
self._geocode_api = GeocodeApi(api_key)
18+
self.geocode_api = GeocodeApi(api_key)
1919
self.clear()
2020

2121
def __repr__(self):
@@ -32,33 +32,31 @@ def validate(location):
3232

3333
return True
3434

35-
def _to_python(self, json_results):
35+
def to_python(self, json_results):
3636
"""Method should converts json_results to python object."""
3737
for item in json_results:
38-
self.geocode.json_data = item
38+
self.geocode_parser.json_data = item
3939

4040
location = LocationModel()
4141

42-
location.city = self.geocode.get_city()
43-
location.route = self.geocode.get_route()
44-
location.street_number = self.geocode.get_street_number()
45-
location.postal_code = self.geocode.get_postal_code()
42+
location.city = self.geocode_parser.get_city()
43+
location.route = self.geocode_parser.get_route()
44+
location.street_number = self.geocode_parser.get_street_number()
45+
location.postal_code = self.geocode_parser.get_postal_code()
4646

47-
location.country = self.geocode.get_country()
48-
location.country_shortcut = self.geocode.get_country_shortcut()
47+
location.country = self.geocode_parser.get_country()
48+
location.country_shortcut = self.geocode_parser.get_country_shortcut()
4949

50-
location.administrative_area = self.geocode.get_administrative_area()
50+
location.administrative_area = self.geocode_parser.get_administrative_area()
5151

52-
location.lat = self.geocode.get_lat()
53-
location.lng = self.geocode.get_lng()
52+
location.lat = self.geocode_parser.get_lat()
53+
location.lng = self.geocode_parser.get_lng()
5454

55-
location.formatted_address = self.geocode.get_formatted_address()
55+
location.formatted_address = self.geocode_parser.get_formatted_address()
5656

5757
if self.validate(location):
5858
self.data.add(location)
5959

60-
return self.all()
61-
6260
def all(self):
6361
"""Method returns location list."""
6462
return list(self.data)
@@ -70,10 +68,10 @@ def first(self):
7068
return None
7169

7270
def search(self, location=None, lat=None, lng=None):
73-
json_results = self._geocode_api.query(location=location, lat=lat, lng=lng)
71+
json_results = self.geocode_api.query(location=location, lat=lat, lng=lng)
7472

7573
if json_results:
76-
self._to_python(json_results)
74+
self.to_python(json_results)
7775

7876
return self
7977

geolocation/models.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ class LocationModel(object):
55
_administrative_area = list()
66

77
def __init__(self, **kwargs):
8-
self.city = kwargs.get('city', None)
9-
self.route = kwargs.get('route', None)
10-
self.street_number = kwargs.get('street_number', None)
11-
self.country = kwargs.get('country', None)
12-
self.country_shortcut = kwargs.get('country_shortcut', None)
13-
self.postal_code = kwargs.get('postal_code', None)
14-
self.lat = kwargs.get('lat', None)
15-
self.lng = kwargs.get('lng', None)
16-
self.formatted_address = kwargs.get('formatted_address', None)
8+
self.city = kwargs.get('city')
9+
self.route = kwargs.get('route')
10+
self.street_number = kwargs.get('street_number')
11+
self.country = kwargs.get('country')
12+
self.country_shortcut = kwargs.get('country_shortcut')
13+
self.postal_code = kwargs.get('postal_code')
14+
self.lat = kwargs.get('lat')
15+
self.lng = kwargs.get('lng')
16+
self.formatted_address = kwargs.get('formatted_address')
1717

1818
def __repr__(self):
1919
return '<LocationModel: %s>' % self.city
@@ -25,7 +25,7 @@ def administrative_area(self):
2525
@administrative_area.setter
2626
def administrative_area(self, value_list):
2727
for value in value_list:
28-
area = AdministrativeAreaLevelModel(value['area_type'], value['name'])
28+
area = AdministrativeAreaLevelModel(value.get('area_type'), value.get('name'))
2929
self._administrative_area.append(area)
3030

3131

geolocation/parsers.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,22 @@ def set_json_data(self, value):
1616
"""Method sets value to json_data"""
1717
self.json_data = value
1818

19-
def _search_address_components(self, type_, shortcut=False):
19+
def search_address_components(self, type_, shortcut=False):
2020
"""Method searches address components in google maps json data."""
21-
if not self.json_data:
22-
return None
21+
data = None
2322

24-
for address_component in self.json_data['address_components']:
25-
types = address_component['types']
23+
for address_component in self.json_data.get('address_components'):
24+
types = address_component.get('types')
2625

2726
if type_ in types:
2827
if shortcut:
29-
return address_component['short_name'].encode('utf-8')
28+
data = address_component.get('short_name').encode('utf-8')
3029
else:
31-
return address_component['long_name'].encode('utf-8')
30+
data = address_component.get('long_name').encode('utf-8')
3231

33-
return None
32+
return data
3433

35-
def _search_geometry_location(self, type_):
34+
def search_geometry_location(self, type_):
3635
"""Method searches geometry location in google maps json data"""
3736
if not self.json_data:
3837
return None
@@ -50,35 +49,35 @@ def get_formatted_address(self):
5049

5150
def get_street_number(self):
5251
"""Method should returns street number of current location."""
53-
return self._search_address_components('street_number')
52+
return self.search_address_components('street_number')
5453

5554
def get_route(self):
5655
"""Method should returns route long name of current location."""
57-
return self._search_address_components('route')
56+
return self.search_address_components('route')
5857

5958
def get_postal_code(self):
6059
"""Method should returns postal code of current location."""
61-
return self._search_address_components('postal_code')
60+
return self.search_address_components('postal_code')
6261

6362
def get_city(self):
6463
"""Method should returns city long name of current location."""
65-
return self._search_address_components('locality')
64+
return self.search_address_components('locality')
6665

6766
def get_country(self):
6867
"""Method should returns country long name from current location."""
69-
return self._search_address_components('country')
68+
return self.search_address_components('country')
7069

7170
def get_country_shortcut(self):
7271
"""Method should returns country short name from current location."""
73-
return self._search_address_components('country', True)
72+
return self.search_address_components('country', True)
7473

7574
def get_lat(self):
7675
"""Method should returns lat property of current location."""
77-
return self._search_geometry_location('lat')
76+
return self.search_geometry_location('lat')
7877

7978
def get_lng(self):
8079
"""Method should returns lng property of current location."""
81-
return self._search_geometry_location('lng')
80+
return self.search_geometry_location('lng')
8281

8382
def get_administrative_area(self):
8483
"""Method should returns all administrative areas of current location."""
@@ -89,7 +88,7 @@ def get_administrative_area(self):
8988
]
9089

9190
for area_type in administrative_areas:
92-
name = self._search_address_components(area_type)
91+
name = self.search_address_components(area_type)
9392

9493
if name:
9594
data.append(dict(name=name, area_type=area_type))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name='geolocation-python',
12-
version='0.1.2d',
12+
version='0.1.3',
1313
packages=['geolocation'],
1414
url='',
1515
download_url='https://github.com/slawek87/geolocation-python/',

0 commit comments

Comments
 (0)