#!/usr/bin/env python
# coding: utf-8
# In[1]:
# Styling
from IPython.core.display import HTML
def css_styling():
sheet = './css/custom.css'
styles = open(sheet, "r").read()
return HTML(styles)
css_styling()
# # NGCM Summer Academy 2016:
#
# ## Intermediate Python
#
# Paul Chambers, Ioannis Begleris
#
# P.R.Chambers@soton.ac.uk
#
#
#
# ## Foreword
#
# * Bridge gap between (SWC) basics and IPython course
# * Focus on applied data analysis
# * Notebook live slideshow: [RISE](https://github.com/damianavila/RISE)
# ## Objectives
#
# - [Timetable](http://ngcm.soton.ac.uk/images/summeracademy/2016/timetable1.pdf) - end at 14:45
# - Basics revision ~ 45 mins
# - Tuples, Dictionaries ~ 30 mins
# - Objects, Classes ~ 120 mins
# ## Material Outline
#
# * Basics Revision
# - Lists, loops, functions, Numpy, Matplotlib
#
# * Tuples, mutability
#
# * Dictionaries
# - Associative data, function argument unpacking
# ## Material Outline (ctd)
#
# * Object Oriented Programming
# - `Classes`
# - Initialization and `__init__`, `self`
# - Encapsulation
# - Inheritance
# - Magic operators
# ## Prerequisites
#
# * Python >= 3.4, Jupyter (Notebook)
# * Numpy, Matplotlib
# * Download material:
# https://ngcm.github.io/summer-academy-2016-basics/basics_B.zip
# If you are following this course and do not know how to obtain the above requirements, see [Setup Instructions](http://ngcm.github.io/summer-academy-2016-basics/indexpages/prerequisites.html).
# In[2]:
# Run this cell before trying examples
import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
# ## Basics Refresher
#
# * Lists
# * `For` loops
# * Functions
# * Numpy
# * Matplotlib
# ### External Material
# * If you missed the basics, try these:
# - [Hans Fangohr Python Book](http://www.southampton.ac.uk/~fangohr/training/python/pdfs/Python-for-Computational-Science-and-Engineering.pdf)
# - [Software Saved Basics Course](http://softwaresaved.github.io/NGCMGSoton-2015-06-21/novice/python/index.html)
# ### Lists
#
# * *Mutable* Container
# * Often appended to in a loop with '`append`'
# * Handles mixed data types
# * Allows indexing and slicing
# #### Example: List indexing and slicing
# In[3]:
lists = [10, 12, 14, 16, 18]
print(lists[0]) # Index starts at 0
print(lists[-1]) # Last index at -1
print(lists[0:3]) # Slicing: exclusive of end value
# i.e. get i=(0, 1, .. n-1)
print(lists[3:]) # "slice from i=3 to end"
# #### Example: Methods of lists
# In[4]:
# List construction example
a = []
print(a)
a.append('Hello world')
print(a)
# In[5]:
a.extend([1, 2, 3, 4])
print(a)
a.remove(1) # Remove value 1 from a
print(a)
a.pop(0)
print(a)
# In[6]:
# All methods
# a. # Tab complete behaviour?
# ### For Loops
#
# * Need to iterate **through** something in Python
# * Not an explicit counting loop as used in lower level languages
# * Syntax (`var` is user defined) -
# For var in iterable:
# #### Basic Example:
# In[7]:
powers = [0, 1, 2, 3]
for power in powers:
value = 10 ** (power)
print("10 to the power of {} is {}".format(power, value))
# In[8]:
# Better to use Pythons built in 'range' here:
for i in range(4):
print("10 to the power of {} is {}".format(i, 10**i))
# #### List compehension
# In[9]:
x = [i**2 for i in range(10)]
y = [i*10 for i in range(10)]
print(x)
print(y)
# In[10]:
#### How would you create a list from zero to 100 in increments of 5 in one line
# ### Functions
# * **`def`** followed by function name and parameters in a parenthesis
# * **`return output_var(s)`** ends the function
# * Args parsed by assignment: Mutability dictates whether the function can update args directly
# #### Basic function
# In[11]:
def square_root(x):
"""Useful docstring: Calculates and returns square root of x"""
i = x ** 0.5
return i
x = 10
y = square_root(x)
print('The square root of {} is {}'.format(x, y))
# In[12]:
# We can set a default value to the function
def square_root(x=20):
i = x ** 0.5
return i
print(square_root())
# In[13]:
# Loops, functions and appending
mylist = []
for i in range(1,5):
mylist.append(square_root(i))
print(mylist)
# #### Example: Arguments and mutability
# In[14]:
def update_integer(i):
# attempt to update i (integers) are immutable
i += 1
def update_list_end(arglist):
arglist[-1] = 50 # Lists are mutable: updates args directly!
a = 1
update_integer(a)
print(a)
mylist = [0, 1, 2, 3, 4]
update_list_end(mylist)
print(mylist)
# Note above that there is no **`return`** statement required: implicitly this function will return the Python builtin-in value **`None`**.
# ### Numpy
# * Arrays and array operations
# * Mathematical evaluations - fast on `np.array`
# * Linear algebra
# * Useful functions
# * Allows integration between C/C++ and Fortran
# #### Examples: Basic functions
# In[15]:
import numpy as np
# basic usage: arange, linspace, array ops
x = np.linspace(0, 10, 11) # use 11 points
print(x)
y = np.arange(0, 10, 1) # use step size of 1
print(y)
print('The average of x is', np.average(x))
# #### Example: 2D arrays
# In[16]:
M1 = np.array([[2,3],[6,3]])
M2 = np.array([[5,6],[2,9]])
print('M1:')
print(M1)
print('M2:')
print(M2)
# $$M_1 = \begin{bmatrix} 2 & 3 \\ 6 & 3\end{bmatrix}, \quad M_2 = \begin{bmatrix} 5 & 6 \\ 2 & 9\end{bmatrix}$$
# In[17]:
M3 = M1 * M2 # Element-wise multiplication
print(M3, '\n')
M4 = np.dot(M1, M2) # Matrix multiplication
print(M4)
# In[18]:
# Given array [0, np.pi/2., np.pi, 3*np.pi/4.] what would you
# expect passing it to np.sin ????
# In[19]:
# live coding show some numpy functions.
# ### Matplotlib
# * Popular plotting library
# * Can produce publication quality plots
# * Allows embedded LaTeX formatting
#
# #### Example: 2D plotting
# In[20]:
x = np.linspace(0, 2*np.pi)
y = np.sin(x)
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(111)
ax.plot(x, y,'o-')
ax.margins(0.1)
ax.set_title('2D plot')
ax.set_xlabel('$x$')
ax.set_ylabel(r'$sin(x)$')
# #### What if we want increments of $\pi$ on our x axis
# In[21]:
xtick_values = np.linspace(0, 2*np.pi, 5)
xtick_labels = ['$0$', r'$\frac{\pi}{2}$', r'$\pi$', r'$\frac{3\pi}{2}$',
r'$2\pi$']
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(111); ax.plot(x, y,'-o')
ax.set_title('2D plot')
ax.margins(0.1)
ax.set_xlabel('$x$'); ax.set_ylabel(r'$sin(x)$')
ax.set_xticks(xtick_values)
ax.set_xticklabels(xtick_labels, fontsize=25);
#
# #### Example: 3D plot
# In[22]:
x = np.linspace(-1, 1, 101)
y = np.linspace(-1, 1, 101)
X, Y = np.meshgrid(x, y)
Z = np.sin(X + Y)**2
# In[23]:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z)
ax.set_xlabel(r'$X$')
ax.set_ylabel(r'$Y$')
ax.set_zlabel(r'$Z$')
plt.show()
# In[24]:
# Live coding Bay....
# ## Exercise: Basics Refresher
#
# [Python Basics Refresher](exercises/00-Basics_refresher.ipynb)
# ## Tuples
#
# * An *Immutable* List
# * Faster than a List (fixed memory)
# * Useful for **structured** data
# * No *append* method - bad for sequential data
# #### Example: Tuple Syntax
# In[25]:
# Create a 'Name, Age' Tuple using bracket notation
my_tuple = ('Dave', 42)
print(type(my_tuple))
print(my_tuple)
# In[26]:
# Create Tuple using bracket-less notation
my_tuple2 = 'Bob', 24
print(type(my_tuple2))
print(my_tuple2)
# #### Example: Usage
# In[27]:
# Tuple indexing
my_tuple = ('Dave', 42)
print(my_tuple[0])
print(my_tuple[1])
# In[28]:
# Could make a list of tuples:
tups = [('Dave', 42), ('Bob', '24')]
# ... and then iterate over it
for tup in tups:
print("{} is {} years old".format(tup[0], tup[1]))
# #### Example: Tuple Unpacking
# In[29]:
# Store multiple variables using tuples:
my_tuple = 'Dave', 42
a, b = my_tuple
print('a = {}'.format(a))
print('b = {}'.format(b))
# In[30]:
# Swap Variables using tuples:
b, a = a, b
print('a = {}'.format(a))
print('b = {}'.format(b))
# #### Example: When NOT to use a Tuple (1)
# In[31]:
# extending or overwriting contents
my_tuple = 'Dave', 42
# my_tuple[0] = 'Steve' # Will give an error
# #### Example: When NOT to use a Tuple (2)
# In[32]:
# Sequences: Stick with a list
seq = [] # tuples have no append method, so need a list []
for i in range(10):
seq.append(i**2)
print(seq)
# In[33]:
# Or a numpy array:
print(np.arange(10)**2)
# In[34]:
# Create a tuple of lists 'a' - can you change the values in
# the lists?
# In[35]:
# Live coding Bay....
# ## Dictionaries
#
# * Set of `key` : `value` pairs
# * Ordering follows hash table rules, not so intuitive to humans
# * Use curly braces - {} or `dict` keyword
# #### Example: Fruit Prices Lookup Table - Construction
# In[36]:
# Using the dict function:
fruit = [('apples', 2), ('bananas', 5), ('pears', 10)]
price_table = dict(fruit)
print(price_table)
# In[37]:
# Short hand (Arguably neater)
price_table = {'apples': 2, 'pears': 10, 'bananas': 5}
print(price_table)
# **Note**: notice that the consistent order on printing of the dictionaries, even though the inputs are reordered. The ordering of [hash tables](https://en.wikipedia.org/wiki/Hash_table) is well defined, but not in a human-intuitive sense. We should therefore treat the data as if it was unordered.
# #### Example: Accessing values from keys
# In[38]:
price_table = {'apples': 2, 'bananas': 5, 'pears': 10}
akey = 'apples'
print("The price of {} is {}p".format(akey, price_table[akey]))
# #### Example: Iterating over a dictionary
# In[39]:
# Iterating over the dictionary will iterate over its keys
price_table = {'apples': 2, 'bananas': 5, 'pears': 10}
for key in price_table:
print("{} cost {}p".format(key, price_table[key]))
# In[40]:
# Or use the items method:
for key, val in price_table.items():
print("{} cost {}p".format(key, val))
# #### Example: Shopping list using dictionary price lookup
# In[41]:
# I don't like pears, so let's buy apples and bananas
shopping_list = [('apples', 50), ('bananas', 20)]
total = 0
for item, quantity in shopping_list:
price = price_table[item]
print('Adding {} {} at {}p each'.format(quantity, item, price))
total += price * quantity
print(total)
# #### Example: When NOT to use a Dictionary
# In[42]:
# Hoping for ordered data:
alpha_num = {'a': 0, 'b': 1, 'c': 2}
for i, key in enumerate(alpha_num.keys()):
print("{} has a value of {}".format(key, i)) # This is wrong
# #### Example: Dictionary unpacking using '**'
# In[43]:
mydict = {'a':1, 'b':2, 'c':3}
def myFunc(a,b,c):
return a*2, b*2, c*2
myFunc(**mydict)
# In[44]:
# Live coding Bay....
# ## Exercise: Tuples and Dictionaries
#
# [Tuples and Dictionaries Exercise](exercises/01-Tuples_Dictionaries.ipynb)
# # Intro to Python OOP
#
# #### (For The Classy Programmer)
#
# >
# "Object-oriented programming (OOP) refers to a type of computer programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure."
# >
#
# Source: [Webopedia](http://www.webopedia.com/TERM/O/object_oriented_programming_OOP.html)
# ## Why OOP?
#
# * Naturally structured data
# * Functions used in context
# * Reduce duplicate code
# * Maintainability in large codes/software
# * [Many other reasons](http://inventwithpython.com/blog/2014/12/02/why-is-object-oriented-programming-useful-with-an-role-playing-game-example/)
# ### OOP in Scientific Computing
# * Java, `C++` and Python designed for OOP
# * **Everything** in Python is an object
# * Scientific libraries, visualisation tools etc.
# * Pseudo Object Orientation in `C` and `Fortran`
# ### What will I learn about OOP here?
# * Language in OOP is very different
# - Learn language used in eg. C++, Java
# * Ability to **read** code is essential
# * Write/migrate code for community library
# - Better world! Work recognition etc...
# ### OOP: Four Fundamental Concepts
#
# * *Inheritance*
# - Reuse code by deriving from existing classes
#
# * *Encapsulation*
# - Data hiding
# ### OOP: Four Fundamental Concepts (2)
#
# * *Abstraction*
# - Simplified representation of complexity
#
# * *Polymorphism*
# - API performs differently based on data type
# **Note:** Encapsulation is sometimes also used in OOP to describe the grouping of data with methods. It is however more common for texts to use it to describe the hiding of data as will be done here.
#
# Useful explanations of these concepts for Python can also be found [here](http://zetcode.com/lang/python/oop/)
# ## Structured data: Numpy dtypes
# * Not a class, but motivational example
# * Structured associative data
# * Multiple data accessible from a single data type
# * Identifiers to indicate data type
# #### Example: Data about people
# In[45]:
with open('data/structured_data.txt', 'w') as f:
f.write('#Name Height Weight\n')
f.write('John 180 80.5\n')
f.write('Paul 172 75.1\n')
f.write('George 185 78.6\n')
f.write('Ringo 170 76.5\n')
# In[46]:
# Notice that the argument is a list of tuples
dt = np.dtype([('Name', np.str_, 16), ('Height', np.int32),
('Weight', np.float64)])
data = np.loadtxt('data/structured_data.txt', dtype=dt)
print(data)
# In[47]:
print(data['Name'])
print("{} has weight {}".format(data[0]['Name'], data[0]['Weight']))
# In[48]:
# Live coding Bay....
# ## Exercise: Numpy `dtypes`
#
# [Exercise: Load image data with dtype structured array](exercises/03-dtypes.ipynb)
# * Data is structured, but not elegant
# * no methods etc
# ## Classes: Basics
# * Attributes (data)
# * Methods (Functions operating on the attributes)
# * 'First class citizens': Same rights as core types
# - pass to functions, store as variables etc.
# #### Example: Numpy arrays showing how it's done
# In[49]:
# Numpy arrays are classes
import numpy as np
a = np.array([0, 1, 6, 8, 12])
print(a.__class__)
print(type(a))
# In[50]:
# We want to operate on the array: try numpy cumulative sum function
print(np.cumsum(a))
# In[51]:
# np.cumsum('helloworld') # Should we expect this to work?
# #### Example: Numpy arrays showing how it's done (ctd.)
#
# * We only know what a cumulative sum means for a narrow scope of data types
#
# * Group them together with an object!
# In[52]:
# cumsum is a method belonging to a
a.cumsum()
# #### Example: Simple class
# * For now, assume all classes defined by: `class ClassName(object)`
# In[53]:
class Greeter(object):
def hello(self): # Method (more on 'self' later)
print("Hello World")
agreeter = Greeter() # 'Instantiate' the class
print(agreeter)
# agreeter. # Tab complete?
# There's a few things here which I haven't introduced, but all will become clear in the remainder of this workshop.
# In[54]:
# Note that we don't pass an argument to hello!
agreeter.hello()
# ## Classes: Initialisation and `self`
#
# * ```__init__``` class method
# * Called on creation of an instance
# * Convention: `self` = instance
# * **Implicit passing** of self, **explicit receive**
#
# **Note:** Passing of `self` is done implicitly in other languages e.g. C++ and Java, and proponents of those languages may argue that this is better. "*Explicit is better than implicit*" is [simply the python way](https://www.python.org/dev/peps/pep-0020/).
# ### What is an "instance"?
#
# * `Class` is like a type
# * Instance is a specific realisation of that type
# * eg. "Hello World" is an instance of string
# * Instances attributes are not shared
#
# ### Classes: Initialisation vs Construction
# * Initialisation changes the instance when it is made
# * ... `__init__` is not *technically* Construction (see: C++)
# * `__new__` '*constructs*' the instance before `__init__`
# * `__init__` then initialises the content
# **More info:** The Constructor *creates* the instance, and the Initialiser *Initialises* its contents. Most languages e.g. C++ refer to these interchangably and perform these steps together, however the new style classes in Python splits the process.
#
# The difference is quite fine, and for most purposes we do not need to redefine the behaviour of `__new__`. This is discussed in several Stack Overflow threads, e.g.
#
# * [Python (and Python C API): `__new__` versus `__init__`](http://stackoverflow.com/questions/4859129/python-and-python-c-api-new-versus-init)
#
# * [Python's use of `__new__` and `__init__`?](http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init)
# #### Example: Class Initialisation
# In[55]:
class A(object):
def __init__(self):
print("Hello")
a_instance = A()
print(type(a_instance))
# ### Instance attributes and Class attributes
# * Instance attributes definition: `self.attribute = value`
# * Class attributes defined outside functions (*class scope*)
# * Class attributes are shared by all instances
# * Be careful with class attributes
# #### Example: Defining Instance attributes/methods
# In[56]:
class Container(object):
"""Simple container which stores an array as an instance attribute
and an instance method"""
def __init__(self, N):
self.data = np.linspace(0, 1, N)
def plot(self):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(self.data, 'bx')
mydata = Container(11) # 11 is passed as 'N' to __init__
print(mydata.__dict__) # __dict__ is where the attr: value
# pairs are stored!
mydata.plot()
# #### Your turn!
# * Implement a class which takes an input number 'N', and doubles and stores it as an instance attribute
# * Test it!
# In[57]:
# Code solution here:
# #### Example: Class attributes vs instance attributes
# In[58]:
class Container(object):
data = np.linspace(0, 1, 5) # class attribute
def __init__(self):
pass
a, b = Container(), Container()
print(a.data)
print(b.data)
# In[59]:
a.data = 0 # Creates INSTANCE attribute
Container.data = 100 # Overwrites CLASS attribute
print(a.data)
print(b.data)
# ### Class vs Instance attributes: priority
#
# ![Class vs instance attribute priority diagram](images/class_vs_instance_priority.png)
#
# * Source: [toptotal.com](https://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide)
# **Note**: There's a couple of things going on in this example which are worth elaborating on. By specifying `ClassName.attribute`, in this case `Container.data = 100` we've overwritten the value of `data` that EVERY instance of the `Container` class will access. Hence printing `b.data` gives the expected result.
#
# By setting `a.data` at the same time, we have set an *instance* attribute, which is given priority and called first even though we overwrote the class attribute after assigning this.
#
# This could create a hard to track bug. To avoid it:
#
# * Stick to instance variables unless you specifically need to share data e.g. constants, total number or list of things that are shared
# * Don't overwrite things you know are class attributes with `instance.attr` unless you really know what you're doing (even then, it's probably better and more readable to make it an instance attribute)
#
# For a really in depth explanation of class vs instance attributes, see either of the following links:
#
# * [python3_class_and_instance_attributes](http://www.python-course.eu/python3_class_and_instance_attributes.php)
#
# * [python-class-attributes-an-overly-thorough-guide](https://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide)
# #### Example: Implicit vs Explicit passing Instance
# In[60]:
class Container(object):
def __init__(self, N):
self.data = np.linspace(0, 1, N)
def print_data(self):
print(self.data)
a = Container(11)
a.print_data() # <<< This is better
Container.print_data(a)
# ## Exercise: Basics and Initialisation
#
# [Python OOP 1 Basics and Initialisation with images](exercises/04-Classes_basics.ipynb)
# ## Classes: Encapsulation
# * Hiding data from users (and developers)
# * Use underscores '`_`' or '`__`'
# * Useful if data changing should be controlled
# * **Convention only in Python** - not enforced!
# ### Single vs Double Underscore
#
# * Single underscore
# - Nobody outside this class or derived classes should access or change
#
# * Double underscore
# - Stronger attempt to enforce the above
# - Also 'mangles' the attribute name with
#
# `instance._ClassName__Attribute`
# #### Example: Data hiding "protected", single underscore
# In[61]:
class Fruit(object):
def __init__(self):
self._hasjuice = True
def juice(self):
if not self.isfull(): raise ValueError('No juice!')
self._hasjuice = False
def isfull(self):
return self._hasjuice
orange = Fruit()
print(orange.isfull())
orange.juice()
print(orange.isfull())
# In[62]:
# orange. # tab completion behaviour?
# orange._ # tab completion behaviour now?
orange._hasjuice = True # bad!
orange.isfull()
# #### Example: Data hiding "private", double underscore
# In[63]:
class Fruit(object):
def __init__(self):
self.__hasjuice = True
def juice(self):
if not self.isfull(): raise ValueError('No juice!')
self.__hasjuice = False
def isfull(self):
return self.__hasjuice
apple = Fruit()
# In[64]:
# apple._ # tab completion behaviour?
apple.juice()
apple._Fruit__hasjuice = False # Definitely bad!
apple.isfull()
# **Note**: This behaviour can be over used in Python. Programmers from C++ or Java backgrounds may want to make all data hidden or `private` and access the data with 'getter' or 'setter' functions, however it's generally accepted by Python programmers that getters and setters are unnecessary. The Pythonista phrase is "we are all consenting adults here", meaning you should trust the programmer to interact with your classes and they should trust you to document/indicate which parts of the data not to touch unless they know what they're doing (hence the underscore convention). See the top answer on [this Stack Overflow thread](http://stackoverflow.com/questions/6930144/underscore-vs-double-underscore-with-variables-and-methods).
#
# For an entertaining view of encapsulation, see [this blog](http://radek.io/2011/07/21/private-protected-and-public-in-python/)
# In[65]:
# Live coding Bay....
# ## Classes: Inheritance
#
# * Group multiple objects and methods
# * `Child`/`Derived` class inherits from `Parent`/`Base`
# * Reduce duplicate code
# * Maintanable: changes to base falls through to all
# * Beware multiple inheritance rules - we won't cover this
# #### Example: Simple inheritance
# In[66]:
class Parent(object):
# Note the base __init__ is overridden in
# Child class
def __init__(self):
pass
def double(self):
return self.data*2
class Child(Parent):
def __init__(self, data):
self.data = data
achild = Child(np.array([0, 1, 5, 10]))
achild.double()
# #### Example: Calling parent methods with `super`
# In[67]:
class Plottable(object):
def __init__(self, data):
self.data = data
def plot(self, ax):
ax.plot(self.data)
class SinWave(Plottable):
def __init__(self):
super().__init__(
np.sin(np.linspace(0, np.pi*2, 101)))
class CosWave(Plottable):
def __init__(self):
super().__init__(
np.cos(np.linspace(0, np.pi*2, 101)))
fig = plt.figure()
ax = fig.add_subplot(111)
mysin = SinWave(); mycos = CosWave()
mysin.plot(ax); mycos.plot(ax)
# **Notes:**
# * We didn't need any arguments to `super` here as Python 3 allows this
# * `super` requires additional arguments in Python 2, e.g.
#
# super(Class, self).method(args...)
#
# If you were wondering why we should use `super().method` instead of `BaseClass.method`, other than the convenience of renaming classes, it relates to multiple inheritance which is beyond the scope of this course. If you need to write programs with multiple inheritance (and there are strong arguments against this), you may want to look at [this blog](http://sixty-north.com/blog/pythons-super-not-as-simple-as-you-thought) for advanced use of `super`.
# ## Classes: Magic Methods
# * The workhorse of how things 'just work' in Python
# * `object` & builtin types come with many of these
# * Surrounded by double underscores
# * We've seen one already: **`__init__`!**
# #### Example: The magic methods of object
# In[68]:
dir(object)
# ### Magic Methods: Closer look
# * `__lt__`:
# - Called when evaluating
# a < b
#
# * `__str__`
# - Called by the print function
#
# * All magics can be overridden
# #### Example: overriding `__str__` and `__lt__` magic methods
# In[69]:
class Wave(object):
def __init__(self, freq):
self.freq = freq
self._data = np.sin(np.linspace(0, np.pi, 101)
* np.pi*2 * freq)
def __str__(self):
"""RETURNS the string for printing"""
return "Wave frequency: {}".format(self.freq)
def __lt__(self, wave2):
return self.freq < wave2.freq
wav_low = Wave(10)
wav_high = Wave(50) # A high frequency wave
print(wav_high)
wav_low < wav_high
# In[70]:
# Live coding Bay....
# **Note**: Magic methods are very briefly introduced here. For an extensive overview of magic methods for Python classes, view [Rafe Kettlers blog](http://www.rafekettler.com/magicmethods.html)
# ## Exercise: Inheritance and Magic Methods
#
# [Python OOP2 Inheritance and Magic Methods using shapes](exercises/05-Classes_pt2.ipynb)
# ## Python 2 vs 3
#
# * Dont need to inherit `object` in Python 3
# * New classes inherit from `object`
# * Inheritance behaviour is different
# * Old classes removed in Python 3
# #### Example: New class default Python 3
# In[71]:
class OldSyntax:
pass
class NewSyntax(object): # This means 'inherit from object'
pass
print(type(OldSyntax)) # Would give
# in Python 2
print(type(NewSyntax))
# * Backwards compatibility: inherit `object` in Py3
# Notes: There are other differences affecting classes which we have not included, such as metaclasses and iterator behaviour, but here is a link to a more complete comparison:
#
# * http://python3porting.com/differences.html
# ## Extra Material
#
# * Harder exercise (if time)
#
# * Should test your knowledge from this course
#
# > [Exercise: Predator Prey ](exercises/06-Predator_prey.ipynb)
#
# * Other things worth looking at (an incredibly biased opinion):
# - [Building Pythonic Packages with setuptools](https://pythonhosted.org/an_example_pypi_project/setuptools.html)
# - [Unit testing with py.test](https://pytest.org/latest/contents.html)
# - [Conda Environments](http://conda.pydata.org/docs/using/envs.html)
# - Play with a visualisation/GUI package!
#
# ## Summary
# * Refreshed your basic Python skills
# * Python builtins not included in standard basics course
# - Tuples, Dictionaries, Generators
# * Software data structuring
# ## Summary (ctd)
# * Covered the language and concepts for Object Orientation (Transferable!)
# * OOP Implementation in Python
# * `Read` the packages you use daily
# * Create maintainable packages for yourself and scientific community
# # Thank You
#
# P.R.Chambers@soton.ac.uk