\documentclass[english,serif,mathserif,xcolor=pdftex,dvipsnames,table]{beamer}
\usepackage{lsci}

\title[Python II]{%
  Python programming exercises, I
}
\author[R. Murri]{%
  \textbf{Riccardo Murri} \\
  Grid Computing Competence Center, \\
  Organisch-Chemisches Institut, \\
  University of Zurich
}
\date{Nov.~10,~2011}

\begin{document}

% title frame
\maketitle


\section{Introduction}

\begin{frame}
  \frametitle{Today's class}
  
  Getting your feet wet with Python programming.
  (Review of yesterday's stuff.)

  \+
  {\small These slides are available for download from: 
    \url{http://www.gc3.uzh.ch/teaching/lsci2011/lab07.pdf}}
\end{frame}



\begin{frame}[fragile]
  \frametitle{Yesterday's homework}
  Write a function \texttt{sp($C$,$L$)} that returns a pair
  (\texttt{tuple}) of elements from a \texttt{list} $L$ whose sum is
  integer $C$.

  {\+\em
    (Unspecified: what should it do when no pairs of values from $L$ sums to $C$?)}
\end{frame}


\begin{frame}
  \frametitle{Testing solutions}
  Rather than inspecting each solutions' code, we shall write a test
  class, using Python standard library
  \href{http://en.wikipedia.org/wiki/Unit_testing}{unit testing}
  facility.

  \+
  Your solution is correct if it passes the test.
\end{frame}

\begin{frame}
  \frametitle{Python's \texttt{unittest} module}
  
  Python's \texttt{unittest} requires one to define a subclass of
  \texttt{unittest.TestCase}. 

  \+
  All methods whose name starts with \texttt{test} are executed; if
  none errors out, the test is passed.

  \+
  Test methods should use methods \texttt{assertEqual},
  \texttt{assertTrue}, etc. defined by class \texttt{TestCase} to
  check if test conditions are satisfied.

  \begin{references}
    \url{http://docs.python.org/library/unittest.html}
  \end{references}
\end{frame}


\begin{frame}[fragile]
  \begin{columns}[t]
    \begin{column}{0.5\textwidth}
\begin{lstlisting}
!\HL{\textbf{import} unittest \textbf{as} ut}!

class SpTest(ut.TestCase):

  def test_sp_1(self):
    (x, y) = sp(100, [5, 75, 25])
    self.assertEqual((x,y), (75, 25))

  def test_sp_2(self):
    (x,y) = sp(8, [2,1,9,4,4,56,90,3])
    self.assertTrue((x,y) == (4,4))

if __name__ == "__main__":
  ut.main()
\end{lstlisting}
    \end{column}
    \begin{column}{0.5\textwidth}
      \raggedleft 
      This syntax allows you to import a module but assign it a
      different name.
    \end{column}
  \end{columns}
\end{frame}


\begin{frame}[fragile]
  \begin{columns}[t]
    \begin{column}{0.5\textwidth}
\begin{lstlisting}
import unittest as ut

!\HL{\textbf{class}~SpTest(ut.TestCase):}!

  def test_sp_1(self):
    (x, y) = sp(100, [5, 75, 25])
    self.assertEqual((x,y), (75, 25))

  def test_sp_2(self):
    (x,y) = sp(8, [2,1,9,4,4,56,90,3])
    self.assertTrue((x,y) == (4,4))

if __name__ == "__main__":
  ut.main()
\end{lstlisting}
    \end{column}
    \begin{column}{0.5\textwidth}
      \raggedleft 
      Declare a new class, ihneriting from the
      \texttt{ut.TestCase} class.
    \end{column}
  \end{columns}
\end{frame}


\begin{frame}[fragile,label=gotoself]
  \begin{columns}[t]
    \begin{column}{0.5\textwidth}
\begin{lstlisting}
import unittest as ut

class SpTest(ut.TestCase):

  def test_sp_1(!\HL{\textbf{self}}!):
    (x, y) = sp(100, [5, 75, 25])
    self.assertEqual((x,y), (75, 25))

  def test_sp_2(self):
    (x,y) = sp(8, [2,1,9,4,4,56,90,3])
    self.assertTrue((x,y) == (4,4))

if __name__ == "__main__":
  ut.main()
\end{lstlisting}
    \end{column}
    \begin{column}{0.5\textwidth}
      \raggedleft
      A method declaration looks exactly like a function
      definition. Every method \emph{must} have at least one argument,
      named \textbf{self}.
      \\
      \hyperlink{self}{\beamergotobutton{More on \texttt{self}}}
    \end{column}
  \end{columns}
\end{frame}


\begin{frame}[fragile]
  \begin{columns}[t]
    \begin{column}{0.45\textwidth}
\begin{lstlisting}
import unittest as ut

class SpTest(ut.TestCase):

  def test_sp_1(self):
    (x, y) = sp(100, [5, 75, 25])
    !\HL{\textbf{self}}!.assertEqual((x,y), (75, 25))

  def test_sp_2(self):
    (x,y) = sp(8, [2,1,9,4,4,56,90,3])
    self.assertTrue((x,y) == (4,4))

if __name__ == "__main__":
  ut.main()
\end{lstlisting}
    \end{column}
    \begin{column}{0.5\textwidth}
      \raggedleft
      {\tt\bf self} is a reference to the object instance (like, e.g.,
      \texttt{this} in Java).  It is used to access attributes and
      invoke methods of the object itself.
      \\
      \hyperlink{self}{\beamergotobutton{More on \texttt{self}}}
    \end{column}
  \end{columns}
\end{frame}


\begin{frame}[fragile]
  \begin{columns}[t]
    \begin{column}{0.5\textwidth}
\begin{lstlisting}
import unittest as ut

class SpTest(ut.TestCase):

  def test_sp_1(self):
    (x, y) = sp(100, [5, 75, 25])
    !\HL{\textbf{self}.assertEqual((x,y), (75, 25))}!

  def test_sp_2(self):
    (x,y) = sp(8, [2,1,9,4,4,56,90,3])
    self.assertTrue((x,y) == (4,4))

if __name__ == "__main__":
  ut.main()
\end{lstlisting}
    \end{column}
    \begin{column}{0.5\textwidth}
      \raggedleft
      This invokes method \texttt{assertEqual} of the current
      object. 
      \\
      {\em (Where is it defined?)}
    \end{column}
  \end{columns}
\end{frame}



\begin{frame}[fragile]
  \begin{columns}[t]
    \begin{column}{0.5\textwidth}
\begin{lstlisting}
import unittest as ut

class SpTest(ut.TestCase):

  def test_sp_1(self):
    (x, y) = sp(100, [5, 75, 25])
    self.assertEqual((x,y), (75, 25))

  def test_sp_2(self):
    (x,y) = sp(8, [2,1,9,4,4,56,90,3])
    self.assertTrue((x,y) == (4,4))

!\HL{\textbf{if} \_\_name\_\_ == "\_\_main\_\_":}!
!\HL{~~ut.main()}!
\end{lstlisting}
    \end{column}
    \begin{column}{0.5\textwidth}
      \raggedleft
      Python idiom: execute \texttt{ut.main()} iff this file is the
      main script, i.e., it is not being read as a module.
    \end{column}
  \end{columns}
\end{frame}


% \begin{frame}[fragile]
%   \begin{columns}[t]
%     \begin{column}{0.5\textwidth}
% \begin{lstlisting}
% import unittest as ut

% class SpTest(ut.TestCase):

%   def test_sp_1(self):
%     (x, y) = sp(100, [5, 75, 25])
%     self.assertEqual((x,y), (75, 25))

%   def test_sp_2(self):
%     (x,y) = sp(8, [2,1,9,4,4,56,90,3])
%     self.assertTrue((x,y) == (4,4))

% if __name__ == "__main__":
%   ut.main()
% \end{lstlisting}
%     \end{column}
%     \begin{column}{0.5\textwidth}
%       This declares a new class, ihneriting from \texttt{ut.TestCase}
%     \end{column}
%   \end{columns}
% \end{frame}


\begin{frame}[fragile]
  \frametitle{Time to check!}

  0. Download the test code from \url{http://www.gc3.uzh.ch/teaching/lsci2011/lab06/sptest.py}

  \+
  1. Add your \texttt{sp}~function  to it.

  \+
  2. Run the script: {\ttfamily\bfseries python sptest.py --verbose}

  \+ 
  3. If you get the following output: \emph{congratulations!} your
  \texttt{sp}~implementation works as expected.
\begin{verbatim}
test_sp_1 (__main__.SpTest) ... ok
test_sp_2 (__main__.SpTest) ... ok

-------------------------------------------
Ran 2 tests in 0.000s

OK
\end{verbatim}
\end{frame}


\begin{frame}[fragile]
  \frametitle{File I/O}

  \begin{describe}{\ttfamily open(path,mode)}
    Return a Python \texttt{file} object for reading or writing the
    file located at \texttt{path}.  Mode is one of '\texttt{r}',
    '\texttt{w}' or '\texttt{a}' for reading, writing (truncates on open), appending.
    You can add a `\texttt{+}' character to enable read+write (other
    effects being the same).
  \end{describe}

  \begin{describe}{\ttfamily close()}
    Close an open file.
  \end{describe}
  
  \begin{describe}{\ttfamily \textbf{for} line \textbf{in} file\_obj}
    Loop over lines in the file one by one.
  \end{describe}

  \begin{references}
    \url{http://docs.python.org/library/stdtypes.html#file-objects}
  \end{references}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Exercise 1}
  \emph{1.} Implement a function \lstinline|count_lines(stream)|, that returns
  the number of lines in \lstinline|stream| (a file-like object).

  \+
  \emph{2.} Implement a function \lstinline|keys_for_value(D, val)|, which
  returns the list of keys that are associated to value
  \lstinline|val| in dictionary \lstinline|D|.
\end{frame}


\begin{frame}[fragile]
  \frametitle{The `{\ttfamily\bfseries in}' operator}
  
  Use the \lstinline|in| operator to test for presence of an item in a
  collection.

  \begin{describe}{\lstinline|x in S|}
    Evaluates to \texttt{True} if \lstinline|x| is equal to a \emph{value}
    contained in the \lstinline|S| sequence (list, tuple, set).
  \end{describe}
  
  \begin{describe}{\lstinline|x in D|}
    Evaluates to \texttt{True} if \lstinline|x| is equal to a \emph{key}
    in the \lstinline|D| dictionary.
  \end{describe}
  
  \begin{describe}{\lstinline|x in T|}
    Evaluates to \texttt{True} if \lstinline|x| is a substring of
    string \lstinline|T|.
  \end{describe}
  
\end{frame}


\begin{frame}[fragile,fragile]
  \frametitle{Strings to streams and back}

  The \texttt{StringIO} module creates a file-like object from a
  string:
\begin{lstlisting}
>>> from StringIO import StringIO
>>> # create a file-like object
>>> stream = StringIO("python")
\end{lstlisting}

  \+
  The \lstinline|read(n)| method can be used to read \emph{at most}
  \lstinline|n| bytes from a file-like object:
\begin{lstlisting}
>>> s = stream.read(2)
>>> s == 'py'
True
\end{lstlisting}
  If \lstinline|n| is omitted, \texttt{read()} reads until end-of-file.

  \begin{references}
    \url{http://docs.python.org/library/stringio.html}
  \end{references}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Exercise 2}
  \small

  Implement a function \lstinline|count_chars(stream)|, that returns a
  dictionary, mapping each character into the count of its occurrences
  in \lstinline|stream|.  Characters that do not occur in the input
  should not be present in the returned dictionary.  You can assume
  \lstinline|stream| is formed of ASCII characters.

  \begin{enumerate}
  \item Write test cases for the function, before writing it. At the
    very least, check:
    \begin{itemize}
    \item That \lstinline|count_chars()| on an empty stream returns an
      empty dictionary;
    \item That \lstinline|count_chars()| on a stream whose content is
      the string \texttt{'aaaaaa'} returns the dictionary \lstinline|{'a':6}|
    \item That \lstinline|count_chars()| on a stream whose content is
      the string \texttt{'ababab'} returns the dictionary
      \lstinline|{'a':3, 'b':3}|
    \end{itemize}
  \item Verify that the test cases fail.
  \item Write the function, and check that all tests pass.
  \end{enumerate}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Operations on strings}
  Assume \texttt{s} is a Python \lstinline|str| object.

  \begin{describe}{%
      \lstinline|s.capitalize()|,
      \lstinline|s.lower()|, 
      \lstinline|s.upper()|}
    Return a copy of the string capitalized / turned all lowercase /
    turned all uppercase.
  \end{describe}
  
  \begin{describe}{\lstinline|s.split(t)|}
    Split \texttt{s} at every occurrence of \texttt{t} and return list
    of parts.  If \texttt{t} is omitted, split on whitespace.
  \end{describe}
  
  \begin{describe}{\lstinline|s.startswith(t)|, 
      \lstinline|s.endswith(t)|}
    Return \texttt{True} if \texttt{t} is the initial/final substring
    of \texttt{s}.
  \end{describe}
  
  \begin{references}
    \url{http://docs.python.org/library/stdtypes.html#string-methods}
  \end{references}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Exercise 3}
  Implement a function \lstinline|count_words|, that counts the number
  of \emph{distinct} words occurring in \lstinline|stream|.  Words are
  delimited by whitespace; case does not matter.
\end{frame}


\begin{frame}[fragile]
  \frametitle{Filesystem operations}
  \small
  These functions are available from the \texttt{os} module.

  \begin{describe}{\lstinline|os.getcwd()|, \lstinline|os.chdir(path)|}
    Return the path to the current working directory / 
    Change the current working directory to \texttt{path}.
  \end{describe}
    
  \begin{describe}{\lstinline|os.listdir(dir)|}
    Return list of entries in directory \texttt{dir} (omitting
    `\texttt{.}' and `\texttt{..}')
  \end{describe}
  
  \begin{describe}{\lstinline|os.mkdir(path)|}
    Create a directory; fails if the directory already exists.
    Assumes that all parent directories exist already.
  \end{describe}
  
  \begin{describe}{\lstinline|os.makedirs(path)|}
    Create a directory; no-op if the directory already exists.
    Creates all the intermediate-level directories needed to contain
    the leaf.
  \end{describe}
  
  \begin{references}
    \url{http://docs.python.org/library/os.html}
  \end{references}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Filesystem operations, II}
  These functions are available from the \texttt{os.path} module.

  \begin{describe}{\lstinline|os.path.exists(path)|, 
      \lstinline|os.path.isdir(path)|}
    Return \texttt{True} if \texttt{path} exists / is a directory / is
    a regular file.
  \end{describe}
  
  \begin{describe}{\lstinline|os.path.basename(path)|, 
      \lstinline|os.path.dirname(path)|}
    Return the base name (the part after the last `\texttt{/}'
    character) or the directory name (the part before the last
    \texttt{/} character).
  \end{describe}
  
  \begin{describe}{\lstinline|os.path.abspath(path)|}
    Make \texttt{path} absolute (i.e., start with a \texttt{/}).
  \end{describe}
  
  \begin{references}
    \url{http://docs.python.org/library/os.path.html}
  \end{references}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Exercise 4}
  Write a Python program \texttt{rename.py} with the following
  command-line:
\begin{lstlisting}[language=sh]
python rename.py EXT1 EXT2 DIR [DIR ...]
\end{lstlisting}
  where:
  \begin{description}
  \item[ext1,ext2] Are file name extensions (without the leading dot),
    e.g., \texttt{jpg} and \texttt{jpeg}.
  \item[dir] Is directory path; possibly, many directories names can be given on
    the command-line.
  \end{description}

The \texttt{rename.py} command should rename all files in directory DIR,
that end with extension \texttt{ext1} to end with extension
\texttt{ext2} instead.
\end{frame}


\begin{frame}
  \frametitle{More exercises...}
 (In case you want to practice.)
 \begin{itemize}
 \item Basic Python Exercises from Google's Python class: 
   \url{http://code.google.com/edu/languages/google-python-class/exercises/basic.html}
 \item \textbf{The Python Challenge}: \url{http://www.pythonchallenge.com/}
 \item Python Koans: \url{https://github.com/gregmalcolm/python_koans}
 \end{itemize}
\end{frame}


\section{Extra slides}


\begin{frame}[fragile,label=self]
  \frametitle{The \texttt{self} argument}

  \textbf{Every method of a Python object always has \texttt{self}
    as first argument.}

  \+
  However, you do not specify it when calling a method: it's
  automatically inserted by Python:
\begin{lstlisting}
>>> class ShowSelf(object):
...     def show(self):
...             print(self)
... 
>>> x = ShowSelf() # construct instance
>>> x.show() # `self' automatically inserted!
<__main__.ShowSelf object at 0x299e150>
\end{lstlisting}

  \+
  The \texttt{self} variable is a reference to the object itself.
  You \emph{need to} use \texttt{self} when accessing other methods or
  attributes of the object.

  \hfill\hyperlink{gotoself}{\beamergotobutton{Back to Unit Testing}}
\end{frame}


\begin{frame}[fragile,label=legb]
  \frametitle{Name resolution rules}
  \small

  Within a function body, names are resolved according to \href{http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules/292502#292502}{the LEGB rule}:
  \begin{description}
  \item[L] Local scope: any names defined in the current function;
  \item[E] Enclosing function scope: names defined in enclosing
    functions (outermost last);
  \item[G] global scope: names defined in the toplevel of the current module;
  \item[B] Built-in names (i.e., Python's \texttt{\_\_builtins\_\_} module).
  \end{description}

  \+
  \textbf{Any name that is not in one of the above scopes \emph{must}
    be qualified.}

  \+
  So you have to write \texttt{self.assertEqual} to call a method on
  this object, \texttt{ut.TestCase} to mean a class defined in module
  \texttt{ut}, etc.

  % \begin{references}
  %   \url{http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules/292502#292502}
  % \end{references}
\end{frame}



\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 

