From f5f8f466658bb0324547c1ccda0deefc77aa600b Mon Sep 17 00:00:00 2001 From: Lukas Drude Date: Sat, 10 Oct 2015 18:25:14 -0400 Subject: [PATCH 1/2] 2to3 conversion --- bin/noaa | 24 ++++++++++++------------ noaa/forecast.py | 2 +- noaa/geocode.py | 5 ++++- noaa/observation.py | 4 ++-- noaa/utils.py | 22 +++++++++++----------- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/bin/noaa b/bin/noaa index d381688..920ebd6 100755 --- a/bin/noaa +++ b/bin/noaa @@ -25,7 +25,7 @@ Database (NDFD). """ import argparse -import ConfigParser +import configparser import os import sys @@ -101,23 +101,23 @@ def format_conditions(conditions, padding=30, color=True): def config_get_boolean(config, section, option, default=None): try: value = config.getboolean(section, option) - except ConfigParser.NoSectionError: + except configparser.NoSectionError: value = default - except ConfigParser.NoOptionError: + except configparser.NoOptionError: value = default return value def make_parser(): - config = ConfigParser.ConfigParser() + config = configparser.ConfigParser() config.read(os.path.expanduser("~/.noaarc")) try: default_location = config.get('default', 'location').split() - except ConfigParser.NoSectionError: + except configparser.NoSectionError: default_location = None - except ConfigParser.NoOptionError: + except configparser.NoOptionError: default_location = None default_metric = config_get_boolean( @@ -220,14 +220,14 @@ def main(): pretty_location, fcast = forecast_func(args) if args.heading: - print "Forecast for %s" % pretty_location + print("Forecast for %s" % pretty_location) for datapoint in fcast: - print datapoint.date.strftime('%a'), - print format_conditions(datapoint.conditions, color=args.color), - print format_temp(datapoint.min_temp, color=args.color), - print format_temp(datapoint.max_temp, color=args.color), - print simple_temp_graph(datapoint.max_temp, color=args.color) + print(datapoint.date.strftime('%a'), end=' ') + print(format_conditions(datapoint.conditions, color=args.color), end=' ') + print(format_temp(datapoint.min_temp, color=args.color), end=' ') + print(format_temp(datapoint.max_temp, color=args.color), end=' ') + print(simple_temp_graph(datapoint.max_temp, color=args.color)) if __name__ == "__main__": diff --git a/noaa/forecast.py b/noaa/forecast.py index ad2a9ba..c3753ad 100644 --- a/noaa/forecast.py +++ b/noaa/forecast.py @@ -74,7 +74,7 @@ def _parse_time_layouts(tree): dt = parse_dt(tl_child.text) end_times.append(dt) - time_layouts[key] = zip(start_times, end_times) + time_layouts[key] = list(zip(start_times, end_times)) return time_layouts diff --git a/noaa/geocode.py b/noaa/geocode.py index e2a0c33..70ef270 100644 --- a/noaa/geocode.py +++ b/noaa/geocode.py @@ -9,6 +9,9 @@ def geocode_location(location, api_key=None): """Use Google to geocode a location string. For high-volume traffic, you will need to specify an API-key. + + TODO: This does not work anymore. Switch to: + https://developers.google.com/maps/documentation/geocoding/intro """ GEOCODE_URL = "http://maps.google.com/maps/geo" params = [('q', location), @@ -19,7 +22,7 @@ def geocode_location(location, api_key=None): params += [('key', api_key)] resp = utils.open_url(GEOCODE_URL, params) - data = json.loads(resp.read()) + data = json.loads(resp.read().decode()) if data['Status']['code'] != 200: raise exceptions.GeocodeException('Unable to geocode this location') diff --git a/noaa/observation.py b/noaa/observation.py index 63a259d..d3b473c 100644 --- a/noaa/observation.py +++ b/noaa/observation.py @@ -28,11 +28,11 @@ def compiled_observation_for_lat_lon(lat, lon, stations, radius=10.0, if not compiled_observation: compiled_observation = copy.copy(station_observation.observation) - if not noaa.utils.any_none(compiled_observation.__dict__.values()): + if not noaa.utils.any_none(list(compiled_observation.__dict__.values())): # If all the values are filled in, break out of loop break - for attr, compiled_value in compiled_observation.__dict__.items(): + for attr, compiled_value in list(compiled_observation.__dict__.items()): if compiled_value is None: station_value = getattr(station_observation.observation, attr) setattr(compiled_observation, attr, station_value) diff --git a/noaa/utils.py b/noaa/utils.py index 62dca24..4462edd 100644 --- a/noaa/utils.py +++ b/noaa/utils.py @@ -1,7 +1,7 @@ import contextlib import math import sys -import urllib +import urllib.request, urllib.parse, urllib.error from xml.etree import ElementTree as ET import dateutil.parser @@ -25,12 +25,12 @@ def colorize(text, color): def any_none(L): - return any(map(lambda x: x is None, L)) + return any([x is None for x in L]) def all_numbers(L): try: - map(float, L) + list(map(float, L)) return True except ValueError: return False @@ -39,10 +39,10 @@ def all_numbers(L): def print_tree(tree, indent=4): """Print an ElementTree for debugging purposes.""" def print_elem(elem, level): - print " " * (indent * level), - print 'tag="%s"' % elem.tag, - print 'text="%s"' % elem.text.strip() if elem.text is not None else "", - print 'attrib=%s' % elem.attrib + print(" " * (indent * level), end=' ') + print('tag="%s"' % elem.tag, end=' ') + print('text="%s"' % elem.text.strip() if elem.text is not None else "", end=' ') + print('attrib=%s' % elem.attrib) for child in elem.getchildren(): print_elem(child, level + 1) print_elem(tree.getroot(), 0) @@ -50,10 +50,10 @@ def print_elem(elem, level): def open_url(url, params=None): if params: - query_string = urllib.urlencode(params) + query_string = urllib.parse.urlencode(params) url = "?".join([url, query_string]) - resp = urllib.urlopen(url) + resp = urllib.request.urlopen(url) return resp @@ -72,7 +72,7 @@ def die_on(*exception_classes, **kwargs): try: yield except exception_classes as e: - print >> sys.stderr, msg_func(e) + print(msg_func(e), file=sys.stderr) sys.exit(exit_code) @@ -96,7 +96,7 @@ def hsin(theta): return math.sin(float(theta) / 2) ** 2 if angle_units == "deg": - lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2]) + lat1, lon1, lat2, lon2 = list(map(radians, [lat1, lon1, lat2, lon2])) elif angle_units == "rad": pass else: From fb7ca7d45be03173d6a63a04474999e8fb3aaca9 Mon Sep 17 00:00:00 2001 From: Lukas Drude Date: Sat, 10 Oct 2015 19:25:47 -0400 Subject: [PATCH 2/2] Changed geocode to new Google service (old was deprecated) --- noaa/geocode.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/noaa/geocode.py b/noaa/geocode.py index 70ef270..3aa745e 100644 --- a/noaa/geocode.py +++ b/noaa/geocode.py @@ -9,14 +9,9 @@ def geocode_location(location, api_key=None): """Use Google to geocode a location string. For high-volume traffic, you will need to specify an API-key. - - TODO: This does not work anymore. Switch to: - https://developers.google.com/maps/documentation/geocoding/intro """ - GEOCODE_URL = "http://maps.google.com/maps/geo" - params = [('q', location), - ('sensor', 'false'), - ('output', 'json')] + GEOCODE_URL = "http://maps.googleapis.com/maps/api/geocode/json" + params = [('address', location)] if api_key: params += [('key', api_key)] @@ -24,12 +19,12 @@ def geocode_location(location, api_key=None): resp = utils.open_url(GEOCODE_URL, params) data = json.loads(resp.read().decode()) - if data['Status']['code'] != 200: + if data['status'] != 'OK': raise exceptions.GeocodeException('Unable to geocode this location') - best_match = data['Placemark'][0] - address = best_match['address'] - lon, lat, _ = best_match['Point']['coordinates'] + address = data['results'][0]['formatted_address'] + lon = data['results'][0]['geometry']['location']['lng'] + lat = data['results'][0]['geometry']['location']['lat'] location = models.Location(lat, lon, address) return location