Intro to
Programming
with Python

Fall 2012
course
navigation

Nov 13

old business

Discuss homework:

various python uses

image processing : http://www.gimp.org/docs/python/ within GUI app http://wiki.python.org/moin/ImageMagick command line 3D modeling and animation : http://www.blender.org/development/ (e.g. 2.6 Python API Reference) numerical scientific work : http://numpy.scipy.org games : http://wiki.python.org/moin/PythonGameLibraries audio : http://wiki.python.org/moin/Audio robots : htpp://pyrobotics.org : a python robot API, including simulators wikipedia: python robotics http://www.youtube.com/watch?v=3gHr3oAd_gk (a python robot) internet : network events : twistedmatrix.com web frameworks : django pylons, google app engine, etc (but you also need HTML, CSS, JavaScript, etc)

data structures

We've talked about lists (and the similar tuples) and dictionaries. These really become powerful when you start embedding them :
Look at some examples :
Specific example: the ATM machine
objects might be something like GUI - all interaction with user Data - file input/output Person - name, password Account - each pot of money, e.g. checking, savings Transaction - deposit or withdrawal or transfer with time
The Data object could put the information could be stored in a single python data structure, stored as a string in a file with pprint read in as a string and then eval() to put it into a variable, which could then be converted to, say, a list of Persons, each with a list of history Transactions and several Accounts.
The storage file might look like this :
[{'name': 'John Smith', 'password':'...', 'accounts': [ {'name':'checking', 'balance': 120.23 # or store as pennies}, {'name':'savings', 'balance':4000.00 ], 'transactions': [ {'date':..., 'amount':..., 'from':..., 'to':...}, {...}, ] }, { # similar for another person } ]
The the top level of the program might be something like storage = Data('stored_data.txt') people = storage.readPeople() gui = GUI(people) while True: person = gui.login() # ask for name/pass; return user if match while person.transaction(): storage.save() # update disk file
where several of these objects contain the others, and would call methods on some of the others to get the work done.
I'm imagining that Data contains a list of people, and so can use that to get a string to send to the file.
Likewise, each person contains the gui, so person.transaction() would
  1. use gui to ask about what to do,
  2. create a Transaction object and append it
  3. update the person's balances, and
  4. return False if user is done.

new business

Finish reading chapter 12 (if you haven't already).
Read chapter 13 : algorithms & recursion! (A bit of a preview of some ideas we'll do in more detail in the Algorithms course next term.)
We'll talk about the "search" part of that chapter on Thursday.
To get started in this direction, consider the following python code:
def print_it(x): print x def looper(i, stop_value, function): if i <= stop_value: function(i) looper(i+1, stop_value, function) loop(100, 110, print_it)
What does it do? And how is it doing it?
It turns out that this is an extremely powerful technique. It's very similar to mathematical induction.
Consider a data structure that looks like this:
root = {'name' : 'root', 'parent' : [] } branch = {'name' : 'branch', 'parent' : [root] }
We can build trees and/or graphs by having things like this that refer to each other. (Another sort of recursion, actually.)
Task 1: What would these things look like as python classes?
Task 2: Generalize this a bit so that it can work as a graph.
Task 3: Given some big collection of these with links to each other, write a program to count how many there are.
We worked on object oriented recursive approaches to trees and graphs in class; the code is attached below.
http://cs.marlboro.edu/ courses/ fall2012/python/ notes/ Nov_13
last modified Tuesday November 13 2012 12:10 pm EST

attachments [paper clip]

     name last modified size
   graph_node.py Nov 13 2012 12:10 pm 1.23kB    node.py Nov 13 2012 12:09 pm 992B