Download presentation

Presentation is loading. Please wait.

Published byBryant Cripps Modified over 2 years ago

1
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

2
Interfaces Classes and objects help you separate interface from implementation

3
Classes and ObjectsInterfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do

4
Classes and ObjectsInterfaces Classes and objects help you separate interface from implementation Interface: what something knows how to do Implementation: how it does things

5
Classes and ObjectsInterfaces 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

6
Classes and ObjectsInterfaces 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

7
Classes and ObjectsInterfaces Starting point: irregular time series signal time strength

8
Classes and ObjectsInterfaces Starting point: irregular time series signal time strength Hide irregularity by allowing sampling at any time

9
Classes and ObjectsInterfaces Starting point: irregular time series signal time strength Hide irregularity by allowing sampling at any time step function

10
Classes and ObjectsInterfaces Starting point: irregular time series signal time strength Hide irregularity by allowing sampling at any time step functionlinear interpolation

11
Classes and ObjectsInterfaces Define the interface first

12
Classes and ObjectsInterfaces Define the interface first class SomeClassName(object): def __init__(self, values): '''Values is ((x0, y0), (x1, y1),...)''' store values

13
Classes and ObjectsInterfaces Define the interface first class 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 exception else: return interpolated value

14
Classes and ObjectsInterfaces First implementation class StepSignal(object): def __init__(self, values): self.values = values[:] # make a copy

15
Classes and ObjectsInterfaces First implementation class 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 y0 raise IndexError, '%f too high' % where

16
Classes and ObjectsInterfaces Test a few points interp = 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.5 0.0 1.0 1.75 1.0

17
Classes and ObjectsInterfaces Test error handling too for 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 exception 2.0 raised expected exception 100.0 raised expected exception

18
Classes and ObjectsInterfaces Now create second implementation class 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

19
Classes and ObjectsInterfaces Now create second implementation class 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

20
Classes and ObjectsInterfaces Test it as well interp = 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.5 1.0 1.75

21
Classes and ObjectsInterfaces Test it as well interp = 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.5 1.0 1.75

22
Classes and ObjectsInterfaces Test it as well interp = 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.5 1.0 1.75 Error handling still works

23
Classes and ObjectsInterfaces And now the payoff

24
Classes and ObjectsInterfaces And now the payoff def average(signal, x0, x1, num_samples): width = (x1 - x0) / num_samples total = 0.0 for i in range(num_samples): x = x0 + i * width total += signal.get(x) return total / num_samples

25
Classes and ObjectsInterfaces And now the payoff def average(signal, x0, x1, num_samples): width = (x1 - x0) / num_samples total = 0.0 for i in range(num_samples): x = x0 + i * width total += signal.get(x) return total / num_samples

26
Classes and ObjectsInterfaces And now the payoff def average(signal, x0, x1, num_samples): width = (x1 - x0) / num_samples total = 0.0 for i in range(num_samples): x = x0 + i * width total += signal.get(x) return total / num_samples Can use an object of either class for signal

27
Classes and ObjectsInterfaces And now the payoff def average(signal, x0, x1, num_samples): width = (x1 - x0) / num_samples total = 0.0 for i in range(num_samples): x = x0 + i * width total += signal.get(x) return total / num_samples Can use an object of either class for signal Or an object of a class that doesn't exist yet

28
Classes and ObjectsInterfaces For example… class Sinusoid(object): def __init__(self, amplitude, frequency): self.amp = amplitude self.freq = frequency def get(self, x): return self.amp * math.sin(x * self.freq)

29
Classes and ObjectsInterfaces For example… class Sinusoid(object): def __init__(self, amplitude, frequency): self.amp = amplitude self.freq = frequency def get(self, x): return self.amp * math.sin(x * self.freq) Clear interfaces make code more extensible

30
Classes and ObjectsInterfaces For example… class Sinusoid(object): def __init__(self, amplitude, frequency): self.amp = amplitude self.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

31
January 2011 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. created by Greg Wilson

Similar presentations

OK

Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.

Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on mergers and acquisitions in banking Ppt on bluetooth controlled robot car Ppt on peak load pricing strategy Ppt on number system for class 9 cbse Ppt on video teleconferencing systems Ppt on wind from sun power plant Ppt on polynomials of 911 Ppt on french culture in french language Ppt on electricity for class 10th date Ppt on acid-base titration equivalence point