\documentclass[10pt,twocolumn]{article}

\setlength{\oddsidemargin}{-0.5in}
\setlength{\evensidemargin}{-0.5in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{7.0in}
\setlength{\textheight}{9.5in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.05in}
\setlength{\columnseprule}{0.3pt}
\usepackage{fancyvrb}
\usepackage{relsize}

\begin{document}

Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

\textbf{Directions: Work only on this sheet (on both sides, if needed); do not
turn in any supplementary sheets of paper. There is actually plenty of room
for your answers, as long as you organize yourself BEFORE starting writing.
In order to get full credit, SHOW YOUR WORK.}

{\bf 1.} (15) Which of the following would be legal/illegal if inserted
into  the program {\bf tfe.py} in Sec. 5.1 of our Python tutorial PLN?
Write ``legal'' or ``illegal.''  Each is independent of the others, i.e.
you are not being asked whether it would be legal to insert more than
one of them together.

\begin{itemize}

\item [(a)]: {\tt b.idnum = 1} after line 23

\item [(b)]: {\tt w = a.name + b.name} after line 23

\item [(c)]: {\tt b = 12} after line 27 (not indented)

\end{itemize}

{\bf 2.} (10) Suppose an exception occurs which our program does not
handle using {\bf try}...{\bf except}.  The name of the default function
which is called at that time  is \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.

{\bf 3.} (10) Write a single line of code which produces {\bf cmdlnargs},
a list consisting of all the command-line arguments excluding the name
of the program being run.  (Note:  Code like {\tt if x == y: z = 8}
counts as two lines, as if the second part were on a new line.)

{\bf 4.} (10) In our Python network programming PLN, we didn't call an
analog of the function {\bf gethostbyname()} seen in our network
tutorial PLN, because that function's actions are included in the Python
function \_\_\_\_\_\_\_\_\_\_\_\_.  Similarly, the information returned
by {\bf getpeername()} in the network tutorial is included in what is
returned by the Python function \_\_\_\_\_\_\_\_\_\_\_\_.

{\bf 5.} (15) Fill in the blanks in the following pair of functions
which find all files of a given name (regardless of whether they be
ordinary files, directories or links) in a given directory tree.  Each
file is reported in terms of the path relative to {\bf dtroot}.
Example:  The tree starts at {\bf /z/a}, which contains an ordinary file
{\bf b} and a directory {\bf c}; the latter contains a directory {\bf
b}; the program is run from within {\bf c}, with `..' as {\bf dtroot}
and `b' as {\bf targetfile}.  Then the output will be

\begin{Verbatim}[fontsize=\relsize{-2}]
../b
../c/b
\end{Verbatim}

Here is the code:

\begin{Verbatim}[fontsize=\relsize{-2}]
import os, sys

def checkthisdir(targetfile,dr,flst):
   if ____________________:
      print os.path.join(____________________)

def findfile(dtroot,targetfile):
   os.path.walk(________________________)
\end{Verbatim}

{\bf 6.} (20) Fill in the blanks in the following lines of code, which
returns a list of all words in a text file.  It is presumed that the
file has already been opened by the calling program, with {\bf fi} being
assigned the result of {\bf open()}, but no other operations have been
performed on the file.

\begin{Verbatim}[fontsize=\relsize{-2}]
def getwords(fi):
   wrds = map(________________________)
   return reduce(________________)
\end{Verbatim}

For example, if called on the file

\begin{Verbatim}[fontsize=\relsize{-2}]
a b cd
xyz

uu vv
\end{Verbatim}

the value returned should be ['a', 'b', 'cd', 'xyz', 'uu', 'vv'].

{\bf 7.} (20) Suppose you are debugging a multi-module program by using
{\bf pdbw.py}.  You wish to know how many breakpoints you've set in each
module.  You will be able to do so using a module {\bf bpl.py}.  For
example, in debugging our example of {\bf tf.py} and {\bf tftest.py} in
Sec.  9.1.1 of our Python tutorial PLN, we might have:

\begin{Verbatim}[fontsize=\relsize{-2}]
(Pdb) import bpl
(Pdb) bpl.bpc(debugger)
/a/b/tf.py 2
/a/b/tftest.py 1
\end{Verbatim}

Fill in the blanks in {\bf bpl.py}:

\begin{Verbatim}[fontsize=\relsize{-2}]
def bpc(dbg):
   for f in ____________________:
      print f, ____________________
\end{Verbatim}

{\bf Solutions:}

{\bf 1.a}  Legal.  Classes are implemented as dictionaries, and thus can
be added to at any time.

{\bf 1.b}  Legal.  This is just string concatenation.

{\bf 1.c}  Legal.  The variable {\bf b} is just a reference, i.e.
pointer.  You can use it to point to whatever type you wish.

{\bf 2.}  {\bf sys.\_\_excepthook\_\_()}; half credit for {\bf
sys.excepthook()}

{\bf 3.}  

\begin{Verbatim}[fontsize=\relsize{-2}]
cmdlnargs = sys.argv[1:]
\end{Verbatim}

{\bf 4.}  {\bf socket.connect()}; {\bf socket.accept()}

{\bf 5.}

\begin{Verbatim}[fontsize=\relsize{-2}]
import os, sys

def checkthisdir(targetfile,dr,flst):
   if targetfile in flst:
      print os.path.join(dr,targetfile)

def findfile(dtroot,targetfile):
   os.path.walk(dtroot,checkthisdir,targetfile)
\end{Verbatim}

{\bf 6.}  

\begin{Verbatim}[fontsize=\relsize{-2}]
def getwords(for instance):
   wrds = map(lambda u:u.split(),fi)
   return reduce(lambda x,y: x+y,wrds)
\end{Verbatim}

{\bf 7.}

\begin{Verbatim}[fontsize=\relsize{-2}]
def bpc(dbg):
   for f in dbg.breaks.keys():
      print f, len(dbg.breaks[f])
\end{Verbatim}


\end{document}


