320 likes | 450 Views
IPRE 2008 WORKSHOP Advanced Python. Keith O’Hara keith.ohara@gatech.edu School of Interactive Computing Georgia Tech. Python. Interactive Cross-platform Looks like pseudo-code Indentation matters Support for functional programming Large collection of libraries
E N D
IPRE 2008 WORKSHOPAdvanced Python Keith O’Hara keith.ohara@gatech.edu School of Interactive Computing Georgia Tech www.roboteducation.org
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 www.roboteducation.org
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 www.roboteducation.org
Myro and Files from myro import * f = open(pickAFile(), ‘r’) data = f.read().split() print data f.close() • 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 www.roboteducation.org
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 www.roboteducation.org
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) www.roboteducation.org
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 www.roboteducation.org
Scribbler Music import random notes = [440, 494, 523, 587, 659] for i in range(10): dur = random.random() freq = random.choice(notes) beep(dur, freq) • Random notes • Use choice function www.roboteducation.org
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 www.roboteducation.org
Set • Unordered collection of unique elements a = set(‘abca’) b = set(‘cbcbcb’) ‘a’ in a ‘a’ in b a - b # set difference a & b # set intersection a | b # set union a ^ b # exclusive or www.roboteducation.org
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]) www.roboteducation.org
Functions 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) • Default values • Keyword arguments • Multiple return values www.roboteducation.org
Getting Functional def mult (x, y): return x * y mult2 = lambda x,y: x*y mult3 = mult def makeMult(): return lambda x,y:x*y print mult2(1,3) print mult3(2,3) print makeMult()(4,2) • 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 www.roboteducation.org
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! www.roboteducation.org
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]) www.roboteducation.org
Map 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) • 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 www.roboteducation.org
Implementing a simple map def ourMap(f, lst): newlst = [] for x in lst: newx = f(x) newlst.append(newx) return newlst www.roboteducation.org
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)) www.roboteducation.org
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)) www.roboteducation.org
Gather Data with the Scribbler • Program to gather and analyze data about light levels of the room • Average • Minimum • Maximum • Variance Light Sensors www.roboteducation.org
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) www.roboteducation.org
Putting it together def max(x,y): if x > y: return x else: return y def moveAndSense(x): turnRight(1, .1) return getLight(“center”) # get 10 readings from sensor readings = map(moveAndSense, range(10)) avg = reduce(lambda x, y: x + y, readings)/ len(readings) maximum = reduce(max, readings) minimum = reduce(min, readings) www.roboteducation.org
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> www.roboteducation.org
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 = reduce(add, map(mul, v1, v2)) www.roboteducation.org
List Comp. and getPixels() [getX(px) for px in getPixels(pic) if getGreen(pix) > 100] # shuffling the pixels pxs = [px for px in getPixels(pic)] random.shuffle(pxs) • Much faster than for-loop equivalent www.roboteducation.org
Using Objects from myro import * r1 = Scribbler(‘COM4’) r2 = Scribbler(‘COM5’) r1.beep(1, 440) r2.beep(1, 483) • Already used objects without knowing it • Picture Object • Controlling multiple robots from one python script • Use dot notation similar to java and c++ obj = CLASSNAME(PARAMS) obj.METHOD(PARAMS) print obj.ATTRIBUTE www.roboteducation.org
Creating Objects (multiple) inheritance 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 explicitly passed ‘this’ reference instance variable Everything is public and virtual! implicitly pass ‘this’ reference www.roboteducation.org
Modules and Namespaces import myModule myModule.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 module from myModule import f,v f() print v from myModule import * f() print v myModule.py v = 3 + 2 def f(): print “myModule:f()” www.roboteducation.org
Other Modules Niceties myModule.py v = 3 + 2 def f(): ```prints a msg ’’’ print “myModule:f()” if __name__ == “__main__”: print v f() • 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) www.roboteducation.org
Nested Modules import myLibs.myModule myModule.f() print myModule.v • Modules nested in directories • Use ‘.’ to indicate containment • __init__.py file in the directory to signal module-ness from myModule import f,v f() print v mylibs/ __init__.py myModule.py v = 3 + 2 def f(): print “myModule:f()” from myModule import * f() print v www.roboteducation.org
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 www.roboteducation.org
Other Standard Modules • math • zlib, csv, pickle, optparse • urllib2, thread, socket, cgi • tkinter, wave • http://docs.python.org/lib/ www.roboteducation.org