Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate...

31
Interfaces Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Classes and Objects

Transcript of Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate...

Page 1: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Interfaces

Copyright © Software Carpentry 2010This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

Classes and Objects

Page 2: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Classes and objects help you separate interface

from implementation

Page 3: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Classes and objects help you separate interface

from implementation

Interface: what something knows how to do

Page 4: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Classes and objects help you separate interface

from implementation

Interface: what something knows how to do

Implementation: how it does things

Page 5: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Classes and objects help you separate interface

from implementation

Interface: what something knows how to do

Implementation: how it does things

Programming to interfaces makes it (much) easier to

test/change/replace parts of a program

Page 6: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Classes and objects help you separate interface

from implementation

Interface: what something knows how to do

Implementation: how it does things

Programming to interfaces makes it (much) easier to

test/change/replace parts of a program

Explain by example

Page 7: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Starting point: irregular time series signal

time

stre

ngth

Page 8: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Starting point: irregular time series signal

time

stre

ngth

Hide irregularity by allowing sampling at any time

Page 9: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Starting point: irregular time series signal

time

stre

ngth

Hide irregularity by allowing sampling at any time

step function

Page 10: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Starting point: irregular time series signal

time

stre

ngth

Hide irregularity by allowing sampling at any time

step function linear interpolation

Page 11: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Define the interface first

Page 12: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Define the interface firstclass SomeClassName(object):

def __init__(self, values):'''Values is ((x0, y0), (x1, y1), ...)'''store values

Page 13: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Define the interface firstclass SomeClassName(object):

def __init__(self, values):'''Values is ((x0, y0), (x1, y1), ...)'''store values

def get(self, where):if where is out of bounds:

raise exceptionelse:

return interpolated value

Page 14: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

First implementationclass StepSignal(object):

def __init__(self, values):self.values = values[:] # make a copy

Page 15: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

First implementationclass StepSignal(object):...def get(self, where):

if where < self.values[0][0]:raise IndexError, '%f too low' % where

for i in range(len(self.values)-1):x0, y0 = self.values[i]x1, y1 = self.values[i+1]if x0 <= where <= x1:

return y0raise IndexError, '%f too high' % where

Page 16: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Test a few pointsinterp = StepSignal(((0., 0.), (1., 1.), (2.,

2.)))for x in (0.0, 0.5, 1.0, 1.75):print x, interp.get(x)

0.0 0.00.5 0.01.0 1.01.75 1.0

Page 17: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Test error handling toofor val in (-100.0, -0.0001, 2.0, 100.0):try:

interp.get(val)assert False, 'Should not be here:', val

except IndexError, e:print val, 'raised expected exception'

-100.0 raised expected exception-0.0001 raised expected exception2.0 raised expected exception100.0 raised expected exception

Page 18: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Now create second implementationclass LinearSignal(object):...def get(self, where):

if where < self.values[0][0]:raise IndexError, '%f too low' % where

for i in range(len(self.values)-1):x0, y0 = self.values[i]x1, y1 = self.values[i+1]if x0 <= where <= x1:

return y0 + (y1-y0) * (where-x0) / (x1-x0)raise IndexError, '%f too high' % where

Page 19: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Now create second implementationclass LinearSignal(object):...def get(self, where):

if where < self.values[0][0]:raise IndexError, '%f too low' % where

for i in range(len(self.values)-1):x0, y0 = self.values[i]x1, y1 = self.values[i+1]if x0 <= where <= x1:

return y0 + (y1-y0) * (where-x0) / (x1-x0)raise IndexError, '%f too high' % where

Page 20: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Test it as wellinterp = LinearSignal(((0., 0.), (1., 1.),

(2., 2.)))for x in (0.0, 0.5, 1.0, 1.75):print x, interp.get(x)

0.0 0.00.5 0.51.0 1.01.75 1.75

Page 21: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Test it as wellinterp = LinearSignal(((0., 0.), (1., 1.),

(2., 2.)))for x in (0.0, 0.5, 1.0, 1.75):print x, interp.get(x)

0.0 0.00.5 0.51.0 1.01.75 1.75

Page 22: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

Test it as wellinterp = LinearSignal(((0., 0.), (1., 1.),

(2., 2.)))for x in (0.0, 0.5, 1.0, 1.75):print x, interp.get(x)

0.0 0.00.5 0.51.0 1.01.75 1.75Error handling still works

Page 23: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

And now the payoff

Page 24: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

And now the payoffdef average(signal, x0, x1, num_samples):width = (x1 - x0) / num_samplestotal = 0.0for i in range(num_samples):

x = x0 + i * widthtotal += signal.get(x)

return total / num_samples

Page 25: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

And now the payoffdef average(signal, x0, x1, num_samples):width = (x1 - x0) / num_samplestotal = 0.0for i in range(num_samples):

x = x0 + i * widthtotal += signal.get(x)

return total / num_samples

Page 26: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

And now the payoffdef average(signal, x0, x1, num_samples):width = (x1 - x0) / num_samplestotal = 0.0for i in range(num_samples):

x = x0 + i * widthtotal += signal.get(x)

return total / num_samplesCan use an object of either class for signal

Page 27: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

And now the payoffdef average(signal, x0, x1, num_samples):width = (x1 - x0) / num_samplestotal = 0.0for i in range(num_samples):

x = x0 + i * widthtotal += signal.get(x)

return total / num_samplesCan use an object of either class for signal

Or an object of a class that doesn't exist yet

Page 28: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

For example…class Sinusoid(object):

def __init__(self, amplitude, frequency):self.amp = amplitudeself.freq = frequency

def get(self, x):return self.amp * math.sin(x * self.freq)

Page 29: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

For example…class Sinusoid(object):

def __init__(self, amplitude, frequency):self.amp = amplitudeself.freq = frequency

def get(self, x):return self.amp * math.sin(x * self.freq)

Clear interfaces make code more extensible

Page 30: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

Classes and Objects Interfaces

For example…class Sinusoid(object):

def __init__(self, amplitude, frequency):self.amp = amplitudeself.freq = frequency

def get(self, x):return self.amp * math.sin(x * self.freq)

Clear interfaces make code more extensible

Only care about actual class when constructing

Page 31: Classes and Objects · Classes and Objects Interfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation:

January 2011

Copyright © Software Carpentry 2010This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

created by

Greg Wilson