ipre 2008 workshop advanced python

32
June 11-13 20 08 www.roboteducation.org 1 IPRE 2008 WORKSHOP Advanced Python Keith O’Hara [email protected] School of Interactive Computing Georgia Tech

Upload: fareeda-hormizd

Post on 04-Jan-2016

50 views

Category:

Documents


4 download

DESCRIPTION

IPRE 2008 WORKSHOP Advanced Python. Keith O’Hara [email protected] School of Interactive Computing Georgia Tech. Python. Interactive Cross-platform Looks like pseudo-code Indentation matters Support for functional programming Large collection of libraries - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 1

IPRE 2008 WORKSHOP

Advanced Python

Keith O’[email protected]

School of Interactive Computing Georgia Tech

Page 2: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 2

Python Interactive Cross-platform Looks like pseudo-code Indentation matters Support for functional

programming Large collection of libraries Object system built on top of

functions Syntax support for common

data structures Used by Google, Yahoo,

NASA among others

Page 3: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 3

Interacting with the User with Myro ask(“prompt”) prompt user with dialog box and return response askQuestion(“prompt”, [choices])

prompt user with dialog box and return response raw_input(“prompt”)

prompt user with text interface and return response

Page 4: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 4

Myro and Files

pickAFile() prompts user for file pickAFolder() prompts user for folder

open(file, mode) open file with mode (`r’,`w’,`a’) file.read(); file.readlines() read file and return contents as a string (list of

strings) string.split() Turn string into a list of strings based on whitespace

from myro import *f = open(pickAFile(), ‘r’)data = f.read().split()print dataf.close()

from myro import *f = open(pickAFile(), ‘r’)data = f.read().split()print dataf.close()

Page 5: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 5

Myro Control Flow Constructs wait(seconds) pause for seconds before executing next

statement currentTime() returns the current time in seconds while timeRemaining(seconds): print "running..." execute loop for at least seconds for s in timer(seconds): print "running for", s, "..." loop for at least seconds; s is the value of the

timer

Page 6: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 6

Myro Random Utilities

flipCoin() returns ‘heads’ or ‘tails’ with equal probability heads() returns True 50% of the time tails() returns True 50% of the time pickOne(v1, v2, …, vn)

chose one value from the values with equal probability randomNumber() return a uniform random number between [0,1)

Page 7: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 7

random module

random. uniform(low, high) Float: low <= x < high

random.randint(low,high) Integer: low <= x <= high

random.choice(sequence) Random element of a sequence

random.shuffle(sequence) Randomly shuffle a sequence

Page 8: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 8

Scribbler Music

Random notes

Use choice function

import random

notes = [440, 494, 523, 587, 659]

for i in range(10):dur = random.random()freq = random.choice(notes)beep(dur, freq)

Page 9: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 9

Tuples Immutable sequences (a, b) Multiple return values return a, b Tuple Assignment a, b = b, a zip - useful for iterating through multiple

sequences zip ([a1, a2, … an], [b1, b2, …, bn]) -->

[(a1, b1), (a2, b2), …, (an, bn)]names = [“al”, “bob”, “ted”]grades = [90, 88, 98]

for n,g in zip(names, grades):print n, “got a”, g

names = [“al”, “bob”, “ted”]grades = [90, 88, 98]

for n,g in zip(names, grades):print n, “got a”, g

Page 10: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 10

Set

Unordered collection of unique elements

a = set(‘abca’)b = set(‘cbcbcb’)

‘a’ in a‘a’ in b

a - b # set differencea & b # set intersectiona | b # set uniona ^ b # exclusive or

a = set(‘abca’)b = set(‘cbcbcb’)

‘a’ in a‘a’ in b

a - b # set differencea & b # set intersectiona | b # set uniona ^ b # exclusive or

Page 11: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 11

Dictionaries

Associative Arrays

Collection of key-value pairs dict[key] =

value

{} is the empty dictionary

notes = {‘a’: 440, ‘b’:494 }notes[‘c’] = 523

beep(1, notes[‘a’])notes[‘a’] = 440 * 2

for key in notes:print ‘Beeping’, keybeep(1, notes[key])

notes = {‘a’: 440, ‘b’:494 }notes[‘c’] = 523

beep(1, notes[‘a’])notes[‘a’] = 440 * 2

for key in notes:print ‘Beeping’, keybeep(1, notes[key])

Page 12: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 12

Functions

Default values

Keyword arguments

Multiple return values

def mult (x, y=2):return x * y

def div(x, y=1):return x/y, x%y

print mult(1,3)print mult(1)print mult(x=3, y=2)quo,rem = div (5, 2)

def mult (x, y=2):return x * y

def div(x, y=1):return x/y, x%y

print mult(1,3)print mult(1)print mult(x=3, y=2)quo,rem = div (5, 2)

Page 13: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 13

Getting Functional

Functions are first class citizens in python

Lambdas - nameless or anonymous functions Limited to one

expression

Higher order functions Functions that take or

return functions

def mult (x, y):return x * y

mult2 = lambda x,y: x*ymult3 = mult

def makeMult():return lambda x,y:x*y

print mult2(1,3)print mult3(2,3)print makeMult()(4,2)

def mult (x, y):return x * y

mult2 = lambda x,y: x*ymult3 = mult

def makeMult():return lambda x,y:x*y

print mult2(1,3)print mult3(2,3)print makeMult()(4,2)

Page 14: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 14

Filter/Map/Reduce in Python

Lots of computations can be described as filter, map, and reduce operations. Filter - pick items from a list Map - perform an operation on each item of a

list, returning a new list Reduce - combine all the items in a list in

some way Shortcut for a for-loop and append/delete

Google has extended this concept to multiple computers MapReduce - terabytes of data!

Page 15: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 15

Filter

Higher-order function A predicate to decide if

an item is “filtered” through

The sequence to be filtered

Returns the filtered sequence

def even(x):if (x % 2) == 0:

return Trueelse:

return False

filter(even, [1, 2, 3, 4, 6])

def even(x):if (x % 2) == 0:

return Trueelse:

return False

filter(even, [1, 2, 3, 4, 6])

#oddsfilter(lambda x: x % 2, [1, 2, 3, 4, 5, 6])#oddsfilter(lambda x: x % 2, [1, 2, 3, 4, 5, 6])

Page 16: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 16

Map

Higher-order function A function to apply to

each item Sequence to be

“mapped” Returns a sequence

combining the results of the maps

(possibly an empty list) Can take multiple

sequences and a function of multiple arguments

def upOctave(note):return note *2

def play(note):beep (.1, note)

notes = [440, 466, 494, 523]

map(play, notes)newNotes = map(upOctave, notes)map(play, newNotes)

def upOctave(note):return note *2

def play(note):beep (.1, note)

notes = [440, 466, 494, 523]

map(play, notes)newNotes = map(upOctave, notes)map(play, newNotes)

Page 17: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 17

Implementing a simple map

def ourMap(f, lst):newlst = []for x in lst:

newx = f(x)newlst.append(newx)

return newlst

def ourMap(f, lst):newlst = []for x in lst:

newx = f(x)newlst.append(newx)

return newlst

Page 18: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 18

Reduce Higher-order function

A function to accumulate the item in some way

The sequence to be “reduced” Returns the accumulated sequence

def mul(x, y):return x * y

data = [88, 90, 91, 66, 100]

geomean = reduce(mul, data)**(1.0/len(data))arimean = reduce(add, data)* (1.0/len(data))

def mul(x, y):return x * y

data = [88, 90, 91, 66, 100]

geomean = reduce(mul, data)**(1.0/len(data))arimean = reduce(add, data)* (1.0/len(data))

Page 19: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 19

Dot Product

Vector dot product using map/reduce

v1 = [8, 9, 1, 6, 0]v2 = [1, 2, 3, 4, 5]dp = reduce(add, map(mul, v1, v2))

v1 = [8, 9, 1, 6, 0]v2 = [1, 2, 3, 4, 5]dp = reduce(add, map(mul, v1, v2))

Page 20: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 20

Gather Data with the Scribbler Program to gather

and analyze data about light levels of the room Average Minimum Maximum Variance

Light Sensors

Page 21: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 21

Computing Statistics

Compute statistics about light levels of the room Average Minimum Maximum Variance

Use list to store data Use map/reduce to

write this program without any loops!

Assume max and min don’t exist already

data = getLightData(10)

avg = computeAverage(data)min = computeMinimum(data)max = computeMaximum(data)var = computeVariance(data)

data = getLightData(10)

avg = computeAverage(data)min = computeMinimum(data)max = computeMaximum(data)var = computeVariance(data)

Page 22: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 22

Putting it together

def max(x,y):if x > y: return xelse: return y

def moveAndSense(x):turnRight(1, .1)return getLight(“center”)

# get 10 readings from sensorreadings = map(moveAndSense, range(10))

avg = reduce(lambda x, y: x + y, readings)/ len(readings)maximum = reduce(max, readings)minimum = reduce(min, readings)

def max(x,y):if x > y: return xelse: return y

def moveAndSense(x):turnRight(1, .1)return getLight(“center”)

# get 10 readings from sensorreadings = map(moveAndSense, range(10))

avg = reduce(lambda x, y: x + y, readings)/ len(readings)maximum = reduce(max, readings)minimum = reduce(min, readings)

Page 23: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 23

List Comprehensions

From definition of mathematical sets

Subsumes map and filter in many ways

“queries” on a sequence

lst = range(7) # [0,1,2,3,4,5,6]

#oddsfilter(lambda x: x % 2, lst)

[x for x in lst if x % 2]

lst = range(7) # [0,1,2,3,4,5,6]

#oddsfilter(lambda x: x % 2, lst)

[x for x in lst if x % 2]

<expr> for <target> in <iterable> <lc-clauses><expr> for <target> in <iterable> <lc-clauses>

Page 24: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 24

Dot Product

Vector dot product using list comprehensions

v1 = [8, 9, 1, 6, 0]v2 = [1, 2, 3, 4, 5]dp = sum(x * y for x,y in zip(v1, v2))

v1 = [8, 9, 1, 6, 0]v2 = [1, 2, 3, 4, 5]dp = sum(x * y for x,y in zip(v1, v2))

v1 = [8, 9, 1, 6, 0]v2 = [1, 2, 3, 4, 5]dp = reduce(add, map(mul, v1, v2))

v1 = [8, 9, 1, 6, 0]v2 = [1, 2, 3, 4, 5]dp = reduce(add, map(mul, v1, v2))

Page 25: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 25

List Comp. and getPixels()

[getX(px) for px in getPixels(pic) if getGreen(pix) > 100]

[getX(px) for px in getPixels(pic) if getGreen(pix) > 100]

# shuffling the pixels

pxs = [px for px in getPixels(pic)]random.shuffle(pxs)

# shuffling the pixels

pxs = [px for px in getPixels(pic)]random.shuffle(pxs)

Much faster than for-loop equivalent

Page 26: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 26

Using Objects Already used

objects without knowing it Picture Object

Controlling multiple robots from one python script

Use dot notation similar to java and c++

from myro import *r1 = Scribbler(‘COM4’)r2 = Scribbler(‘COM5’)r1.beep(1, 440)r2.beep(1, 483)

from myro import *r1 = Scribbler(‘COM4’)r2 = Scribbler(‘COM5’)r1.beep(1, 440)r2.beep(1, 483)

obj = CLASSNAME(PARAMS)obj.METHOD(PARAMS)print obj.ATTRIBUTE

obj = CLASSNAME(PARAMS)obj.METHOD(PARAMS)print obj.ATTRIBUTE

Page 27: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 27

Creating Objects

class CLASSNAME (PARENT1, PARENT2, …):CLASSVARIABLE = INITIAL_VALUE

def __init__(self, PARAMS): self.ATTRIBUTE = INITIAL_VALUE

def CLASSMETHOD(PARAMS):CLASSVARIABLE

def METHOD(self, PARAMS):self.ATTRIBUTE

obj = CLASSNAME(PARAMS)obj.METHOD(PARAMS)print obj.ATTRIBUTE

class CLASSNAME (PARENT1, PARENT2, …):CLASSVARIABLE = INITIAL_VALUE

def __init__(self, PARAMS): self.ATTRIBUTE = INITIAL_VALUE

def CLASSMETHOD(PARAMS):CLASSVARIABLE

def METHOD(self, PARAMS):self.ATTRIBUTE

obj = CLASSNAME(PARAMS)obj.METHOD(PARAMS)print obj.ATTRIBUTE

constructor

(multiple)inheritance

instancevariable

explicitly passed‘this’ reference

implicitly pass‘this’ reference

Everything is public and virtual!

Page 28: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 28

Modules and Namespaces

import myModulemyModule.f()print myModule.v

import myModulemyModule.f()print myModule.v

Unit of source code organization e.g. myro

File Name = Module Name Assume module is in current

directory or in PYTHONPATH (sys.path)

Runs script when imported dir(module) lists things in

modulemyModule.py

v = 3 + 2

def f(): print “myModule:f()”

myModule.py

v = 3 + 2

def f(): print “myModule:f()”

from myModule import f,vf()print v

from myModule import f,vf()print v

from myModule import *f()print v

from myModule import *f()print v

Page 29: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 29

Other Modules Niceties Every module has a

__name__ Use conditional execution

depending on whether script is imported or run explicitly

Comment in function enclosed with three quotes is a pydoc comment

help(myModule)

myModule.py

v = 3 + 2

def f(): ```prints a msg ’’’ print “myModule:f()”

if __name__ == “__main__”:print vf()

myModule.py

v = 3 + 2

def f(): ```prints a msg ’’’ print “myModule:f()”

if __name__ == “__main__”:print vf()

Page 30: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 30

Nested Modules

import myLibs.myModulemyModule.f()print myModule.v

import myLibs.myModulemyModule.f()print myModule.v

Modules nested in directories

Use ‘.’ to indicate containment

__init__.py file in the directory to signal module-nessmylibs/

__init__.py myModule.py v = 3 + 2

def f(): print “myModule:f()”

mylibs/ __init__.py myModule.py v = 3 + 2

def f(): print “myModule:f()”

from myModule import f,vf()print v

from myModule import f,vf()print v

from myModule import *f()print v

from myModule import *f()print v

Page 31: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 31

sys - A very useful module sys has lots of useful stuff!

How can you find out about it?

sys.argv - list of arguments to script

sys.path - path to look for modules

sys.modules - loaded modules

sys.version sys.platform

args.py

import sys

if __name__ == “__main__”:print sys.argv

args.py

import sys

if __name__ == “__main__”:print sys.argv

Page 32: IPRE 2008 WORKSHOP Advanced Python

June 11-13 2008

www.roboteducation.org 32

Other Standard Modules

math zlib, csv, pickle, optparse urllib2, thread, socket, cgi tkinter, wave http://docs.python.org/lib/