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!

 - review quizgrades.py


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.

 - GOALS: get main() written, decide on data structures, how data
   will go to/from each function, write function stubs so we can
   write and test each function, one at-a-time!


YOUR TURN:

 - here's my design for a grade-reading program: /home/jk/inclass/readgradesfromfile.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]

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

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 by adding test code in main():

  print findMax([9,0,7,4])
  print findMax([9,0,700,4])
  print findMax([2,2,2,2,2,2])


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