Simeon Franklin

Lab1

(Sample completions of all the labs below as well as various other python code samples demoed during the class can be found here)

Learning Objectives: iteration (for loop), lists

classmates1.py

Create a file called classmates1.py. Give it a list with the first names of each of your classmates and use a for loop to print out each name.

Lab2 (tuples, string formatting)

classmates2.py

  1. Create a list of tuples (firstname, lastname, role).
  2. Loop over the list of rows, printing "fname lname is a role" with the % formatting operator and tuple unpacking
  3. Be sure to create a docstring. Add a trivial doctest and make sure it runs with
    python -m doctest -v classmates2.py

Lab3 (conditionals, input, dicts)

classmates3.py

  1. Store list of classmates as a dict by first name => tuple row
  2. get input from the user, printing out the corresponding student if exists, error message if not found.

Lab 4 iteration (while loop), modifying dicts, sys.exit

classmates4.py

  1. wrap an infinite while loop around input
  2. If the name entered isn't found, solict input and add it to the dict.
  3. Exit if the user enters "Quit"

Lab 5 - refactoring, functions

classmates5.py

  1. Hide your the entry point to your script inside a function protected by an if __name__ == "__main__" block. Do your doctests run now?
  2. define a function "print_record" that outputs a row and use it for all output
  3. define a function get_record that solicits user input and returns a firstname, lastname, role tuple.

Lab 6a - dict methods, zip, *args, sets

classmates6.py

  1. modify classmates5.py to print the set of unique roles before soliciting user input. (Hint: use dict.values, zip with *args, and convert the resulting column to a set for uniqueness)

Lab 6b - =default, *args, **kwargs

classmates6.py

  1. modify classmates5.py to use functions with flexible calling signatures. Your print_record should accept either a record tuple or named arguments representing fname, lname, etc. This can be done at least 3 different ways: discuss the best function signature and the drawbacks of each approach.
  2. Be sure to write a doctest that exercises both valid cases of your print record function and at least one invalid case.

Lab 7

classmates7.py - main, filtering with loops

  1. Add code to present a menu of user choices and take appropriate action upon user input:

    1. List all
    2. Search by name
    3. Add record
    4. Filter by role
    5. Quit
  2. implement Menu item #4 with a for loop and if statement. Pass the filtered array to the function used for menu item #1 to output a list.
  3. Be sure to import your script (start python then 'import classmates7'. Notice no code runs. Inspect classmates7 with help and dir())

Lab 8 - fileIO, split, join, strip

classmates8.py

  1. Load data to your data structure from a csv textfile "classmates.csv" using the file builtin
  2. Add a menu option to save and write data to the same file

Lab 9 - optparse

classmates9.py

  1. add optparse argument handling to your script. Let the user supply a filename argument to read/write from.

Lab 10 - first class functions, namespace and scope

classmates10.py

  1. Use the filter builtin to implement filtering by role. How can a predefined function filter by user input? (Hint: a function can access the global namespace).
  2. Use function variables and a data structure to map menu selections to code blocks. Your main loop should have very few if statements in it.

Lab 11a - doctests

classmates11.py

  1. Add doctests to each of your utility functions. Refactor as necessary to doctest filtering by role. Does thinking about testing change the way you write code?
  2. Add a test flag to your optparse frontend to run your tests

Lab 11b - UnitTest

classmates11b.py

  1. Remove any doctests that do not add value to your documentation and add a tests.py file with UnitTest for your utility functions. Can you "UnitTest" your whole program? Should you? Does thinking about functional testing change the way you write code?
  2. Add a test flag to your optparse frontend to run your unit tests

Lab 12 - sort and higher order functions

classmates12.py Add a menu item to allow sorting by a field the user selects. Implement using the key parameter of the sorted function and create a keyfunction using a closure that captures the users input.

Refactoring the menu should be easy as it is a data structure, not a series of if statements.

Not hard enough? Solve a few more fp examples

Lab 13 - list comprehensions

classmates13.py Yay for a killer feature! Filtering and modifying in the same compact syntax - python's () sure get a workout. Discuss places to replace filter calls or for loops with a list comprehension.

Lab 14 - OOP

(classmates14.py)

"Rows" should be objects. Rows know how to sort themselves and print themselves. The addressbook should be an object that knows how to print itself and add new rows. Did switching to OOP improve your code?

Lab 15a - csv module

Change your file reading/parsing/writing code to use the CSV module instead. Be sure to see the PyMOTW article linked from the fundamentals page.

Lab 15b - json

JSON is a common data format in web applications. Add a menu option to import or export json data. Be sure to support reading a url (like http://simeonfranklin.com/labs/json.txt). How will you handle unexpected or missing fields?

Lab 15c - databases

Python has a spec for database drivers called DB-API that is used by most libraries for specific databases (Postgresql, MySql, etc). This means that you write very similar Python code even if connecting to different types of Databases. There are also ORM's and more advanced API's designed to hide the complexities of databases or smooth the incompatibilities between various flavors of SQL - but coverage of these topics is definitely beyond the scope of the class. Python does ship with a built-in database that is file based instead of server based and is appropriate for single-user applications. Convert your OOP version of classnames to read and write from an Sqlite database - see the PyMOTW sqlite coverage for details but this

import sqlite3
conn = sqlite3.connect("/tmp/foo.sqlite3")
conn.executescript("create table classmates(fname text primary key, lname text, role text);")
conn.execute("insert into classmates(fname, lname, role) values('simeon', 'franklin', 'instructor')")
cursor = conn.cursor()
cursor.execute("select * from classmates")
cursor.fetchall()

may be enough to get you started.