Intro to
Programming
(with Python)

Fall 2015
course
navigation

Nov 17

Questions about anything?

project thoughts

Blender meshes example
(Notes: must launch blender* app from command line to see errors. With blender 2.75, must comment out "use_abso" line to get this to work.)

chap 12 material

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 (in the Department of Reduncy of Reduncies)

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.

recursion

Finish reading chapter 12 (if you haven't already).
Then read chapter 13 : algorithms & recursion! (Cool stuff ...)
""" an example of counting using recursion. $ python count_recursion.py How many times? 5 1 2 3 4 5 Done. Exercise: write this without using any of the normal python looping constructs, "for", "while", those guys. """ count = input("How many times? ") def loop(i): """ print one line of the count """ print i if i < count: loop(i + 1) loop(1)

Look at this with the "visualize python" site (see the resources page) to see the allocated memory for each recursive call.
Try with count=2000 to see Python's recursive depth limit. (Python isn't very good at recursion ...)
If time allows : talk about recursive object structures, i.e. a Person object that contains a list of children ... which are also Person objects. This is powerful way to represent graphs, and allow recursive functions to traverse the graph. We explore these sorts of things in the "Algorithms" course.

We explored recursive data structures (i.e. linked graphs) with the family_tree.py program (attached), showing using both dictionaries and objects.
Coming next : a recursive function to explore the graph.
http://cs.marlboro.edu/ courses/ fall2015/python/ notes/ Nov_17
last modified Tuesday November 17 2015 11:30 am EST

attachments [paper clip]

     name last modified size
   family_tree.py Nov 17 2015 11:30 am 1.23kB