IPRE 2008 WORKSHOP Advanced Python Keith OHara keith

  • Slides: 32
Download presentation
IPRE 2008 WORKSHOP Advanced Python Keith O’Hara keith. ohara@gatech. edu School of Interactive Computing

IPRE 2008 WORKSHOP Advanced Python Keith O’Hara keith. ohara@gatech. edu School of Interactive Computing Georgia Tech June 11 -13 2008 www. roboteducation. org 1

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

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 June 11 -13 2008 www. roboteducation. org 2

Interacting with the User with Myro § ask(“prompt”) prompt user with dialog box and

Interacting with the User with Myro § ask(“prompt”) prompt user with dialog box and return response § ask. Question(“prompt”, [choices]) prompt user with dialog box and return response § raw_input(“prompt”) prompt user with text interface and return response June 11 -13 2008 www. roboteducation. org 3

Myro and Files § pick. AFile() prompts user for file § pick. AFolder() prompts

Myro and Files § pick. AFile() prompts user for file § pick. AFolder() prompts user folder from myro import * f = open(pick. AFile(), ‘r’) data = f. read(). split() print data f. close() § 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 June 11 -13 2008 www. roboteducation. org 4

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

Myro Control Flow Constructs § wait(seconds) pause for seconds before executing next statement § current. Time() returns the current time in seconds § while time. Remaining(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 June 11 -13 2008 www. roboteducation. org 5

Myro Random Utilities § flip. Coin() returns ‘heads’ or ‘tails’ with equal probability §

Myro Random Utilities § flip. Coin() returns ‘heads’ or ‘tails’ with equal probability § heads() returns True 50% of the time § tails() returns True 50% of the time § pick. One(v 1, v 2, …, vn) chose one value from the values with equal probability § random. Number() return a uniform random number between [0, 1) June 11 -13 2008 www. roboteducation. org 6

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

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 June 11 -13 2008 www. roboteducation. org 7

Scribbler Music § Random notes § Use choice function June 11 -13 2008 import

Scribbler Music § Random notes § Use choice function June 11 -13 2008 import random notes = [440, 494, 523, 587, 659] for i in range(10): dur = random() freq = random. choice(notes) beep(dur, freq) www. roboteducation. org 8

Tuples § Immutable sequences (a, b) § Multiple return values return a, b §

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 ([a 1, a 2, … an], [b 1, b 2, …, bn]) --> [(a 1, b 1), (a 2, b 2), …, (an, bn)] names = [“al”, “bob”, “ted”] grades = [90, 88, 98] for n, g in zip(names, grades): print n, “got a”, g June 11 -13 2008 www. roboteducation. org 9

Set § Unordered collection of unique elements a = set(‘abca’) b = set(‘cbcbcb’) ‘a’

Set § Unordered collection of unique elements a = set(‘abca’) b = set(‘cbcbcb’) ‘a’ in a ‘a’ in b a a June 11 -13 2008 & | ^ b b # # set difference set intersection set union exclusive or www. roboteducation. org 10

Dictionaries § Associative Arrays § Collection of key -value pairs § dict[key] = value

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’, key beep(1, notes[key]) June 11 -13 2008 www. roboteducation. org 11

Functions § Default values § Keyword arguments § Multiple return values June 11 -13

Functions § Default values § Keyword arguments § Multiple return values June 11 -13 2008 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) www. roboteducation. org 12

Getting Functional § Functions are first class citizens in python § Lambdas - nameless

Getting Functional § Functions are first class citizens in python § Lambdas - nameless or anonymous functions def mult (x, y): return x * y mult 2 = lambda x, y: x*y mult 3 = mult § Limited to one expression § Higher order functions def make. Mult(): return lambda x, y: x*y § Functions that take or return functions print mult 2(1, 3) print mult 3(2, 3) print make. Mult()(4, 2) June 11 -13 2008 www. roboteducation. org 13

Filter/Map/Reduce in Python § Lots of computations can be described as filter, map, and

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 § Map. Reduce - terabytes of data! June 11 -13 2008 www. roboteducation. org 14

Filter § Higher-order function § A predicate to decide if an item is “filtered”

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 True else: return False filter(even, [1, 2, 3, 4, 6]) #odds filter(lambda x: x % 2, [1, 2, 3, 4, 5, 6]) June 11 -13 2008 www. roboteducation. org 15

Map § Higher-order function § A function to apply to each item § Sequence

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) def up. Octave(note): return note *2 def play(note): beep (. 1, note) notes = [440, 466, 494, 523] map(play, notes) new. Notes = map(up. Octave, notes) map(play, new. Notes) § Can take multiple sequences and a function of multiple arguments June 11 -13 2008 www. roboteducation. org 16

Implementing a simple map def our. Map(f, lst): newlst = [] for x in

Implementing a simple map def our. Map(f, lst): newlst = [] for x in lst: newx = f(x) newlst. append(newx) return newlst June 11 -13 2008 www. roboteducation. org 17

Reduce § Higher-order function § A function to accumulate the item in some way

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)) June 11 -13 2008 www. roboteducation. org 18

Dot Product § Vector dot product using map/reduce v 1 = [8, 9, 1,

Dot Product § Vector dot product using map/reduce v 1 = [8, 9, 1, 6, 0] v 2 = [1, 2, 3, 4, 5] dp = reduce(add, map(mul, v 1, v 2)) June 11 -13 2008 www. roboteducation. org 19

Gather Data with the Scribbler § Program to gather and analyze data about light

Gather Data with the Scribbler § Program to gather and analyze data about light levels of the room § § Average Minimum Maximum Variance Light Sensors June 11 -13 2008 www. roboteducation. org 20

Computing Statistics § Compute statistics about light levels of the room § § Average

Computing Statistics § Compute statistics about light levels of the room § § Average Minimum Maximum Variance data = get. Light. Data(10) avg min max var = = compute. Average(data) compute. Minimum(data) compute. Maximum(data) compute. Variance(data) § Use list to store data § Use map/reduce to write this program without any loops! § Assume max and min don’t exist already June 11 -13 2008 www. roboteducation. org 21

Putting it together def max(x, y): if x > y: return x else: return

Putting it together def max(x, y): if x > y: return x else: return y def move. And. Sense(x): turn. Right(1, . 1) return get. Light(“center”) # get 10 readings from sensor readings = map(move. And. Sense, range(10)) avg = reduce(lambda x, y: x + y, readings)/ len(readings) maximum = reduce(max, readings) minimum = reduce(min, readings) June 11 -13 2008 www. roboteducation. org 22

List Comprehensions § From definition of mathematical sets § Subsumes map and filter in

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] #odds filter(lambda x: x % 2, lst) [x for x in lst if x % 2] <expr> for <target> in <iterable> <lc-clauses> June 11 -13 2008 www. roboteducation. org 23

Dot Product § Vector dot product using list comprehensions v 1 = [8, 9,

Dot Product § Vector dot product using list comprehensions v 1 = [8, 9, 1, 6, 0] v 2 = [1, 2, 3, 4, 5] dp = sum(x * y for x, y in zip(v 1, v 2)) v 1 = [8, 9, 1, 6, 0] v 2 = [1, 2, 3, 4, 5] dp = reduce(add, map(mul, v 1, v 2)) June 11 -13 2008 www. roboteducation. org 24

List Comp. and get. Pixels() # shuffling the pixels pxs = [px for px

List Comp. and get. Pixels() # shuffling the pixels pxs = [px for px in get. Pixels(pic)] random. shuffle(pxs) [get. X(px) for px in get. Pixels(pic) if get. Green(pix) > 100] � § Much faster than for-loop equivalent June 11 -13 2008 www. roboteducation. org 25

Using Objects § Already used objects without knowing it § Picture Object § Controlling

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++ June 11 -13 2008 from myro import * r 1 = Scribbler(‘COM 4’) r 2 = Scribbler(‘COM 5’) r 1. beep(1, 440) r 2. beep(1, 483) obj = CLASSNAME(PARAMS) obj. METHOD(PARAMS) print obj. ATTRIBUTE www. roboteducation. org 26

Creating Objects (multiple) inheritance class CLASSNAME (PARENT 1, PARENT 2, …): CLASSVARIABLE = INITIAL_VALUE

Creating Objects (multiple) inheritance class CLASSNAME (PARENT 1, PARENT 2, …): CLASSVARIABLE = INITIAL_VALUE def __init__(self, PARAMS): self. ATTRIBUTE = INITIAL_VALUE explicitly passed ‘this’ reference constructor def CLASSMETHOD(PARAMS): CLASSVARIABLE def METHOD(self, PARAMS): self. ATTRIBUTE obj = CLASSNAME(PARAMS) obj. METHOD(PARAMS) print obj. ATTRIBUTE instance variable Everything is public and virtual! implicitly pass ‘this’ reference June 11 -13 2008 www. roboteducation. org 27

Modules and Namespaces § Unit of source code organization § e. g. myro §

Modules and Namespaces § 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 module import my. Module. f() print my. Module. v from my. Module import f, v f() print v from my. Module import * f() print v my. Module. py v = 3 + 2 def f(): print “my. Module: f()” June 11 -13 2008 www. roboteducation. org 28

Other Modules Niceties § Every module has a __name__ § Use conditional execution depending

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(my. Module) June 11 -13 2008 my. Module. py v = 3 + 2 def f(): ```prints a msg ’’’ print “my. Module: f()” if __name__ == “__main__”: print v f() www. roboteducation. org 29

Nested Modules § Modules nested in directories § Use ‘. ’ to indicate containment

Nested Modules § Modules nested in directories § Use ‘. ’ to indicate containment § __init__. py file in the directory to signal module-ness from my. Module import f, v f() print v mylibs/ __init__. py my. Module. py v = 3 + 2 def f(): print “my. Module: f()” June 11 -13 2008 import my. Libs. my. Module. f() print my. Module. v from my. Module import * f() print v www. roboteducation. org 30

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

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 args. py § sys. platform import sys if __name__ == “__main__”: print sys. argv June 11 -13 2008 www. roboteducation. org 31

Other Standard Modules § § math zlib, csv, pickle, optparse urllib 2, thread, socket,

Other Standard Modules § § math zlib, csv, pickle, optparse urllib 2, thread, socket, cgi tkinter, wave § http: //docs. python. org/lib/ June 11 -13 2008 www. roboteducation. org 32