From ed113078d2e782acd5aa41050581e5dbffd1f274 Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 12 Apr 2010 23:46:42 +0100 Subject: [PATCH] Closer to pep8 --- events.py | 16 ++++++++-------- locator.py | 6 ++++-- resultsparser.py | 46 +++++++++++++++++++++++++++------------------- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/events.py b/events.py index e6e4f2c..6f8d7b9 100644 --- a/events.py +++ b/events.py @@ -10,12 +10,12 @@ class Events: self.format = 'json' self.method = 'geo.getevents' - def get_events(self, lat, long, distance): + def get_events(self, lat, lng, distance): """ Retrieve xml and parse into events list """ - xml = self.get_xml(lat, long, distance) + xml = self.get_xml(lat, lng, distance) events = parse_xml(xml, lat, - long) + lng) return self.sort_events(events) def sort_events(self, events): @@ -24,15 +24,15 @@ class Events: events.sort(cmp=self.distance_cmp, key=lambda x: x['distance']) return events - def get_json(self, lat='', long='', distance=''): + def get_json(self, lat='', lng='', distance=''): # testing json results lat = '51.5174' - long = '-0.0829' + lng = '-0.0829' distance = '10' params = urllib.urlencode({'method': self.method, 'api_key': self.api_key, 'distance': distance, - 'long': long, + 'long': lng, 'lat': lat, 'format': self.format}) url = '%s?%s' % (self.url_base, params) @@ -40,12 +40,12 @@ class Events: response = urllib2.urlopen(request) return response - def get_xml(self, lat, long, distance): + def get_xml(self, lat, lng, distance): """ Return xml from lastfm """ params = urllib.urlencode({'method': self.method, 'api_key': self.api_key, 'distance': distance, - 'long': long, + 'long': lng, 'lat': lat}) response = urllib.urlopen(self.url_base, params) return response.read() diff --git a/locator.py b/locator.py index ab3db53..e562e8d 100644 --- a/locator.py +++ b/locator.py @@ -10,8 +10,10 @@ class LocationUpdater: self.fix_count = 0 self.control = location.GPSDControl.get_default() - self.control.set_properties(preferred_method=location.METHOD_USER_SELECTED, - preferred_interval=location.INTERVAL_DEFAULT) + self.control.set_properties(preferred_method=location\ + .METHOD_USER_SELECTED, + preferred_interval=location\ + .INTERVAL_DEFAULT) self.control.connect("error-verbose", self.on_error, self.loop) self.control.connect("gpsd-stopped", self.on_stop, self.loop) diff --git a/resultsparser.py b/resultsparser.py index 7d2835a..793cb4c 100644 --- a/resultsparser.py +++ b/resultsparser.py @@ -4,11 +4,11 @@ import time import location -def parse_json(json, lat, long): +def parse_json(json, lat, lng): """ Parse json into usable format """ pass -def parse_xml(xml, lat, long): +def parse_xml(xml, lat, lng): """ Parse xml into a dict """ events_list = [] today = date.today() @@ -16,7 +16,8 @@ def parse_xml(xml, lat, long): events = dom.getElementsByTagName('event') for event in events: - start_date = parse_date(event.getElementsByTagName('startDate')[0].childNodes[0].data) + start_date = parse_date(event.getElementsByTagName('startDate')[0]\ + .childNodes[0].data) if start_date.date() == today: title = event.getElementsByTagName('title')[0].childNodes[0].data @@ -26,14 +27,17 @@ def parse_xml(xml, lat, long): artist_list.append(artist.childNodes[0].data) artists = ', '.join(artist_list) - venue_details = event.getElementsByTagName('venue')[0] - venue_name = venue_details.getElementsByTagName('name')[0].childNodes[0].data - address = get_address(venue_details.getElementsByTagName('location')[0]) - geo_data = venue_details.getElementsByTagName('geo:point')[0] - venue_lat = geo_data.getElementsByTagName('geo:lat')[0].childNodes[0].data - venue_long = geo_data.getElementsByTagName('geo:long')[0].childNodes[0].data + v_details = event.getElementsByTagName('venue')[0] + venue_name = v_details.getElementsByTagName('name')[0]\ + .childNodes[0].data + address = get_address(v_details.getElementsByTagName('location')[0]) + geo_data = v_details.getElementsByTagName('geo:point')[0] + venue_lat = geo_data.getElementsByTagName('geo:lat')[0]\ + .childNodes[0].data + venue_long = geo_data.getElementsByTagName('geo:long')[0]\ + .childNodes[0].data distance = location.distance_between(float(lat), - float(long), + float(lng), float(venue_lat), float(venue_long)) @@ -45,20 +49,24 @@ def parse_xml(xml, lat, long): 'date': start_date}) return events_list -def get_address(location): +def get_address(location_element): """ Return the venues address details from the xml element """ street = '' city = '' country = '' postalcode = '' - if location.getElementsByTagName('street')[0].childNodes: - street = location.getElementsByTagName('street')[0].childNodes[0].data - if location.getElementsByTagName('city')[0].childNodes: - city = location.getElementsByTagName('city')[0].childNodes[0].data - if location.getElementsByTagName('country')[0].childNodes: - country = location.getElementsByTagName('country')[0].childNodes[0].data - if location.getElementsByTagName('postalcode')[0].childNodes: - postalcode = location.getElementsByTagName('postalcode')[0].childNodes[0].data + if location_element.getElementsByTagName('street')[0].childNodes: + street = location_element.getElementsByTagName('street')[0]\ + .childNodes[0].data + if location_element.getElementsByTagName('city')[0].childNodes: + city = location_element.getElementsByTagName('city')[0]\ + .childNodes[0].data + if location_element.getElementsByTagName('country')[0].childNodes: + country = location_element.getElementsByTagName('country')[0]\ + .childNodes[0].data + if location_element.getElementsByTagName('postalcode')[0].childNodes: + postalcode = location_element.getElementsByTagName('postalcode')[0]\ + .childNodes[0].data return '\n'.join([street, city, country, postalcode]) def parse_date(date_string): -- 1.7.9.5