% I've added this file for plonking stuff that is currently out of
 % place in current chapters. E.g. the stuff on the return statement
 % doesn't belong in conditionals.tex

\chapter{No Home Yet}

\section{Keyboard input}

In Section \ref{sec-input} we were introduced to Python's built-in
functions that get input from the keyboard: \pythoninline{raw\_input}
and \pythoninline{input}. Now let's look at these again in greater
depth. When either of these functions are called, the program stops
and waits for the user to type something. When the user presses
\pythoninline{Return} or the \pythoninline{Enter} key, the program
resumes and \pythoninline{raw\_input} returns what the user typed as a
string: 
\begin{pythonInteractive}
>>> my_input = raw_input()
What are you waiting for?
>>> print my_input
What are you waiting for?
\end{pythonInteractive}

Before calling \pythoninline{raw\_input}, it is a good idea to print a
message telling the user what to input. This message is called a
\myidx{prompt}. We can supply a prompt as an argument to
\pythoninline{raw\_input}: 
\begin{pythonInteractive}
>>> name = raw_input("What...is your name? ")
What...is your name? Arthur, King of the Britons!
>>> print name
Arthur, King of the Britons!
\end{pythonInteractive}
Notice that the prompt is a string, so it must be enclosed in
quotation marks.
If we expect the response to be an integer, we can use the input
function which evaluates the response as a Python expression:
\begin{python}
prompt = "What...is the airspeed velocity of an unladen swallow?\n"
speed = input(prompt)
\end{python}
If the user types a string of digits, it is converted to an integer
and assigned to \pythoninline{speed}. Unfortunately, if the user types
characters 
that do not make up a valid Python expression, the program crashes:
\begin{pythonInteractive}
>>> speed = input(prompt)
What...is the airspeed velocity of an unladen swallow?
What do you mean, an African or a European swallow?
...
SyntaxError: invalid syntax
\end{pythonInteractive}
In the last example, if the user had made the response a valid Python
expression by putting quotes around it, it would not have given an
error:
\begin{pythonInteractive}
>>> speed = input(prompt)
What...is the airspeed velocity of an unladen swallow?
"What do you mean, an African or a European swallow?"
>>> speed
'What do you mean, an African or a European swallow?'
>>>
\end{pythonInteractive}
To avoid this kind of error, it is a good idea to use
\pythoninline{raw\_input} to
get a string and then use conversion commands to convert it to other
types.


\section{Glossary}
\begin{description}
\item[prompt:]A visual cue that tells the user to input data.
\item[type conversion:]An explicit statement that takes a value of one
  type and computes a corresponding value of another type.
\end{description}

\section{Test-driven development (TDD)}
\myidx{Test-driven development (TDD)} is a software development
practice which arrives at a desired feature through a series of
small, iterative steps motivated by automated tests which are
\emph{written first} that express increasing refinements of the
desired feature.
Doctest enables us to easily demontrate TDD. Let's say we want a
function which creates a rows by columns matrix given arguments for
rows and columns.
We first setup a test for this function in a file named
\pythoninline{matrices.py}:
\begin{python}
def make_matrix(rows, columns):
   """
     >>> make_matrix(3, 5)
     [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
   """

if __name__ == '__main__':
   import doctest
   doctest.testmod()
\end{python}
Running this returns in a failing test:
\begin{pythonOutput}

**********************************************************************
File "matrices.py", line 3, in __main__.make_matrix
Failed example:
   make_matrix(3, 5)
Expected:
   [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Got nothing
**********************************************************************
1 items had failures:
  1 of   1 in __main__.make_matrix
***Test Failed*** 1 failures.
\end{pythonOutput}
The test fails because the body of the function contains only a
single triple quoted string and no return statement, so it returns
\pythoninline{None}. Our test indicates that we wanted it to return a
matrix with 3 rows of 5 columns of zeros.
The rule in using TDD is to use the \emph{simplest thing that works}
in writing a solution to pass the test, so in this case we can simply
return the desired result:
\begin{python}
def make_matrix(rows, columns):
   """
     >>> make_matrix(3, 5)
     [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
   """
   return [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
\end{python}
Running this now the test passes, but our current implimentation of
\pythoninline{make\_matrix} always returns the same result, which is clearly not
what we intended. To fix this, we first motivate our improvement by
adding a test:
\begin{python}
def make_matrix(rows, columns):
   """
     >>> make_matrix(3, 5)
     [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
     >>> make_matrix(4, 2)
     [[0, 0], [0, 0], [0, 0], [0, 0]]
   """
   return [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
\end{python}
which as we expect fails:
\begin{pythonOutput}
**********************************************************************
File "matrices.py", line 5, in __main__.make_matrix
Failed example:
   make_matrix(4, 2)
Expected:
   [[0, 0], [0, 0], [0, 0], [0, 0]]
Got:
   [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
**********************************************************************
1 items had failures:
  1 of   2 in __main__.make_matrix
***Test Failed*** 1 failures.
\end{pythonOutput}
This technique is called \emph{test-driven} because code should only
be written when there is a failing test to make pass. Motivated by
the failing test, we can now produce a more general solution:
\begin{python}
def make_matrix(rows, columns):
   """
     >>> make_matrix(3, 5)
     [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
     >>> make_matrix(4, 2)
     [[0, 0], [0, 0], [0, 0], [0, 0]]
   """
   return [[0] * columns] * rows
\end{python}
This solution appears to work, and we may think we are finished, but
when we use the new function later we discover a bug:
\begin{pythonInteractive}
>>> from matrices import *
>>> m = make_matrix(4, 3)
>>> m
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> m[1][2] = 7
>>> m
[[0, 0, 7], [0, 0, 7], [0, 0, 7], [0, 0, 7]]
>>>
\end{pythonInteractive}
We wanted to assign the element in the second row and the third
column the value 7, instead, \emph{all} elements in the third column
are 7!
Upon reflection, we realize that in our current solution, each row is
an \emph{alias} of the other rows. This is definitely not what we
intended, so we set about fixing the problem, \emph{first by writing a
failing test}:
\begin{python}
def make_matrix(rows, columns):
   """
     >>> make_matrix(3, 5)
     [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
     >>> make_matrix(4, 2)
     [[0, 0], [0, 0], [0, 0], [0, 0]]
     >>> m = make_matrix(4, 2)
     >>> m[1][1] = 7
     >>> m
     [[0, 0], [0, 7], [0, 0], [0, 0]]
   """
   return [[0] * columns] * rows
\end{python}
With a failing test to fix, we are now driven to a better solution:
\begin{python}
def make_matrix(rows, columns):
   """
     >>> make_matrix(3, 5)
     [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
     >>> make_matrix(4, 2)
     [[0, 0], [0, 0], [0, 0], [0, 0]]
     >>> m = make_matrix(4, 2)
     >>> m[1][1] = 7
     >>> m
     [[0, 0], [0, 7], [0, 0], [0, 0]]
   """
   matrix = []
   for row in range(rows):
       matrix += [[0] * columns]
   return matrix
\end{python}

Using TDD has several benefits to our software development
process. It:
\begin{itemize}
\item helps us think concretely about the problem we are trying solve
  \emph{before} we attempt to solve it.
\item encourages breaking down complex problems into smaller, simpler
  problems and working our way toward a solution of the larger
  problem step-by-step.
\item assures that we have a well developed automated test suite for
  our software, facilitating later additions and improvements.
\end{itemize}

\begin{description}
\item[test-driven development (TDD):]A software development practice
  which arrives at a desired feature through a series of small,
  iterative steps motivated by automated tests which are
  \emph{written first} that express increasing refinements of the
  desired feature. (see the Wikipedia article on Test-driven
  development for more information.)
\end{description}

\begin{enumerate}
\item Open a file named \pythoninline{ch09e02.py} and with the
following content: 
\begin{python}
#  Add your doctests here:
"""
"""

# Write your Python code here:


if __name__ == '__main__':
   import doctest
   doctest.testmod()
\end{python}
Add each of the following sets of doctests to the docstring at the
top of the file and write Python code to make the doctests
pass. 
\begin{enumerate}
\item
\begin{python}
"""
 >>> a_list[3]
 42
 >>> a_list[6]
 'Ni!'
 >>> len(a_list)
 8
"""
\end{python}

\item
\begin{python}
"""
 >>> b_list[1:]
 ['Stills', 'Nash']
 >>> group = b_list + c_list
 >>> group[-1]
 'Young'
"""
\end{python}

\item
\begin{python}
"""
 >>> 'war' in mystery_list
 False
 >>> 'peace' in mystery_list
 True
 >>> 'justice' in mystery_list
 True
 >>> 'oppression' in mystery_list
 False
 >>> 'equality' in mystery_list
 True
"""
\end{python}

\item
\begin{python}
"""
 >>> range(a, b, c)
 [5, 9, 13, 17]
"""
\end{python}
\end{enumerate}
Only add one set of doctests at a time. The next set of doctests
should not be added until the previous set pass.

\item Open a file named ch09e06.py and use the same proceedure as in
  exercise 2 to make the following doctests pass: \begin{enumerate}
  \item
\begin{python}

"""
 >>> 13 in junk
 True
 >>> del junk[4]
 >>> junk
 [3, 7, 9, 10, 17, 21, 24, 27]
 >>> del junk[a:b]
 >>> junk
 [3, 7, 27]
"""

\end{python}

  \item
\begin{python}

"""
 >>> nlist[2][1]
 0
 >>> nlist[0][2]
 17
 >>> nlist[1][1]
 5
"""

\end{python}

  \item
\begin{python}

"""
 >>> import string
 >>> string.split(message, '??')
 ['this', 'and', 'that']
"""

\end{python}


\end{enumerate}

\item Add the following two functions to \pythoninline{matrices.py}
introduced in section on test-driven development:
\begin{python}
def add_row(matrix):
   """
     >>> m = [[0, 0], [0, 0]]
     >>> add_row(m)
     [[0, 0], [0, 0], [0, 0]]
     >>> n = [[3, 2, 5], [1, 4, 7]]
     >>> add_row(n)
     [[3, 2, 5], [1, 4, 7], [0, 0, 0]]
     >>> n
     [[3, 2, 5], [1, 4, 7]]
   """

def add_column(matrix):
   """
     >>> m = [[0, 0], [0, 0]]
     >>> add_column(m)
     [[0, 0, 0], [0, 0, 0]]
     >>> n = [[3, 2], [5, 1], [4, 7]]
     >>> add_column(n)
     [[3, 2, 0], [5, 1, 0], [4, 7, 0]]
     >>> n
     [[3, 2], [5, 1], [4, 7]]
   """

\end{python}
Your new functions should pass the doctests. Note that the last
doctest in each function assures that \pythoninline{add\_row} and
\pythoninline{add\_column} are pure functions. 

\item Write a function \pythoninline{add\_matrices(m1, m2)} that adds
\pythoninline{m1} and \pythoninline{m2} and returns a new matrix
containing their sum. You can assume that \pythoninline{m1} and
\pythoninline{m2} are the same size. You add two matrices by adding
their corresponding values. 
\begin{python}
def add_matrices(m1, m2):
   """
     >>> a = [[1, 2], [3, 4]]
     >>> b = [[2, 2], [2, 2]]
     >>> add_matrices(a, b)
     [[3, 4], [5, 6]]
     >>> c = [[8, 2], [3, 4], [5, 7]]
     >>> d = [[3, 2], [9, 2], [10, 12]]
     >>> add_matrices(c, d)
     [[11, 4], [12, 6], [15, 19]]
     >>> c
     [[8, 2], [3, 4], [5, 7]]
     >>> d
     [[3, 2], [9, 2], [10, 12]]
   """
\end{python}
Add your new function to \pythoninline{matrices.py} and be sure it passes the
doctests above. The last two doctests confirm that
\pythoninline{add\_matrices} is a pure function.

\item Write a function \pythoninline{scalar\_mult(n, m)} that
multiplies a matrix, \pythoninline{m}, by a scalar, \pythoninline{n}. 
\begin{python}
def scalar_mult(n, m):
   """
     >>> a = [[1, 2], [3, 4]]
     >>> scalar_mult(3, a)
     [[3, 6], [9, 12]]
     >>> b = [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
     >>> scalar_mult(10, b)
     [[30, 50, 70], [10, 10, 10], [0, 20, 0], [20, 20, 30]]
     >>> b
     [[3, 5, 7], [1, 1, 1], [0, 2, 0], [2, 2, 3]]
   """
\end{python}
Add your new function to \pythoninline{matrices.py} and be sure it passes the
doctests above.
\item
\begin{python}
def row_times_column(m1, row, m2, column):
   """
     >>> row_times_column([[1, 2], [3, 4]], 0, [[5, 6], [7, 8]], 0)
     19
     >>> row_times_column([[1, 2], [3, 4]], 0, [[5, 6], [7, 8]], 1)
     22
     >>> row_times_column([[1, 2], [3, 4]], 1, [[5, 6], [7, 8]], 0)
     43
     >>> row_times_column([[1, 2], [3, 4]], 1, [[5, 6], [7, 8]], 1)
     50
   """

def matrix_mult(m1, m2):
   """
     >>> matrix_mult([[1, 2], [3,  4]], [[5, 6], [7, 8]])
     [[19, 22], [43, 50]]
     >>> matrix_mult([[1, 2, 3], [4,  5, 6]], [[7, 8], [9, 1], [2, 3]])
     [[31, 19], [85, 55]]
     >>> matrix_mult([[7, 8], [9, 1], [2, 3]], [[1, 2, 3], [4, 5, 6]])
     [[39, 54, 69], [13, 23, 33], [14, 19, 24]]
   """

\end{python}
Add your new functions to \pythoninline{matrices.py} and be sure it passes the
doctests above.
\end{enumerate}

\section{pydoc}

You can use \myidx{pydoc} to search through the Python libraries
installed on your system. At the \myidx{command prompt} type the
following:
\begin{shell}
$ pydoc -g
\end{shell} % $
and the following will appear (\textbf{No it doesn't - at least not on
my linux machine!}):

\includegraphics[scale=1]{illustrations/pydoc_tk.png}

(\emph{note} : see exercise 2 if you get an error)
Click on the open browser button to launch a web browser window
containing the documentation generated by pydoc:

\includegraphics[width=\textwidth]{illustrations/pydoc_firefox.png}

This is a listing of all the python libraries found by Python on your
system. Clicking on a module name opens a new page with documenation
for that module. Clicking keyword, for example, opens the following
page:

\includegraphics[width=\textwidth]{illustrations/pydoc_keyword_firefox.png}

Documentation for most modules contains three color coded sections:
\begin{itemize}
\item \emph{Classes} in pink
\item \emph{Functions} in orange
\item \emph{Data} in green
\end{itemize}


In Lecture \ref{lec-strings} we introduced the \pythoninline{find}
function from the \pythoninline{string} module. The
\pythoninline{string} module contains many other useful functions: 
\begin{pythonInteractive}
>>> import string
>>> string.capitalize('maryland')
'Maryland'
>>> string.capwords("what's all this, then, amen?")
"What's All This, Then, Amen?"
>>> string.center('How to Center Text Using Python', 70)
'                   How to Center Text Using Python                    '
>>> string.upper('angola')
'ANGOLA'
>>> 
\end{pythonInteractive}
You should use \pythoninline{pydoc} to browse the other functions and
attributes in the string module.

\section{String and list methods}

As the Python language developed, most of functions from the string
module have also been added as \myidx{methods} of string objects. A
method acts much like a function, but the syntax for calling it is a
bit different:
\begin{pythonInteractive}
>>> 'maryland'.capitalize()
'Maryland'
>>> "what's all this, then, amen?".title()
"What'S All This, Then, Amen?"
>>> 'How to Center Text Using Python'.center(70)
'                   How to Center Text Using Python                    '
>>> 'angola'.upper()
'ANGOLA'
>>>
\end{pythonInteractive}
String methods are built into string objects, and they are
\emph{invoked} (called) by following the object with the dot operator
and the method name.
We will be learning how to create our own objects with their own
methods in later chapters. For now we will only be using methods that
come with Python's built-in objects.

The dot operator can also be used to access built-in methods of list
objects:
\begin{pythonInteractive}
>>> mylist = []
>>> mylist.append(5)
>>> mylist.append(27)
>>> mylist.append(3)
>>> mylist.append(12)
>>> mylist
[5, 27, 3, 12]
>>>
\end{pythonInteractive}
\pythoninline{append} is a list method which adds the argument passed
to it to the 
end of the list. Continuing with this example, several other
\pythoninline{list} 
methods are:
\begin{pythonInteractive}
>>> mylist.insert(1, 12)
>>> mylist
[5, 12, 27, 3, 12]
>>> mylist.count(12)
2
>>> mylist.extend([5, 9, 5, 11])
>>> mylist
[5, 12, 27, 3, 12, 5, 9, 5, 11])
>>> mylist.index(9)
6
>>> mylist.count(5)
3
>>> mylist.reverse()
>>> mylist
[11, 5, 9, 5, 12, 3, 27, 12, 5]
>>> mylist.sort()
>>> mylist
[3, 5, 5, 5, 9, 11, 12, 12, 27]
>>> mylist.remove(12)
>>> mylist
[3, 5, 5, 5, 9, 11, 12, 27]
>>>
\end{pythonInteractive}

Experiment with the list methods in this example until you feel
confident that you understand how they work.

\section{Glossary}
\begin{description}

\item[standard library:]A library is a collection of software used as
   tools in the development of other software. The standard library of
   a programming language is the set of such tools that are
   distributed with the core programming language. Python comes with
   an extensive standard library.
\item[pydoc:]A documentation generator that comes with the Python
   standard library.
\item[command prompt:]A string displayed by a command line interface
   indicating that commands can be entered.
\item[method:] Function-like attribute of an object. Methods are
   \emph{invoked} (called) on an object using the dot operator. For
   example:
\begin{pythonInteractive}
>>> s = "this is a string."
>>> s.upper()
'THIS IS A STRING.'
>>>
\end{pythonInteractive}
We say that the method, \pythoninline{upper} is invoked on the string,
\pythoninline{s}. \pythoninline{s} is implicitly the first argument to
\pythoninline{upper}. 
 \end{description}

\section{Laboratory Exercises}

\iain{Proposed second mastery test 26-28 May}

\begin{enumerate}
\item Complete the following: 
\begin{itemize}
   \item Start the pydoc server with the command \pythoninline{pydoc
-g} at the
     command prompt.
   \item Click on the open browser button in the pydoc tk window.
   \item Find the calendar module and click on it.
   \item While looking at the \emph{Functions} section, try out the
     following in a Python shell:
\begin{pythonInteractive}
  >>> import calendar
  >>> year = calendar.calendar(2008)
  >>> print year                      # What happens here?
\end{pythonInteractive}

   \item Experiment with \pythoninline{calendar.isleap}. What does it
expect as an 
     argument? What does it return as a result? What kind of a
     function is this?
   \end{itemize}
   Make detailed notes about what you learned from this exercise.
 \item

   If you don't have Tkinter installed on your computer, then
\pythoninline{pydoc -g} 
   will return an error, since the graphics window that it opens
   requires Tkinter. An alternative is to start the web server
   directly:
\begin{shell}
$ pydoc -p 7464
\end{shell} % $
This starts the \pythoninline{pydoc} web server on port 7464. Now point your web
browser at \pythoninline{http://localhost:7464}
and you will be able to browse the Python libraries installed on
you system.
Use this approach to start pydoc and take a look at the math
module. Record detailed notes of your investigation in this exercise.
   \begin{enumerate}
   \item How many functions are in the math module?
   \item What does \pythoninline{math.ceil} do? What about
\pythoninline{math.floor}? (\emph{hint:}
     both \pythoninline{floor} and \pythoninline{ceil} expect floating
point arguments.) 
   \item Describe how we have been computing the same value as
     \pythoninline{math.sqrt} without using the math module.
   \item What are the two data contstants in the math module?
   \end{enumerate}

\item Use \pythoninline{pydoc} to investigate the \pythoninline{copy}
module. What does \pythoninline{deepcopy} do? In which exercises from
the last laboratory would \pythoninline{deepcopy} have come in handy?
\item  Create a module named \pythoninline{mymodule1.py}. Add
attributes \pythoninline{myage} set to your current age, and
\pythoninline{year} set to the current year. Create another module
named \pythoninline{mymodule2.py}. Add attributes \pythoninline{myage}
set to \pythoninline{0}, and \pythoninline{year} set to the year you
were born. 
Now create a file named \pythoninline{namespace\_test.py}. Import both of the
modules above and write the following statement:
\begin{python}
print (mymodule2.myage - mymodule1.myage) == (mymodule2.year - mymodule1.year)
\end{python}
When you run \pythoninline{namespace\_test.py} you will see either True or
False as output depending on whether or not you've already had your
birthday this year.
\item Add the following statement to \pythoninline{mymodule1.py},
\pythoninline{mymodule2.py} , and
\pythoninline{namespace\_test.py} from the previous exercise:
\begin{python}
print "My name is %s" % __name__
\end{python}
Run \pythoninline{namespace\_test.py}. What happens? Why? Now add the
following to the bottom of \pythoninline{mymodule1.py}:
\begin{python}
if __name__ == '__main__':
    print "This won't run if I'm  imported."
\end{python}
Run \pythoninline{mymodule1.py} and \pythoninline{namespace\_test.py}
again. In which case do you see the new \pythoninline{print} statement?
\item In a Python shell try the following:
\begin{pythonInteractive}
>>> import this
\end{pythonInteractive}
What does Tim Peter's have to say about namespaces?
\item Use \pythoninline{pydoc} to find and test three other functions from the
   \pythoninline{string} module. Record your findings.
\item The \pythoninline{dir} function, which we first saw in Section
\ref{sec-string-module}, prints out a 
list of the \emph{attributes} of an object passed to it as an
argument. In other words, \pythoninline{dir} returns the contents of the
\emph{namespace} of its argument.
   \\
   Use \pythoninline{dir(str)} and \pythoninline{dir(list)} to find at
least three string and list methods which have not been introduced in
the examples in the chapter. You should ignore anything that begins
with double underscore (\_\_) for the time being. Be sure to make
detailed notes of your findings, including names of the new methods
and examples of their use. 
   \\
   (\emph{hint:} Print the \pythoninline{docstring} of a function you want to
   explore. For example, to find out how \pythoninline{str.join} works, print
   \pythoninline{str.join.\_\_doc\_\_})
 \item Give the Python interpreter's response to each of the following
   from a continuous interpreter session: \begin{enumerate}
   \item
\begin{pythonInteractive}
  >>> s = "If we took the bones out, it wouldn't be crunchy, would it?"
  >>> s.split()
\end{pythonInteractive}
   \item
\begin{pythonInteractive}
  >>> type(s.split())
\end{pythonInteractive}
   \item
\begin{pythonInteractive}
  >>> s.split('o')
\end{pythonInteractive}
   \item
\begin{pythonInteractive}
  >>> s.split('i')
\end{pythonInteractive}
   \item
\begin{pythonInteractive}
  >>> '0'.join(s.split('o'))
\end{pythonInteractive}
\end{enumerate}
Be sure you understand why you get each result. Then apply what you
have learned to fill in the body of the function below using the
\pythoninline{split} and \pythoninline{join} methods of
\pythoninline{str} objects: 
\begin{python}
def myreplace(old, new, s):
    """
    Replace all occurances of old with new in the string s.

       >>> myreplace(',', ';', 'this, that, and, some, other, thing')
       'this; that; and; some; other; thing'
       >>> myreplace(' ', '**', 'Words will now be separated by stars.')
       'Words**will**now**be**separated**by**stars.'
    """
\end{python}
Your solution should pass all doctests.

\item Create a module named \pythoninline{wordtools.py} with the
following at the bottom:
\begin{python}
if __name__ == '__main__':
    import doctest
    doctest.testmod()
\end{python}
Explain how this statement makes both using and testing this module
convenient. What will be the value of \pythoninline{\_\_name\_\_} when
\pythoninline{wordtools.py} is imported from another module? What will
it be when it is run as a main program? In which case will the
doctests run? 
   \\
Now add bodies to each of the following functions to make the
doctests pass:
\begin{python}
def cleanword(word):
    """
      >>> cleanword('what?')
      'what'
      >>> cleanword('"now!"')
      'now'
      >>> cleanword('?+="word!,@$()"')
      'word'
    """

def has_dashdash(s):
    """
      >>> has_dashdash('distance--but')
      True
      >>> has_dashdash('several')
      False
      >>> has_dashdash('critters')
      False
      >>> has_dashdash('spoke--fancy')
      True
      >>> has_dashdash('yo-yo')
      False
    """

def extract_words(s):
    """
      >>> extract_words('Now is the time!  "Now", is the time? Yes, now.')
      ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time', 'yes', 'now']
      >>> extract_words('she tried to curtsey as she spoke--fancy')
      ['she', 'tried', 'to', 'curtsey', 'as', 'she', 'spoke', 'fancy']
    """

def wordcount(word, wordlist):
    """
      >>> wordcount('now', ['now', 'is', 'time', 'is', 'now', 'is', 'is'])
      ['now', 2]
      >>> wordcount('is', ['now', 'is', 'time', 'is', 'now', 'is', 'the', 'is'])
      ['is', 4]
      >>> wordcount('time', ['now', 'is', 'time', 'is', 'now', 'is', 'is'])
      ['time', 1]
      >>> wordcount('frog', ['now', 'is', 'time', 'is', 'now', 'is', 'is'])
      ['frog', 0]
    """

def wordset(wordlist):
    """
      >>> wordset(['now', 'is', 'time', 'is', 'now', 'is', 'is'])
      ['is', 'now', 'time']
      >>> wordset(['I', 'a', 'a', 'is', 'a', 'is', 'I', 'am'])
      ['I', 'a', 'am', 'is']
      >>> wordset(['or', 'a', 'am', 'is', 'are', 'be', 'but', 'am'])
      ['a', 'am', 'are', 'be', 'but', 'is', 'or']
    """

def longestword(wordset):
    """
      >>> longestword(['a', 'apple', 'pear', 'grape'])
      5
      >>> longestword(['a', 'am', 'I', 'be'])
      2
      >>> longestword(['this', 'that', 'supercalifragilisticexpialidocious'])
      34
    """

\end{python}
Save this module so you can use the tools it contains in your
programs.
\end{enumerate}
