From ce6cb66fdb82f198d5598850ef8e6447a33cfec0 Mon Sep 17 00:00:00 2001 From: neo-anderson Date: Sun, 26 Apr 2015 21:09:40 -0700 Subject: [PATCH 1/2] Adding support for Python 3 in dataset download scripts --- doc/data/sdss_colors/fetch_data.py | 12 +++++++++--- doc/data/sdss_colors/scatter_colors.py | 5 +++-- doc/data/sdss_photoz/fetch_data.py | 19 +++++++++++++------ doc/data/sdss_spectra/fetch_data.py | 11 +++++++++-- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/doc/data/sdss_colors/fetch_data.py b/doc/data/sdss_colors/fetch_data.py index 8dcfbd9..fcaffe0 100644 --- a/doc/data/sdss_colors/fetch_data.py +++ b/doc/data/sdss_colors/fetch_data.py @@ -1,5 +1,11 @@ import os -import urllib2 +# Supporting Python 2 and 3 +try: + import urllib.request as urllib2 +except ImportError: + import urllib2 +from __future__ import print_function + import numpy as np DTYPE_TRAIN = [('u-g', np.float32), @@ -28,7 +34,7 @@ destination = TRAIN_FILE.rstrip('.dat') + '.npy' if not os.path.exists(destination): url = SDSS_COLORS_URL + TRAIN_FILE - print "downloading data from", url + print("downloading data from", url) fhandle = opener.open(url) np.save(destination, np.loadtxt(opener.open(url), dtype=DTYPE_TRAIN)) @@ -36,7 +42,7 @@ destination = TEST_FILE.rstrip('.dat') + '.npy' if not os.path.exists(destination): url = SDSS_COLORS_URL + TEST_FILE - print "downloading data from", url + print("downloading data from", url) fhandle = opener.open(url) np.save(destination, np.loadtxt(opener.open(url), dtype=DTYPE_TEST)) diff --git a/doc/data/sdss_colors/scatter_colors.py b/doc/data/sdss_colors/scatter_colors.py index 555a94a..149bbe6 100644 --- a/doc/data/sdss_colors/scatter_colors.py +++ b/doc/data/sdss_colors/scatter_colors.py @@ -1,5 +1,6 @@ import numpy as np import pylab as pl +from __future__ import print_function data = np.load('sdssdr6_colors_class_train.npy') @@ -9,8 +10,8 @@ redshift = data['redshift'] -print "%i qsos" % np.sum(redshift > 0) -print "%i stars" % np.sum(redshift == 0) +print("%i qsos" % np.sum(redshift > 0)) +print("%i stars" % np.sum(redshift == 0)) kwargs = dict(s=1, c=(redshift > 0), lw=0) diff --git a/doc/data/sdss_photoz/fetch_data.py b/doc/data/sdss_photoz/fetch_data.py index fe19ea4..d516e69 100644 --- a/doc/data/sdss_photoz/fetch_data.py +++ b/doc/data/sdss_photoz/fetch_data.py @@ -6,7 +6,14 @@ """ import os -import urllib, urllib2 +import urllib +# Supporting Python 2 and 3 +try: + import urllib.request as urllib2 +except ImportError: + import urllib2 +from __future__ import print_function + import numpy as np # Here's how the data can be downloaded directly from the SDSS server. @@ -42,17 +49,17 @@ def sql_query(sql_str, url=URL, format='csv'): if not os.path.exists(archive_file): - print "querying for %i objects" % N - print query_text + print("querying for %i objects" % N) + print(query_text) output = sql_query(query_text) - print "finished. Processing & saving data" + print("finished. Processing & saving data") try: data = np.loadtxt(output, delimiter=',', skiprows=1, dtype=DTYPE) except: raise ValueError(output.read()) np.save(archive_file, data) else: - print "data already on disk" + print("data already on disk") DATA_URL = ('http://www.astro.washington.edu/users/' @@ -67,6 +74,6 @@ def sql_query(sql_str, url=URL, format='csv'): # download training data if not os.path.exists(LOCAL_FILE): - print "downloading data from", DATA_URL + print("downloading data from", DATA_URL) fhandle = opener.open(DATA_URL) open(LOCAL_FILE, 'wb').write(fhandle.read()) diff --git a/doc/data/sdss_spectra/fetch_data.py b/doc/data/sdss_spectra/fetch_data.py index f576019..5a9a82e 100644 --- a/doc/data/sdss_spectra/fetch_data.py +++ b/doc/data/sdss_spectra/fetch_data.py @@ -1,5 +1,12 @@ import os -import urllib2 + +# Supporting Python 2 and 3 +try: + import urllib.request as urllib2 +except ImportError: + import urllib2 +from __future__ import print_function + import numpy as np DATA_URL = ('http://www.astro.washington.edu/users/' @@ -14,6 +21,6 @@ # download training data if not os.path.exists(LOCAL_FILE): - print "downloading data from", DATA_URL + print("downloading data from", DATA_URL) fhandle = opener.open(DATA_URL) open(LOCAL_FILE, 'wb').write(fhandle.read()) From 55cfb4f4cb33da3b7984bec877ca8b323312139f Mon Sep 17 00:00:00 2001 From: neo-anderson Date: Sun, 26 Apr 2015 22:07:33 -0700 Subject: [PATCH 2/2] Import print command from future --- doc/data/sdss_colors/fetch_data.py | 3 ++- doc/data/sdss_colors/scatter_colors.py | 2 +- doc/data/sdss_photoz/fetch_data.py | 3 +-- doc/data/sdss_spectra/fetch_data.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/data/sdss_colors/fetch_data.py b/doc/data/sdss_colors/fetch_data.py index fcaffe0..330ecd9 100644 --- a/doc/data/sdss_colors/fetch_data.py +++ b/doc/data/sdss_colors/fetch_data.py @@ -1,10 +1,11 @@ +from __future__ import print_function + import os # Supporting Python 2 and 3 try: import urllib.request as urllib2 except ImportError: import urllib2 -from __future__ import print_function import numpy as np diff --git a/doc/data/sdss_colors/scatter_colors.py b/doc/data/sdss_colors/scatter_colors.py index 149bbe6..78c142e 100644 --- a/doc/data/sdss_colors/scatter_colors.py +++ b/doc/data/sdss_colors/scatter_colors.py @@ -1,6 +1,6 @@ +from __future__ import print_function import numpy as np import pylab as pl -from __future__ import print_function data = np.load('sdssdr6_colors_class_train.npy') diff --git a/doc/data/sdss_photoz/fetch_data.py b/doc/data/sdss_photoz/fetch_data.py index d516e69..1d060d0 100644 --- a/doc/data/sdss_photoz/fetch_data.py +++ b/doc/data/sdss_photoz/fetch_data.py @@ -4,7 +4,7 @@ queries the SDSS database for the information, and thus can take a few minutes to run. """ - +from __future__ import print_function import os import urllib # Supporting Python 2 and 3 @@ -12,7 +12,6 @@ import urllib.request as urllib2 except ImportError: import urllib2 -from __future__ import print_function import numpy as np diff --git a/doc/data/sdss_spectra/fetch_data.py b/doc/data/sdss_spectra/fetch_data.py index 5a9a82e..9c97ca6 100644 --- a/doc/data/sdss_spectra/fetch_data.py +++ b/doc/data/sdss_spectra/fetch_data.py @@ -1,3 +1,4 @@ +from __future__ import print_function import os # Supporting Python 2 and 3 @@ -5,7 +6,6 @@ import urllib.request as urllib2 except ImportError: import urllib2 -from __future__ import print_function import numpy as np