18. Accessing remote resources

In [1]:
import requests
import numpy as np

# We can store the base URL in a constant
URL = "http://mpia.de/~robitaille/share/ice_data/{year:04d}{month:02d}{day:02d}.npy"

def get_map(year, month, day):
    
    result = requests.get(URL.format(year=year, month=month, day=day))
    
    # For requests, it turns out that if the file is not
    # found no error is raised but ``status_code`` is set
    # to 404:
    if result.status_code == 404:
        return None
    
    with open('temporary.npy', 'wb') as f:
        f.write(result.content)
    
    return np.load('temporary.npy')
In [2]:
m = get_map(2010, 12, 3)
In [3]:
%matplotlib inline
import matplotlib.pyplot as plt
In [4]:
plt.imshow(m)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-94c7929615f2> in <module>()
----> 1 plt.imshow(m)

~/miniconda3/envs/dev/lib/python3.6/site-packages/matplotlib/pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, data, **kwargs)
   3078                         filternorm=filternorm, filterrad=filterrad,
   3079                         imlim=imlim, resample=resample, url=url, data=data,
-> 3080                         **kwargs)
   3081     finally:
   3082         ax._hold = washold

~/miniconda3/envs/dev/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1708                     warnings.warn(msg % (label_namer, func.__name__),
   1709                                   RuntimeWarning, stacklevel=2)
-> 1710             return func(ax, *args, **kwargs)
   1711         pre_doc = inner.__doc__
   1712         if pre_doc is None:

~/miniconda3/envs/dev/lib/python3.6/site-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   5192                               resample=resample, **kwargs)
   5193 
-> 5194         im.set_data(X)
   5195         im.set_alpha(alpha)
   5196         if im.get_clip_path() is None:

~/miniconda3/envs/dev/lib/python3.6/site-packages/matplotlib/image.py in set_data(self, A)
    598         if (self._A.dtype != np.uint8 and
    599                 not np.can_cast(self._A.dtype, float, "same_kind")):
--> 600             raise TypeError("Image data cannot be converted to float")
    601 
    602         if not (self._A.ndim == 2

TypeError: Image data cannot be converted to float
In [5]:
print(get_map(2011,12,3))
None

19. Object-oriented programming

In [6]:
class Particle(object):
    
    def __init__(self, mass, x, y, z, vx, vy, vz):
        self.mass = mass
        self.x = x
        self.y = y
        self.z = z
        self.vx = vx
        self.vy = vy
        self.vz = vz
    
    def kinetic_energy(self):
        return 0.5 * self.mass * np.sqrt(self.vx ** 2 +
                                         self.vy ** 2 +
                                         self.vz ** 2)
    
    def distance(self, other):
        return np.sqrt((self.x - other.x) ** 2 +
                       (self.y - other.y) ** 2 +
                       (self.z - other.z) ** 2)
    
    def move(self, dt):
        self.x = self.x + self.vx * dt
        self.y = self.y + self.vy * dt
        self.z = self.z + self.vz * dt
        
class ChargedParticle(Particle):
    
    def __init__(self, mass, x, y, z, vx, vy, vz, charge):
        Particle.__init__(self, mass, x, y, z, vx, vy, vz)
        self.charge = charge
In [7]:
p1 = Particle(2., 1., 2., 3., 1., -3., 4.)
In [8]:
p1.kinetic_energy()
Out[8]:
5.0990195135927845
In [9]:
p1.move(1.)
In [10]:
print(p1.x, p1.y, p1.z)
2.0 -1.0 7.0
In [11]:
p1.move(1.)
In [12]:
print(p1.x, p1.y, p1.z)
3.0 -4.0 11.0
In [13]:
p2 = ChargedParticle(3., 4., 3., -2., -2., 3., 2., -1.)
In [14]:
p1.distance(p2)
Out[14]:
14.798648586948742