Skip to content

Commit

Permalink
Change all urllib2 refs to the Py3-compatible version
Browse files Browse the repository at this point in the history
  • Loading branch information
taldcroft committed Feb 7, 2015
1 parent 5efa0a3 commit a84d01d
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 32 deletions.
5 changes: 3 additions & 2 deletions source/astropy-UVES/UVES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ Please download this
the content, either by clicking on the link or by executing this
python code::

import urllib2, tarfile
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.io/_downloads/astropy_UVES.tar.gz'
tarfile.open(fileobj=urllib2.urlopen(url), mode='r|*').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|*').extractall()
cd UVES
ls

Expand Down
5 changes: 3 additions & 2 deletions source/astropy/astropy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ Please download this
the content, either by clicking on the link or by executing this
python code::

import urllib2, tarfile
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.io/_downloads/astropy_examples.tar'
tarfile.open(fileobj=urllib2.urlopen(url), mode='r|').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|').extractall()
cd data
ls

Expand Down
11 changes: 6 additions & 5 deletions source/core/numpy_scipy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ Now start IPython ("ipython --matplotlib") or use your existing session and ente
import numpy as np
import matplotlib.pyplot as plt

import urllib2, tarfile
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.com/core/core_examples.tar'
tarfile.open(fileobj=urllib2.urlopen(url), mode='r|').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|').extractall()
cd py4ast/core
ls

Expand All @@ -63,15 +64,15 @@ Leave this IPython session open for the rest of the workshop.
.. admonition:: Exercise (for the interested reader): How did that code above work?

Explain what's happening in each part of the previous code snippet to grab
the file at a URL and untar it. Google on "python urllib2" and "python
the file at a URL and untar it. Google on "python3 urllib" and "python
tarfile" to find the relevant module docs. Figure out how you would
use the ``tarfile`` module to create a tarfile.

.. raw:: html

<p class="flip0">Click to Show/Hide Solution</p> <div class="panel0">

- ``urllib2.urlopen(url)`` opens the URL as a streaming file-like object
- ``urllib.request.urlopen(url)`` opens the URL as a streaming file-like object
- ``mode='r|' means ``tarfile`` is expecting a streaming file-like object
with no ability to seek in the file
- ``tarfile.open(..).extractall`` then extracts the tar archive
Expand Down Expand Up @@ -579,7 +580,7 @@ image which includes the source region.

Let's tackle a simpler problem first and fit the background for a single column::

x = append(np.arange(10, 200), np.arange(300, 480)) # Background rows
x = np.append(np.arange(10, 200), np.arange(300, 480)) # Background rows
y = img_cr[x, 10] # Background rows of column 10 of cleaned image
plt.figure()
plt.plot(x, y)
Expand Down
8 changes: 4 additions & 4 deletions source/files/asciifiles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Do the usual imports of numpy and matplotlib::

If you have trouble downloading the file, then from within IPython enter::

import urllib2
from astropy.extern.six.moves.urllib import request
url = 'http://python4astronomers.github.com/_downloads/data.txt'
open('data.txt', 'wb').write(urllib2.urlopen(url).read())
open('data.txt', 'wb').write(request.urlopen(url).read())
ls

Now let's try and get the contents of the file into IPython. We start off by creating a file object::
Expand Down Expand Up @@ -516,9 +516,9 @@ rewrite the same code every time when it is already done!). Instead just use `a
You'll
want to start with::

import urllib2
from astropy.extern.six.moves.urllib import request
from astropy.io import ascii
html = urllib2.urlopen('http://hea-www.harvard.edu/XJET/').read() # Get web page as string
html = request.urlopen('http://hea-www.harvard.edu/XJET/').read() # Get web page as string
table1 = html2tsv(html, 0) # Parse the first table in the web page
table2 = html2tsv(html, 1) # Parse the second table
table3 = html2tsv(html, 2) # Parse the third table
Expand Down
9 changes: 5 additions & 4 deletions source/files/binaryfiles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ astropy.io.fits

Use the following code to download a FITS file for this exercise::
import urllib2, tarfile
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.com/core/core_examples.tar'
tarfile.open(fileobj=urllib2.urlopen(url), mode='r|').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|').extractall()
cd py4ast/core
ls

Expand Down Expand Up @@ -65,9 +66,9 @@ IDL is still a very common tool in astronomy. While IDL packages exist to read a
Here is an examplary ``.sav`` :download:`file <../downloads/myidlfile.sav>`.
If you have trouble downloading the file, then use IPython::

import urllib2
from astropy.extern.six.moves.urllib import request
url = 'http://python4astronomers.github.com/_downloads/myidlfile.sav'
open('myidlfile.sav', 'wb').write(urllib2.urlopen(url).read())
open('myidlfile.sav', 'wb').write(request.urlopen(url).read())
ls


Expand Down
5 changes: 3 additions & 2 deletions source/files/summary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ Here are some hints:

Download the example :download:`data <../downloads/files-excercise.tar.gz>`::

import urllib2, tarfile
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.com/_downloads/files-excercise.tar.gz'
tarfile.open(fileobj=urllib2.urlopen(url), mode='r|gz').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|gz').extractall()
cd files
ls

Expand Down
4 changes: 2 additions & 2 deletions source/fitting/image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Do the usual imports of numpy and matplotlib::
If you have trouble accessing the image you can download it straight away using
Python::

import urllib2
from astropy.extern.six.moves.urllib import request
url = "http://python4astronomers.github.com/_downloads/image2.fits"
open("image2.fits", "wb").write(urllib2.urlopen(url).read())
open("image2.fits", "wb").write(request.urlopen(url).read())
ls

Here we eschew the advice of keeping modules separate and load the
Expand Down
4 changes: 2 additions & 2 deletions source/fitting/low-level.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ and start IPython along with the standard imports::
If you have trouble accessing the spectrum you can download it straight away
using Python::

import urllib2
from astropy.extern.six.moves.urllib import request
url = 'http://python4astronomers.github.com/_downloads/3c273.fits'
open('3c273.fits', 'wb').write(urllib2.urlopen(url).read())
open('3c273.fits', 'wb').write(request.urlopen(url).read())
%ls

Import a few Sherpa classes needed to characterize a fit::
Expand Down
5 changes: 3 additions & 2 deletions source/fitting/sherpa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ then go to the py4ast/core directory, otherwise ::

$ ipython --matplotlib

import urllib2, tarfile
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.com/core/core_examples.tar'
tarfile.open(fileobj=urllib2.urlopen(url), mode='r|').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|').extractall()
cd py4ast/core

Now we load the Sherpa UI module and other requirements::
Expand Down
4 changes: 2 additions & 2 deletions source/fitting/spectrum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ and start IPython ::
If you have trouble accessing the spectrum you can download it straight away
using Python ::

import urllib2
from astropy.extern.six.moves.urllib import request
url = 'http://python4astronomers.github.com/_downloads/3c273.fits'
open('3c273.fits', 'wb').write(urllib2.urlopen(url).read())
open('3c273.fits', 'wb').write(request.urlopen(url).read())

We also need to load in Sherpa ::

Expand Down
10 changes: 6 additions & 4 deletions source/plotting/aplpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ Start off by downloading :download:`this tar file <../downloads/APLpy-example.ta

If you have trouble downloading the file, then within your IPython session enter::

import urllib2, tarfile
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.com/_downloads/APLpy-example.tar'
tarfile.open(fileobj=urllib2.urlopen(url), mode='r|').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|').extractall()
cd APLpy-example
ls

Expand Down Expand Up @@ -137,8 +138,9 @@ To show the y-axis labels in dd:mm format::

If you have trouble downloading the file, then within your IPython session enter::

import urllib2, tarfile
from astropy.extern.six.moves.urllib import request
import tarfile
url = 'http://python4astronomers.github.com/_downloads/m82_wise.tar'
tarfile.open(fileobj=urllib2.urlopen(url), mode='r|').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|').extractall()
cd m82_wise
ls
2 changes: 1 addition & 1 deletion source/plotting/matplotlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ somewhat cryptic messsage above. For new users it would be nice if it
said "hold on, size isn't callable", but then this would inhibit
useful - if complex - statements such as::

tarfile.open(fileobj=urllib2.urlopen(url), mode='r|').extractall()
tarfile.open(fileobj=request.urlopen(url), mode='r|').extractall()

Controlling line properties
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit a84d01d

Please sign in to comment.