diff --git a/README.md b/README.md index 764ebf9..80d14e2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# data-gc-ca-api 0.2.4 README +# data-gc-ca-api 2.0.0 README ## Introduction data-gc-ca-api: a simple python api for the Canada Open Data Portal @@ -15,19 +15,7 @@ Environment Canada provides a [description](http://goo.gl/XD7w4) of what can be accessed but it's far easier to look at an example [city XML](http://goo.gl/vyL7r). -## Installation - -This package is available from the [Python Package Index](http://pypi.python.org). It -can be easily installed using pip as follows - - $ pip install data-gc-ca-api - -Tarballs of are available from the [project website](https://github.com/igable/data-gc-ca-api). -To install from a tarball: - - $ tar xzvf data-gc-ca-api-X.Y.Z.tar.gz - $ cd data-gc-ca-api-X.Y.Z - $ python setup.py install +Updated to work with Python 3. ## Command line tool: weatherca diff --git a/data_gc_ca_api/cityweather.py b/data_gc_ca_api/cityweather.py index a565bc5..98312c4 100755 --- a/data_gc_ca_api/cityweather.py +++ b/data_gc_ca_api/cityweather.py @@ -6,7 +6,7 @@ ## Auth.: Ian Gable -import urllib +import urllib.request import sys @@ -29,7 +29,7 @@ def __init__(self): cityListTree = ElementTree() try: - urlhandle = urllib.urlopen(self.city_list_url) + urlhandle = urllib.request.urlopen(self.city_list_url) except IOError: raise IOError("Unable to open the data url: " + self.city_list_url) @@ -37,13 +37,12 @@ def __init__(self): siteList = cityListTree.findall("site") for site in siteList: - cityNameEnglish = site.findtext("nameEn").encode('utf-8') - + cityNameEnglish = site.findtext("nameEn") + city_name_english=cityNameEnglish.encode('utf-8') self.cities[cityNameEnglish] = {\ - 'sitecode': site.attrib['code'].encode('utf-8'),\ - 'provincecode': site.findtext("provinceCode").encode('utf-8'),\ - 'nameFr': site.findtext("nameFr").encode('utf-8') } - + 'sitecode': site.attrib['code'],\ + 'provincecode': site.findtext("provinceCode"),\ + 'nameFr': site.findtext("nameFr")} def is_city(self, name): """ @@ -55,9 +54,7 @@ def data_url(self,name): """ Returns resource URL for the city denoted by name """ - if self.is_city(name): - return self.base_url + self.province(name) + "/" + self.site_code(name) + "_e.xml" - return None + return self.base_url + self.province(name) + "/" + self.site_code(name) + "_e.xml" def province(self,name): """ @@ -97,9 +94,9 @@ def __init__(self, dataurl): self.tree = ElementTree() try: - urlhandle = urllib.urlopen(dataurl) + urlhandle = urllib.request.urlopen(dataurl) except IOError: - print "[Error] Unable to open the data url: " + dataurl + print("[Error] Unable to open the data url: " + dataurl) sys.exit(1) self.tree.parse(urlhandle) @@ -143,7 +140,7 @@ def _get_all_xpaths(self, pathlist, path, element): def _make_attribute_list(self, attrib): xpathattrib = "" - for attribute, value in attrib.iteritems(): + for attribute, value in attrib.items(): xpathattrib = xpathattrib + "[@" + attribute + "='" + value + "']" return xpathattrib diff --git a/weatherca b/weatherca index 76ee15b..e62658d 100755 --- a/weatherca +++ b/weatherca @@ -11,7 +11,7 @@ import sys import difflib from optparse import OptionParser from difflib import SequenceMatcher -from data_gc_ca_api.cityweather import City, CityIndex +from data_gc_ca_api.cityweather import City, CityIndex from data_gc_ca_api.__version__ import version @@ -45,8 +45,6 @@ def get_best_guess(city, cityList): return None - - def main(): @@ -81,12 +79,11 @@ def main(): sys.exit(1) if options.list: for cityname in cityindex.english_city_list(): - print cityname + print(str(cityname)) sys.exit(1) - if not options.city: - print "No City given, using, Victoria, BC (the best city)." + print("No city specified, using default: " + (city)) else: city = options.city @@ -97,25 +94,22 @@ def main(): if options.quantity: quantity = options.quantity - - if cityindex.is_city(city): cityObject = City(cityindex.data_url(city)) if options.list_quantities: for q in cityObject.get_available_quantities(): - print q + print(q) else: if not options.quantity: - print "No requested quantity given, using temperature" - print quantity + " is: " + cityObject.get_quantity(quantity) + print("No requested quantity given, using temperature") + print(quantity + " is: " + cityObject.get_quantity(quantity)) else: - print city + " is not available" + print(city + " is not available") bestGuess = get_best_guess(city,cityindex.english_city_list()) if bestGuess: - print "Did you mean: " + bestGuess + print("Did you mean: " + bestGuess) else: - print "Do 'weatherca --list' to get a list of cities" - + print("Do 'weatherca --list' to get a list of cities") if __name__ == "__main__": main()