day1/Session-4.tex
changeset 59 9aac0f128482
parent 54 c3fe152b3539
child 61 fc495fd7e84e
child 63 f5eac04a00fe
equal deleted inserted replaced
55:09c73bf81987 59:9aac0f128482
   135     \item can we have duplicate keys?
   135     \item can we have duplicate keys?
   136   \end{itemize}
   136   \end{itemize}
   137   \inctime{5}
   137   \inctime{5}
   138 \end{frame}
   138 \end{frame}
   139 
   139 
   140 \begin{frame} {Problem Set 2.1}
   140 \begin{frame} {Problem Set 6.1}
   141   \begin{description}
   141   \begin{description}
   142 \item[2.1.1] You are given date strings of the form ``29, Jul 2009'', or ``4 January 2008''. In other words a number a string and another number, with a comma sometimes separating the items.Write a function that takes such a string and returns a tuple (yyyy, mm, dd) where all three elements are ints.
   142 \item[6.1.1] You are given date strings of the form ``29, Jul 2009'', or ``4 January 2008''. In other words a number a string and another number, with a comma sometimes separating the items.Write a function that takes such a string and returns a tuple (yyyy, mm, dd) where all three elements are ints.
   143     \item[2.1.2] Count word frequencies in a file.
   143     \item[6.1.2] Count word frequencies in a file.
   144     \item[2.1.3] Find the most used Python keywords in your Python code (import keyword).
   144     \item[6.1.3] Find the most used Python keywords in your Python code (import keyword).
   145 \end{description}
   145 \end{description}
   146 
   146 
   147 \inctime{10}
   147 \inctime{10}
   148 \end{frame}
   148 \end{frame}
   149 
   149 
   187 \inctime{5}
   187 \inctime{5}
   188 \end{frame}
   188 \end{frame}
   189 
   189 
   190 
   190 
   191 \begin{frame}
   191 \begin{frame}
   192   \frametitle{Problem set 2.2}
   192   \frametitle{Problem set 6.2}
   193   \begin{description}
   193   \begin{description}
   194     \item[2.2.1] Given a dictionary of the names of students and their marks, identify how many duplicate marks are there? and what are these?
   194     \item[2.2.1] Given a dictionary of the names of students and their marks, identify how many duplicate marks are there? and what are these?
   195     \item[2.2.2] Given a string of the form ``4-7, 9, 12, 15'' find the numbers missing in this list for a given range.
   195     \item[2.2.2] Given a string of the form ``4-7, 9, 12, 15'' find the numbers missing in this list for a given range.
   196 \end{description}
   196 \end{description}
   197 \inctime{10}
   197 \inctime{10}
   241                   'nope'): 
   241                   'nope'): 
   242             return False
   242             return False
   243         print complaint
   243         print complaint
   244 
   244 
   245 ask_ok(prompt='?')
   245 ask_ok(prompt='?')
   246 ask_ok(prompt='?', complaint='[Y/N]')
   246 ask_ok(prompt='?', complaint='[y/n]')
   247 ask_ok(complaint='[Y/N]', prompt='?')
   247 ask_ok(complaint='[y/n]', prompt='?')
   248 \end{lstlisting}
   248 \end{lstlisting}
   249 \inctime{15} 
   249 \inctime{15} 
   250 \end{frame}
   250 \end{frame}
   251 
   251 
   252 \subsection{Functional programming}
   252 \subsection{Functional programming}
   297 \subsection{Debugging}
   297 \subsection{Debugging}
   298 \begin{frame}[fragile]
   298 \begin{frame}[fragile]
   299  \frametitle{Errors}
   299  \frametitle{Errors}
   300  \begin{lstlisting}
   300  \begin{lstlisting}
   301 >>> while True print 'Hello world'
   301 >>> while True print 'Hello world'
       
   302  \end{lstlisting}
       
   303 \pause
       
   304   \begin{lstlisting}
   302   File "<stdin>", line 1, in ?
   305   File "<stdin>", line 1, in ?
   303     while True print 'Hello world'
   306     while True print 'Hello world'
   304                    ^
   307                    ^
   305 SyntaxError: invalid syntax
   308 SyntaxError: invalid syntax
   306 \end{lstlisting}
   309 \end{lstlisting}
   308 
   311 
   309 \begin{frame}[fragile]
   312 \begin{frame}[fragile]
   310  \frametitle{Exceptions}
   313  \frametitle{Exceptions}
   311  \begin{lstlisting}
   314  \begin{lstlisting}
   312 >>> print spam
   315 >>> print spam
       
   316 \end{lstlisting}
       
   317 \pause
       
   318 \begin{lstlisting}
   313 Traceback (most recent call last):
   319 Traceback (most recent call last):
   314   File "<stdin>", line 1, in <module>
   320   File "<stdin>", line 1, in <module>
   315 NameError: name 'spam' is not defined
   321 NameError: name 'spam' is not defined
   316 
   322 \end{lstlisting}
       
   323 \end{frame}
       
   324 
       
   325 \begin{frame}[fragile]
       
   326  \frametitle{Exceptions}
       
   327  \begin{lstlisting}
   317 >>> 1 / 0
   328 >>> 1 / 0
       
   329 \end{lstlisting}
       
   330 \pause
       
   331 \begin{lstlisting}
   318 Traceback (most recent call last):
   332 Traceback (most recent call last):
   319   File "<stdin>", line 1, in <module>
   333   File "<stdin>", line 1, in <module>
   320 ZeroDivisionError: integer division 
   334 ZeroDivisionError: integer division 
   321 or modulo by zero
   335 or modulo by zero
   322 \end{lstlisting}
   336 \end{lstlisting}
   323 \end{frame}
   337 \end{frame}
   324 
   338 
   325 \begin{frame}[fragile]
   339 \begin{frame}[fragile]
   326     \frametitle{Debugging effectively}
   340     \frametitle{Debugging effectively}
   327 
       
   328     \begin{itemize}
   341     \begin{itemize}
   329         \item  \kwrd{print} based strategy
   342         \item  \kwrd{print} based strategy
   330         \item Process: Hypothesis, test, refine, rinse-repeat
   343         \item Process: Hypothesis, test, refine, rinse-repeat
   331         \item Using \typ{\%debug} and \typ{\%pdb} in IPython
       
   332     \end{itemize}
   344     \end{itemize}
   333 \end{frame}
   345 \inctime{15}
   334 
   346 \end{frame}
   335 \begin{frame}[fragile]
   347 
   336 \frametitle{Debugging: example}
   348 \begin{frame}[fragile]
   337 \small
   349     \frametitle{Debugging effectively}
   338 \begin{lstlisting}
   350     \begin{itemize}
   339 >>> import pdb
   351       \item Using \typ{\%debug} and \typ{\%pdb} in IPython
   340 >>> import mymodule
   352     \end{itemize}
   341 >>> pdb.run('mymodule.test()')
   353 \inctime{15}
   342 > <string>(1)<module>()
       
   343 (Pdb) continue
       
   344 Traceback (most recent call last):
       
   345   File "<stdin>", line 1, in <module>
       
   346   File "/usr/lib/python2.6/pdb.py", line 1207, in run
       
   347     Pdb().run(statement, globals, locals)
       
   348   File "/usr/lib/python2.6/bdb.py", line 368, in run
       
   349     exec cmd in globals, locals
       
   350   File "<string>", line 1, in <module>
       
   351   File "mymodule.py", line 2, in test
       
   352     print spam
       
   353 NameError: global name 'spam' is not defined
       
   354 \end{lstlisting}
       
   355 \end{frame}
       
   356 
       
   357 \begin{frame}[fragile]
       
   358 \frametitle{Debugging in IPython}
       
   359 \small
       
   360 \begin{lstlisting}
       
   361 In [1]: %pdb
       
   362 Automatic pdb calling has been turned ON
       
   363 In [2]: import mymodule
       
   364 In [3]: mymodule.test()
       
   365 ----------------------------------------------
       
   366 NameError    Traceback (most recent call last)
       
   367 /media/python/iitb/workshops/day1/<ipython console> in <module>()
       
   368 /media/python/iitb/workshops/day1/mymodule.pyc in test()
       
   369       1 def test():
       
   370 ----> 2     print spam
       
   371 NameError: global name 'spam' is not defined
       
   372 > /media/python/iitb/workshops/day1/mymodule.py(2)test()
       
   373       0     print spam
       
   374 ipdb>
       
   375 \end{lstlisting}
       
   376 \inctime{15} 
       
   377 \end{frame}
   354 \end{frame}
   378 
   355 
   379 \begin{frame}[fragile]
   356 \begin{frame}[fragile]
   380 \frametitle{Debugging: Exercise}
   357 \frametitle{Debugging: Exercise}
   381 \inctime{10}
   358 \inctime{10}