Download presentation
Presentation is loading. Please wait.
1
June 11-13 2008www.roboteducation.org1 IPRE 2008 WORKSHOP Advanced Python Keith O’Hara keith.ohara@gatech.edu School of Interactive Computing Georgia Tech
2
June 11-13 2008www.roboteducation.org2 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
3
June 11-13 2008www.roboteducation.org3 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
4
June 11-13 2008www.roboteducation.org4 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 data f.close() from myro import * f = open(pickAFile(), ‘r’) data = f.read().split() print data f.close()
5
June 11-13 2008www.roboteducation.org5 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
6
June 11-13 2008www.roboteducation.org6 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)
7
June 11-13 2008www.roboteducation.org7 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
8
June 11-13 2008www.roboteducation.org8 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)
9
June 11-13 2008www.roboteducation.org9 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
10
June 11-13 2008www.roboteducation.org10 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 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
11
June 11-13 2008www.roboteducation.org11 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]) 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])
12
June 11-13 2008www.roboteducation.org12 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)
13
June 11-13 2008www.roboteducation.org13 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*y mult3 = 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*y mult3 = mult def makeMult(): return lambda x,y:x*y print mult2(1,3) print mult3(2,3) print makeMult()(4,2)
14
June 11-13 2008www.roboteducation.org14 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!
15
June 11-13 2008www.roboteducation.org15 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]) 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]) #odds filter(lambda x: x % 2, [1, 2, 3, 4, 5, 6])
16
June 11-13 2008www.roboteducation.org16 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)
17
June 11-13 2008www.roboteducation.org17 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
18
June 11-13 2008www.roboteducation.org18 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))
19
June 11-13 2008www.roboteducation.org19 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))
20
June 11-13 2008www.roboteducation.org20 Gather Data with the Scribbler Program to gather and analyze data about light levels of the room Average Minimum Maximum Variance Light Sensors
21
June 11-13 2008www.roboteducation.org21 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)
22
June 11-13 2008www.roboteducation.org22 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) 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)
23
June 11-13 2008www.roboteducation.org23 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] 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] for in
24
June 11-13 2008www.roboteducation.org24 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))
25
June 11-13 2008www.roboteducation.org25 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
26
June 11-13 2008www.roboteducation.org26 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
27
June 11-13 2008www.roboteducation.org27 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 instance variable explicitly passed ‘this’ reference implicitly pass ‘this’ reference Everything is public and virtual!
28
June 11-13 2008www.roboteducation.org28 Modules and Namespaces import myModule myModule.f() print myModule.v 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 myModule.py v = 3 + 2 def f(): print “myModule:f()” myModule.py v = 3 + 2 def f(): print “myModule:f()” from myModule import f,v f() print v from myModule import f,v f() print v from myModule import * f() print v from myModule import * f() print v
29
June 11-13 2008www.roboteducation.org29 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 v f() myModule.py v = 3 + 2 def f(): ```prints a msg ’’’ print “myModule:f()” if __name__ == “__main__”: print v f()
30
June 11-13 2008www.roboteducation.org30 Nested Modules import myLibs.myModule myModule.f() print myModule.v 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 mylibs/ __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,v f() print v from myModule import f,v f() print v from myModule import * f() print v from myModule import * f() print v
31
June 11-13 2008www.roboteducation.org31 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
32
June 11-13 2008www.roboteducation.org32 Other Standard Modules math zlib, csv, pickle, optparse urllib2, thread, socket, cgi tkinter, wave http://docs.python.org/lib/
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.