WEEK07: File I/O, top-down design
---------------------------------------------------------------
 W: start top-down design

ANNOUNCEMENTS:
 - Lab 7 due after break...want top-down design turned in first!


TOP-DOWN DESIGN:

  Top Down Design is a problem solving technique where you:

   1. Start with General Description of Problem
   2. Break it into several high-level steps
   3. Iteratively break the steps into smaller steps until you have 
      steps that are easy to solve 

 - just like writing a paper...start with an outline, then fill
   in the second-level details, and so on until you can start
   writing each section/function

 - for example, writing a wheel-of-fortune game might start
   with something like this:

        main:     read phrases from file
                  pick one phrase for the game
                  play the game, with the chosen phrase
                  output the results
   
   Each one of those could be a single function, or may need to
   be broken down into multiple functions. If you think one of
   the above should be a single function, sketch it out (what
   arguments would it need, what would it do and how, and what
   should it return). If you think one of the above (ex: play the
   game) will be more than one function, do another level of 
   top-down design on that.


YOUR TURN:

 - here's my design for the grade-reading program we talked about
   last time: /home/jk/inclass/designReadGrades.py

"""
read grades from file, calc average, min, max, etc...
J. Knerr
Spring 2011
"""
# ---------------------------------------------- #

def main():
  """
  get grades, calculate stats, output results
  """
  grades = readGrades("grades.txt")
  ave = findAve(grades)
  mingrade = findMin(grades)
  maxgrade = findMax(grades)
  print "\ngrades: ", grades
  print "ave grade = %0.1f" % (ave)
  print "min grade = %0.1f" % (mingrade)
  print "max grade = %0.1f" % (maxgrade)

# ---------------------------------------------- #

def findMax(glist):
  """
  given list of grades, find and return highest grade
  """
  maxgrade = -1

  return maxgrade

# ---------------------------------------------- #

def findMin(glist):
  """
  given list of grades, find and return lowest grade
  """
  return -1

# ---------------------------------------------- #

def findAve(glist):
  """
  given list of grades, calc and return ave grade
  """
  return -1

# ---------------------------------------------- #

def readGrades(fname):
  """
  given a file name, open the file, then read each line
  from the file and get the grade. convert grade to a float
  and add to the list of grades.

  grade file has this format:

name1: grade1
name2: grade2
name3: grade3

  use split(":") to get the name and grade from each line of the file
  """
  return [3,4,5]

# ---------------------------------------------- #

# this is how python knows to run your code when
# executed (python prog.py), but NOT run your code
# when imported: from prog import *

if __name__ == "__main__":
	main()


 NOTE: it doesn't do anything yet, but the structure and design are
 all specified. The program runs, and I can start working on adding
 each function. I can also TEST each function as I write them. Once
 I know they are working, I can move on to the next function.

 TEST AS YOU GO!!!

 - suppose I add the findMax function:

def findMax(glist):
  """
  given list of grades, find and return highest grade
  """
  maxgrade = glist[0]
  for g in glist:
    if g > maxgrade:
      maxgrade = g

  return maxgrade

 - Now I can test just this function to make sure it works:

$ python
>>> from designReadGrades import findMax
>>> findMax([9,0,7,4])
9
>>> findMax([9,0,700,4])
700
>>> findMax([2,2,2,2])
2
>>>
>>> help(findMax)
Help on function findMax in module designReadGrades:

findMax(glist)
    given list of grades, find and return highest grade

 - this is called UNIT TESTING...test each unit (function) as
   you write it. Only move on once you know it is working...


STRING METHODS AND FILE I/O:

 - given a file called "grades.txt" which contains these lines:

lisa     :95
jeff     :35
charlie  :88
jonathan :97
rich     :77
andy     :70
doug     :55
betsy    :100
amanda   :99

 how do I read the grades???

 - here's my readGrades function:

def readGrades(fname):
  """
  given a file name, open the file, then read each line
  from the file and get the grade. convert grade to a float
  and add to the list of grades.

  grade file has this format:

name1: grade1
name2: grade2
name3: grade3

  use split(":") to get the name and grade from each line of the file
  """
  glist = []
  myfile = open(fname, "r")
  for line in myfile:
    name, grade = line.split(":")
    glist.append(float(grade))

  myfile.close()
  return glist