WEEK08: more top-down design
---------------------------------------------------------------
F: QUIZ 4, last TDD example
30-minute QUIZ 4
LAST TDD EXAMPLE:
$ python madlib.py
<noun> computer
<adjective> slippery
<plural noun> dogs
<noun> car
Shall I compare thee to a summer's computer?
Thou art more lovely and more slippery:
Rough winds do shake the darling dogs of May,
And summer's lease hath all too short a car.
$ cat ml-summerday.txt
Shall I compare thee to a summer's <noun>
Thou art more lovely and more <adjective>
Rough winds do shake the darling <plural noun> of May,
And summer's lease hath all too short a <noun>
- you can assume only 1 or 0 <tags> per line
- let's do the main function together:
* open ml-summerday.txt, read each line into a python list
* for each line in the list:
> find each "less than" and "greater than" char
> use slicing to pull out what's between them
> ask user for that (noun, verb, etc)
> put together new line using user's input + old line
* print out new lines
Here's my design:
def main():
lines = readFile("ml-summerday.txt")
newlines = askUser(lines)
for line in newlines:
print line
################################################
def askUser(lines):
"""
find <tags> in each line, ask user for what's inside,
return new list of lines with <tags>'s replaced
"""
newlist = []
return newlist
################################################
def readFile(filename):
"""opens file, reads txt into list of lines"""
return ["line one <noun> "line two <adjective>
NOTE: this file runs! Now I can implement each function
and test them as I implement them
- python help:
>>> help(str)
>>> line = "this is a <adjective> car"
>>> line.index("<")
10
>>> line.index(">")
20
>>> line[11:20]
'adjective'
>>>