|
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
2 % Tutorial slides on Python. |
|
3 % |
|
4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in> |
|
5 % Copyright (c) 2005-2009, Prabhu Ramachandran |
|
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
7 |
|
8 \documentclass[compress,14pt]{beamer} |
|
9 % \documentclass[handout]{beamer} |
|
10 % \usepackage{pgfpages} |
|
11 % \pgfpagesuselayout{4 on 1}[a4paper,border, shrink=5mm,landscape] |
|
12 \usepackage{tikz} |
|
13 \newcommand{\hyperlinkmovie}{} |
|
14 %\usepackage{movie15} |
|
15 |
|
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
17 % Note that in presentation mode |
|
18 % \paperwidth 364.19536pt |
|
19 % \paperheight 273.14662pt |
|
20 % h/w = 0.888 |
|
21 |
|
22 |
|
23 \mode<presentation> |
|
24 { |
|
25 \usetheme{Warsaw} |
|
26 %\usetheme{Boadilla} |
|
27 %\usetheme{default} |
|
28 \useoutertheme{split} |
|
29 \setbeamercovered{transparent} |
|
30 } |
|
31 |
|
32 % To remove navigation symbols |
|
33 \setbeamertemplate{navigation symbols}{} |
|
34 |
|
35 \usepackage{amsmath} |
|
36 \usepackage[english]{babel} |
|
37 \usepackage[latin1]{inputenc} |
|
38 \usepackage{times} |
|
39 \usepackage[T1]{fontenc} |
|
40 |
|
41 % Taken from Fernando's slides. |
|
42 \usepackage{ae,aecompl} |
|
43 \usepackage{mathpazo,courier,euler} |
|
44 \usepackage[scaled=.95]{helvet} |
|
45 \usepackage{pgf} |
|
46 |
|
47 \definecolor{darkgreen}{rgb}{0,0.5,0} |
|
48 |
|
49 \usepackage{listings} |
|
50 \lstset{language=Python, |
|
51 basicstyle=\ttfamily\bfseries, |
|
52 commentstyle=\color{red}\itshape, |
|
53 stringstyle=\color{darkgreen}, |
|
54 showstringspaces=false, |
|
55 keywordstyle=\color{blue}\bfseries} |
|
56 |
|
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
58 % My Macros |
|
59 \setbeamercolor{postit}{bg=yellow,fg=black} |
|
60 \setbeamercolor{emphbar}{bg=blue!20, fg=black} |
|
61 \newcommand{\emphbar}[1] |
|
62 {\begin{beamercolorbox}[rounded=true]{emphbar} |
|
63 {#1} |
|
64 \end{beamercolorbox} |
|
65 } |
|
66 %{\centerline{\fcolorbox{gray!50} {blue!10}{ |
|
67 %\begin{minipage}{0.9\linewidth} |
|
68 % {#1} |
|
69 %\end{minipage} |
|
70 % }}} |
|
71 |
|
72 \newcommand{\myemph}[1]{\structure{\emph{#1}}} |
|
73 \newcommand{\PythonCode}[1]{\lstinline{#1}} |
|
74 |
|
75 \newcommand{\tvtk}{\texttt{tvtk}} |
|
76 \newcommand{\mlab}{\texttt{mlab}} |
|
77 |
|
78 \newcounter{time} |
|
79 \setcounter{time}{0} |
|
80 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\vspace*{0.1in}\tiny \thetime\ m}} |
|
81 |
|
82 \newcommand\BackgroundPicture[1]{% |
|
83 \setbeamertemplate{background}{% |
|
84 \parbox[c][\paperheight]{\paperwidth}{% |
|
85 \vfill \hfill |
|
86 \hfill \vfill |
|
87 }}} |
|
88 |
|
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
90 % Configuring the theme |
|
91 %\setbeamercolor{normal text}{fg=white} |
|
92 %\setbeamercolor{background canvas}{bg=black} |
|
93 |
|
94 |
|
95 |
|
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
97 % Title page |
|
98 \title[]{Test Driven Approach} |
|
99 |
|
100 \author[FOSSEE Team] {FOSSEE} |
|
101 |
|
102 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
103 \date[] {11, October 2009} |
|
104 \date[] % (optional) |
|
105 |
|
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
107 |
|
108 %\pgfdeclareimage[height=0.75cm]{iitblogo}{iitblogo} |
|
109 %\logo{\pgfuseimage{iitblogo}} |
|
110 |
|
111 \AtBeginSection[] |
|
112 { |
|
113 \begin{frame}<beamer> |
|
114 \frametitle{Outline} |
|
115 \Large |
|
116 \tableofcontents[currentsection,currentsubsection] |
|
117 \end{frame} |
|
118 } |
|
119 |
|
120 %% Delete this, if you do not want the table of contents to pop up at |
|
121 %% the beginning of each subsection: |
|
122 \AtBeginSubsection[] |
|
123 { |
|
124 \begin{frame}<beamer> |
|
125 \frametitle{Outline} |
|
126 \tableofcontents[currentsection,currentsubsection] |
|
127 \end{frame} |
|
128 } |
|
129 |
|
130 \AtBeginSection[] |
|
131 { |
|
132 \begin{frame}<beamer> |
|
133 \frametitle{Outline} |
|
134 \tableofcontents[currentsection,currentsubsection] |
|
135 \end{frame} |
|
136 } |
|
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
138 % DOCUMENT STARTS |
|
139 \begin{document} |
|
140 |
|
141 \begin{frame} |
|
142 \maketitle |
|
143 \end{frame} |
|
144 |
|
145 |
|
146 \section{Debugging} |
|
147 \subsection{Errors and Exceptions} |
|
148 \begin{frame}[fragile] |
|
149 \frametitle{Errors} |
|
150 \begin{lstlisting} |
|
151 >>> while True print 'Hello world' |
|
152 \end{lstlisting} |
|
153 \pause |
|
154 \begin{lstlisting} |
|
155 File "<stdin>", line 1, in ? |
|
156 while True print 'Hello world' |
|
157 ^ |
|
158 SyntaxError: invalid syntax |
|
159 \end{lstlisting} |
|
160 \end{frame} |
|
161 |
|
162 \begin{frame}[fragile] |
|
163 \frametitle{Exceptions} |
|
164 \begin{lstlisting} |
|
165 >>> print spam |
|
166 \end{lstlisting} |
|
167 \pause |
|
168 \begin{lstlisting} |
|
169 Traceback (most recent call last): |
|
170 File "<stdin>", line 1, in <module> |
|
171 NameError: name 'spam' is not defined |
|
172 \end{lstlisting} |
|
173 \end{frame} |
|
174 |
|
175 \begin{frame}[fragile] |
|
176 \frametitle{Exceptions} |
|
177 \begin{lstlisting} |
|
178 >>> 1 / 0 |
|
179 \end{lstlisting} |
|
180 \pause |
|
181 \begin{lstlisting} |
|
182 Traceback (most recent call last): |
|
183 File "<stdin>", line 1, in <module> |
|
184 ZeroDivisionError: integer division |
|
185 or modulo by zero |
|
186 \end{lstlisting} |
|
187 \end{frame} |
|
188 |
|
189 \subsection{Strategy} |
|
190 \begin{frame}[fragile] |
|
191 \frametitle{Debugging effectively} |
|
192 \begin{itemize} |
|
193 \item \kwrd{print} based strategy |
|
194 \item Process: |
|
195 \end{itemize} |
|
196 \pgfimage[interpolate=true,width=5cm,height=5cm]{DebugginDiagram.png} |
|
197 \end{frame} |
|
198 |
|
199 \begin{frame}[fragile] |
|
200 \frametitle{Debugging effectively} |
|
201 \begin{itemize} |
|
202 \item Using \typ{\%debug} in IPython |
|
203 \end{itemize} |
|
204 \end{frame} |
|
205 |
|
206 \begin{frame}[fragile] |
|
207 \frametitle{Debugging in IPython} |
|
208 \small |
|
209 \begin{lstlisting} |
|
210 In [1]: import mymodule |
|
211 In [2]: mymodule.test() |
|
212 --------------------------------------------- |
|
213 NameError Traceback (most recent call last) |
|
214 <ipython console> in <module>() |
|
215 mymodule.py in test() |
|
216 1 def test(): |
|
217 ----> 2 print spam |
|
218 NameError: global name 'spam' is not defined |
|
219 |
|
220 In [3]: %debug |
|
221 > mymodule.py(2)test() |
|
222 0 print spam |
|
223 ipdb> |
|
224 \end{lstlisting} |
|
225 \inctime{15} |
|
226 \end{frame} |
|
227 |
|
228 \subsection{Exercise} |
|
229 \begin{frame}[fragile] |
|
230 \frametitle{Debugging: Exercise} |
|
231 \small |
|
232 \begin{lstlisting} |
|
233 import keyword |
|
234 f = open('/path/to/file') |
|
235 |
|
236 freq = {} |
|
237 for line in f: |
|
238 words = line.split() |
|
239 for word in words: |
|
240 key = word.strip(',.!;?()[]: ') |
|
241 if keyword.iskeyword(key): |
|
242 value = freq[key] |
|
243 freq[key] = value + 1 |
|
244 |
|
245 print freq |
|
246 \end{lstlisting} |
|
247 \inctime{10} |
|
248 \end{frame} |
|
249 |
|
250 %% \begin{frame} |
|
251 %% \frametitle{Testing} |
|
252 |
|
253 %% \begin{itemize} |
|
254 %% \item Writing tests is really simple! |
|
255 |
|
256 %% \item Using nose. |
|
257 |
|
258 %% \item Example! |
|
259 %% \end{itemize} |
|
260 %% \end{frame} |
|
261 |
|
262 \section{Test Driven Approach} |
|
263 \begin{frame} |
|
264 \frametitle{Need of Testing!} |
|
265 |
|
266 \begin{itemize} |
|
267 \item Quality |
|
268 \item Regression |
|
269 \item Documentation |
|
270 \end{itemize} |
|
271 %% \vspace*{0.25in} |
|
272 %% \emphbar{It is to assure that section of code is working as it is supposed to work} |
|
273 \end{frame} |
|
274 |
|
275 \begin{frame}[fragile] |
|
276 \frametitle{Example} |
|
277 \begin{block}{Problem Statement} |
|
278 Write a function to check whether a given input |
|
279 string is a palindrome. |
|
280 \end{block} |
|
281 \end{frame} |
|
282 |
|
283 \begin{frame}[fragile] |
|
284 \frametitle{Function: palindrome.py} |
|
285 \begin{lstlisting} |
|
286 def is_palindrome(input_str): |
|
287 return input_str == input_str[::-1] |
|
288 \end{lstlisting} |
|
289 \end{frame} |
|
290 |
|
291 \begin{frame}[fragile] |
|
292 \frametitle{Test for the palindrome: palindrome.py} |
|
293 \begin{lstlisting} |
|
294 from plaindrome import is_palindrome |
|
295 def test_function_normal_words(): |
|
296 input = "noon" |
|
297 assert is_palindrome(input) == True |
|
298 \end{lstlisting} |
|
299 \end{frame} |
|
300 |
|
301 \begin{frame}[fragile] |
|
302 \frametitle{Running the tests.} |
|
303 \begin{lstlisting} |
|
304 $ nosetests test.py |
|
305 . |
|
306 ---------------------------------------------- |
|
307 Ran 1 test in 0.001s |
|
308 |
|
309 OK |
|
310 \end{lstlisting} |
|
311 \end{frame} |
|
312 |
|
313 \begin{frame}[fragile] |
|
314 \frametitle{Exercise: Including new tests.} |
|
315 \begin{lstlisting} |
|
316 def test_function_ignore_cases_words(): |
|
317 input = "Noon" |
|
318 assert is_palindrome(input) == True |
|
319 \end{lstlisting} |
|
320 \vspace*{0.25in} |
|
321 Check\\ |
|
322 \PythonCode{$ nosetests test.py} \\ |
|
323 \begin{block}{Task} |
|
324 Tweak the code to pass this test. |
|
325 \end{block} |
|
326 \end{frame} |
|
327 |
|
328 %\begin{frame}[fragile] |
|
329 % \frametitle{Lets write some test!} |
|
330 %\begin{lstlisting} |
|
331 %#for form of equation y=mx+c |
|
332 %#given m and c for two equation, |
|
333 %#finding the intersection point. |
|
334 %def intersect(m1,c1,m2,c2): |
|
335 % x = (c2-c1)/(m1-m2) |
|
336 % y = m1*x+c1 |
|
337 % return (x,y) |
|
338 %\end{lstlisting} |
|
339 % |
|
340 %Create a simple test for this |
|
341 % |
|
342 %function which will make it fail. |
|
343 % |
|
344 %\inctime{15} |
|
345 %\end{frame} |
|
346 % |
|
347 |
|
348 \begin{frame}[fragile] |
|
349 \frametitle{Exercise} |
|
350 Based on Euclid's algorithm: |
|
351 \begin{center} |
|
352 $gcd(a,b)=gcd(b,b\%a)$ |
|
353 \end{center} |
|
354 gcd function can be written as: |
|
355 \begin{lstlisting} |
|
356 def gcd(a, b): |
|
357 if a%b == 0: return b |
|
358 return gcd(b, a%b) |
|
359 \end{lstlisting} |
|
360 \vspace*{-0.15in} |
|
361 \begin{block}{Task} |
|
362 \begin{itemize} |
|
363 \item Write at least |
|
364 two tests for above mentioned function. |
|
365 \item Write a non recursive implementation |
|
366 of gcd(), and test it using already |
|
367 written tests. |
|
368 \end{itemize} |
|
369 \end{block} |
|
370 |
|
371 \inctime{15} |
|
372 \end{frame} |
|
373 |
|
374 \end{document} |