1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
2 % Tutorial slides on Python. |
2 %Tutorial slides on Python. |
3 % |
3 % |
4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in> |
4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in> |
5 % Copyright (c) 2005-2009, Prabhu Ramachandran |
5 % Copyright (c) 2005-2009, Prabhu Ramachandran |
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
7 |
7 |
8 \documentclass[compress,14pt]{beamer} |
8 \documentclass[14pt,compress]{beamer} |
9 % \documentclass[handout]{beamer} |
9 %\documentclass[draft]{beamer} |
10 % \usepackage{pgfpages} |
10 %\documentclass[compress,handout]{beamer} |
11 % \pgfpagesuselayout{4 on 1}[a4paper,border, shrink=5mm,landscape] |
11 %\usepackage{pgfpages} |
12 \usepackage{tikz} |
12 %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] |
13 \newcommand{\hyperlinkmovie}{} |
13 |
14 %\usepackage{movie15} |
14 % Modified from: generic-ornate-15min-45min.de.tex |
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> |
15 \mode<presentation> |
24 { |
16 { |
25 \usetheme{Warsaw} |
17 \usetheme{Warsaw} |
26 %\usetheme{Boadilla} |
|
27 %\usetheme{default} |
|
28 \useoutertheme{infolines} |
18 \useoutertheme{infolines} |
29 \setbeamercovered{transparent} |
19 \setbeamercovered{transparent} |
30 } |
20 } |
31 |
21 |
32 % To remove navigation symbols |
|
33 \setbeamertemplate{navigation symbols}{} |
|
34 |
|
35 \usepackage{amsmath} |
|
36 \usepackage[english]{babel} |
22 \usepackage[english]{babel} |
37 \usepackage[latin1]{inputenc} |
23 \usepackage[latin1]{inputenc} |
38 \usepackage{times} |
24 %\usepackage{times} |
39 \usepackage[T1]{fontenc} |
25 \usepackage[T1]{fontenc} |
40 |
26 |
41 % Taken from Fernando's slides. |
27 % Taken from Fernando's slides. |
42 \usepackage{ae,aecompl} |
28 \usepackage{ae,aecompl} |
43 \usepackage{mathpazo,courier,euler} |
29 \usepackage{mathpazo,courier,euler} |
44 \usepackage[scaled=.95]{helvet} |
30 \usepackage[scaled=.95]{helvet} |
45 \usepackage{pgf} |
|
46 |
31 |
47 \definecolor{darkgreen}{rgb}{0,0.5,0} |
32 \definecolor{darkgreen}{rgb}{0,0.5,0} |
48 |
33 |
49 \usepackage{listings} |
34 \usepackage{listings} |
50 \lstset{language=Python, |
35 \lstset{language=Python, |
53 stringstyle=\color{darkgreen}, |
38 stringstyle=\color{darkgreen}, |
54 showstringspaces=false, |
39 showstringspaces=false, |
55 keywordstyle=\color{blue}\bfseries} |
40 keywordstyle=\color{blue}\bfseries} |
56 |
41 |
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
58 % My Macros |
43 % Macros |
59 \setbeamercolor{postit}{bg=yellow,fg=black} |
|
60 \setbeamercolor{emphbar}{bg=blue!20, fg=black} |
44 \setbeamercolor{emphbar}{bg=blue!20, fg=black} |
61 \newcommand{\emphbar}[1] |
45 \newcommand{\emphbar}[1] |
62 {\begin{beamercolorbox}[rounded=true]{emphbar} |
46 {\begin{beamercolorbox}[rounded=true]{emphbar} |
63 {#1} |
47 {#1} |
64 \end{beamercolorbox} |
48 \end{beamercolorbox} |
65 } |
49 } |
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 \newcommand{\typ}[1]{\lstinline{#1}} |
|
78 \newcounter{time} |
50 \newcounter{time} |
79 \setcounter{time}{0} |
51 \setcounter{time}{0} |
80 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\vspace*{0.1in}\tiny \thetime\ m}} |
52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} |
81 |
53 |
82 \newcommand\BackgroundPicture[1]{% |
54 \newcommand{\typ}[1]{\texttt{#1}} |
83 \setbeamertemplate{background}{% |
55 |
84 \parbox[c][\paperheight]{\paperwidth}{% |
56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } |
85 \vfill \hfill |
57 |
86 \hfill \vfill |
58 %%% This is from Fernando's setup. |
87 }}} |
59 % \usepackage{color} |
88 |
60 % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
61 % % Use and configure listings package for nicely formatted code |
90 % Configuring the theme |
62 % \usepackage{listings} |
91 %\setbeamercolor{normal text}{fg=white} |
63 % \lstset{ |
92 %\setbeamercolor{background canvas}{bg=black} |
64 % language=Python, |
93 |
65 % basicstyle=\small\ttfamily, |
|
66 % commentstyle=\ttfamily\color{blue}, |
|
67 % stringstyle=\ttfamily\color{orange}, |
|
68 % showstringspaces=false, |
|
69 % breaklines=true, |
|
70 % postbreak = \space\dots |
|
71 % } |
94 |
72 |
95 |
73 |
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
97 % Title page |
75 % Title page |
98 \title[Python Development]{Python Development} |
76 \title[Exercises]{Exercises} |
99 |
77 |
100 \author[FOSSEE] {FOSSEE} |
78 \author[FOSSEE] {FOSSEE} |
101 |
79 |
102 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
103 \date[] {1 November, 2009\\Day 2, Session 3} |
81 \date[] {8 November, 2009\\Day 2, Session 5} |
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
105 |
83 |
106 %\pgfdeclareimage[height=0.75cm]{iitblogo}{iitblogo} |
84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
107 %\logo{\pgfuseimage{iitblogo}} |
85 %\logo{\pgfuseimage{iitmlogo}} |
108 |
86 |
109 \AtBeginSection[] |
|
110 { |
|
111 \begin{frame}<beamer> |
|
112 \frametitle{Outline} |
|
113 \Large |
|
114 \tableofcontents[currentsection,currentsubsection] |
|
115 \end{frame} |
|
116 } |
|
117 |
87 |
118 %% Delete this, if you do not want the table of contents to pop up at |
88 %% Delete this, if you do not want the table of contents to pop up at |
119 %% the beginning of each subsection: |
89 %% the beginning of each subsection: |
120 \AtBeginSubsection[] |
90 \AtBeginSubsection[] |
121 { |
91 { |
123 \frametitle{Outline} |
93 \frametitle{Outline} |
124 \tableofcontents[currentsection,currentsubsection] |
94 \tableofcontents[currentsection,currentsubsection] |
125 \end{frame} |
95 \end{frame} |
126 } |
96 } |
127 |
97 |
128 \AtBeginSection[] |
98 |
129 { |
99 % If you wish to uncover everything in a step-wise fashion, uncomment |
130 \begin{frame}<beamer> |
100 % the following command: |
131 \frametitle{Outline} |
101 %\beamerdefaultoverlayspecification{<+->} |
132 \tableofcontents[currentsection,currentsubsection] |
102 |
133 \end{frame} |
103 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
134 } |
104 |
135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
136 % DOCUMENT STARTS |
106 % DOCUMENT STARTS |
137 \begin{document} |
107 \begin{document} |
138 |
108 |
139 \begin{frame} |
109 \begin{frame} |
140 \maketitle |
110 \titlepage |
141 \end{frame} |
111 \end{frame} |
142 |
112 |
143 \section{Tests: Getting started} |
113 \begin{frame}{Problem 1.1} |
144 \begin{frame}[fragile] |
114 The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. \\For example: |
145 \frametitle{gcd revisited!} |
115 \center{aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.}\\ |
146 \begin{itemize} |
116 Write a function that returns the aliquot number of a given number. |
147 \item Open gcd.py |
117 \end{frame} |
148 \end{itemize} |
118 |
149 \begin{lstlisting} |
119 \begin{frame}{Problem 1.2} |
150 def gcd(a, b): |
120 Pair of numbers (a, b) is said to be \alert{amicable} if aliquot number of a is b and aliquot number of b is a.\\ |
151 if a % b == 0: |
121 Example: \texttt{220, 284}\\ |
152 return b |
122 Write a program that prints all four digit amicable pairs. |
153 return gcd(b, a%b) |
123 |
154 |
124 \inctime{20} |
155 print gcd(15, 65) |
125 \end{frame} |
156 print gcd(16, 76) |
126 |
157 \end{lstlisting} |
127 %% \begin{frame}{Problem 2} |
158 \begin{itemize} |
128 %% Given an empty chessboard and one Bishop placed in any s%quare, say (r, c), generate the list of all squares the Bi%shop could move to. |
159 \item python gcd.py |
129 |
160 \end{itemize} |
130 %% \end{frame} |
161 \end{frame} |
131 |
162 |
132 \begin{frame}[fragile] |
163 \begin{frame}[fragile] |
133 \frametitle{Problem Set 2} |
164 \frametitle{Find lcm using our gcd module} |
134 Given a string like, ``1, 3-7, 12, 15, 18-21'', produce the list \\ |
165 \begin{itemize} |
|
166 \item Open lcm.py |
|
167 \item $lcm = \frac{a*b}{gcd(a,b)}$ |
|
168 \end{itemize} |
|
169 \begin{lstlisting} |
|
170 from gcd import gcd |
|
171 def lcm(a, b): |
|
172 return (a * b) / gcd(a, b) |
|
173 |
|
174 print lcm(14, 56) |
|
175 \end{lstlisting} |
|
176 \begin{itemize} |
|
177 \item python lcm.py |
|
178 \end{itemize} |
|
179 \begin{lstlisting} |
135 \begin{lstlisting} |
180 5 |
136 [1,3,4,5,6,7,12,15,18,19,20,21] |
181 4 |
137 \end{lstlisting} |
182 56 |
138 \inctime{10} |
183 \end{lstlisting} |
139 \end{frame} |
184 \end{frame} |
140 |
185 |
141 \begin{frame} |
186 \begin{frame}[fragile] |
142 \frametitle{Problem Set 3} |
187 \frametitle{Writing stand-alone module} |
143 \begin{description} |
188 Edit gcd.py file to: |
144 \item[3.1] Count word frequencies in a file. |
189 \begin{lstlisting} |
145 \end{description} |
190 def gcd(a, b): |
146 \inctime{5} |
191 if a % b == 0: |
|
192 return b |
|
193 return gcd(b, a%b) |
|
194 |
|
195 if __name__ == "__main__": |
|
196 print gcd(15, 65) |
|
197 print gcd(16, 76) |
|
198 \end{lstlisting} |
|
199 \begin{itemize} |
|
200 \item python gcd.py |
|
201 \item python lcm.py |
|
202 \end{itemize} |
|
203 \end{frame} |
147 \end{frame} |
204 |
148 |
205 \begin{frame}[fragile] |
149 \begin{frame}[fragile] |
206 \frametitle{More use of main} |
150 \frametitle{Problem set 4} |
207 For automating tests. |
151 Central difference |
|
152 \begin{equation*} |
|
153 \frac{sin(x+h)-sin(x-h)}{2h} |
|
154 \end{equation*} |
208 \begin{lstlisting} |
155 \begin{lstlisting} |
209 if __name__ == '__main__': |
156 In []: x = linspace(0, 2*pi, 100) |
210 for line in open('numbers.txt'): |
157 In []: y = sin(x) |
211 numbers = line.split() |
158 In []: deltax = x[1] - x[0] |
212 x = int(numbers[0]) |
|
213 y = int(numbers[1]) |
|
214 result = (int(numbers[2])) |
|
215 assert gcd(x, y) == result |
|
216 \end{lstlisting} |
|
217 \end{frame} |
|
218 |
|
219 \section{Coding Style} |
|
220 \begin{frame}{Readability and Consistency} |
|
221 \begin{itemize} |
|
222 \item Readability Counts!\\Code is read more often than its written. |
|
223 \item Consistency! |
|
224 \item Know when to be inconsistent. |
|
225 \end{itemize} |
|
226 \end{frame} |
|
227 |
|
228 \begin{frame}[fragile] \frametitle{A question of good style} |
|
229 \begin{lstlisting} |
|
230 amount = 12.68 |
|
231 denom = 0.05 |
|
232 nCoins = round(amount/denom) |
|
233 rAmount = nCoins * denom |
|
234 \end{lstlisting} |
159 \end{lstlisting} |
235 \pause |
160 \pause |
236 \begin{block}{Style Rule \#1} |
161 \begin{enumerate} |
237 Naming is 80\% of programming |
162 \item Given this, get the finite difference of sin in the range 0 to 2*pi |
238 \end{block} |
163 \end{enumerate} |
239 \end{frame} |
164 \end{frame} |
240 |
165 |
241 \begin{frame}[fragile] |
166 \begin{frame} |
242 \frametitle{Code Layout} |
167 \frametitle{Problem Set 5} |
243 \begin{itemize} |
168 \begin{itemize} |
244 \item Indentation |
169 \item[5.1] Write a function that plots any regular n-gon given \typ{n}. |
245 \item Tabs or Spaces?? |
170 \item[5.2] Consider the logistic map, $f(x) = kx(1-x)$, plot it for |
246 \item Maximum Line Length |
171 $k=2.5, 3.5$ and $4$ in the same plot. |
247 \item Blank Lines |
172 \end{itemize} |
248 \item Encodings |
173 \end{frame} |
249 \end{itemize} |
174 |
250 \end{frame} |
175 \begin{frame}[fragile] |
251 |
176 \frametitle{Problem Set 5} |
252 \begin{frame}{Whitespaces in Expressions} |
177 \begin{columns} |
253 \begin{itemize} |
178 \column{0.6\textwidth} |
254 \item When to use extraneous whitespaces?? |
179 \small{ |
255 \item When to avoid extra whitespaces?? |
|
256 \item Use one statement per line |
|
257 \end{itemize} |
|
258 \end{frame} |
|
259 |
|
260 \begin{frame}{Comments} |
|
261 \begin{itemize} |
|
262 \item No comments better than contradicting comments |
|
263 \item Block comments |
|
264 \item Inline comments |
|
265 \end{itemize} |
|
266 \end{frame} |
|
267 |
|
268 \begin{frame}{Docstrings} |
|
269 \begin{itemize} |
|
270 \item When to write docstrings? |
|
271 \item Ending the docstrings |
|
272 \item One liner docstrings |
|
273 \end{itemize} |
|
274 More information at PEP8: http://www.python.org/dev/peps/pep-0008/ |
|
275 \inctime{5} |
|
276 \end{frame} |
|
277 |
|
278 \section{Debugging} |
|
279 \subsection{Errors and Exceptions} |
|
280 \begin{frame}[fragile] |
|
281 \frametitle{Errors} |
|
282 \begin{lstlisting} |
|
283 In []: while True print 'Hello world' |
|
284 \end{lstlisting} |
|
285 \pause |
|
286 \begin{lstlisting} |
|
287 File "<stdin>", line 1, in ? |
|
288 while True print 'Hello world' |
|
289 ^ |
|
290 SyntaxError: invalid syntax |
|
291 \end{lstlisting} |
|
292 \end{frame} |
|
293 |
|
294 \begin{frame}[fragile] |
|
295 \frametitle{Exceptions} |
|
296 \begin{lstlisting} |
|
297 In []: print spam |
|
298 \end{lstlisting} |
|
299 \pause |
|
300 \begin{lstlisting} |
|
301 Traceback (most recent call last): |
|
302 File "<stdin>", line 1, in <module> |
|
303 NameError: name 'spam' is not defined |
|
304 \end{lstlisting} |
|
305 \end{frame} |
|
306 |
|
307 \begin{frame}[fragile] |
|
308 \frametitle{Exceptions} |
|
309 \begin{lstlisting} |
|
310 In []: 1 / 0 |
|
311 \end{lstlisting} |
|
312 \pause |
|
313 \begin{lstlisting} |
|
314 Traceback (most recent call last): |
|
315 File "<stdin>", line 1, in <module> |
|
316 ZeroDivisionError: integer division |
|
317 or modulo by zero |
|
318 \end{lstlisting} |
|
319 \end{frame} |
|
320 |
|
321 \begin{frame}[fragile] |
|
322 \frametitle{Handling Exceptions} |
|
323 Python uses \typ{try} and \typ{except} clause. |
|
324 %%Revisiting the raw\_input |
|
325 \begin{lstlisting} |
|
326 a = raw_input('Enter number(Q to quit):') |
|
327 try: |
|
328 num = int(a) |
|
329 print num |
|
330 except: |
|
331 if a == 'Q': |
|
332 print 'Exiting...' |
|
333 else: |
|
334 print 'Wrong input!' |
|
335 \end{lstlisting} |
|
336 |
|
337 |
|
338 \end{frame} |
|
339 |
|
340 %% \begin{frame}[fragile] |
|
341 %% \frametitle{Solving it with \typ{try} and \typ{except}} |
|
342 %% \vspace{-0.2in} |
|
343 %% \begin{lstlisting} |
|
344 %% highest = 0 |
|
345 %% for record in open('sslc1.txt'): |
|
346 %% fields = record.split(';') |
|
347 %% try: |
|
348 %% total = 0 |
|
349 %% for score_str in fields[3:8]: |
|
350 %% score = int(score_str) |
|
351 %% total += score |
|
352 %% if total > highest: |
|
353 %% highest = total |
|
354 %% except: |
|
355 %% pass |
|
356 %% print highest |
|
357 %% \end{lstlisting} |
|
358 %% \end{frame} |
|
359 \subsection{Strategy} |
|
360 \begin{frame}[fragile] |
|
361 \frametitle{Debugging effectively} |
|
362 \begin{itemize} |
180 \begin{itemize} |
363 \item \typ{print} based strategy |
181 \item[3] Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) = kx(1-x)$. Plot the successive iterates of this process as explained below. |
364 \item Process: |
182 \end{itemize}} |
365 \end{itemize} |
183 \column{0.35\textwidth} |
366 \begin{center} |
184 \hspace*{-0.5in} |
367 \pgfimage[interpolate=true,width=5cm,height=5cm]{DebugginDiagram.png} |
185 \includegraphics[height=1.6in, interpolate=true]{data/cobweb} |
368 \end{center} |
186 \end{columns} |
369 \end{frame} |
187 \end{frame} |
370 |
188 |
371 \begin{frame}[fragile] |
|
372 \frametitle{Debugging effectively} |
|
373 \begin{itemize} |
|
374 \item Using \typ{\%debug} in IPython |
|
375 \end{itemize} |
|
376 \end{frame} |
|
377 |
|
378 \begin{frame}[fragile] |
|
379 \frametitle{Debugging in IPython} |
|
380 \small |
|
381 \begin{lstlisting} |
|
382 In []: import mymodule |
|
383 In []: mymodule.test() |
|
384 --------------------------------------------- |
|
385 NameError Traceback (most recent call last) |
|
386 <ipython console> in <module>() |
|
387 mymodule.py in test() |
|
388 1 def test(): |
|
389 ----> 2 print spam |
|
390 NameError: global name 'spam' is not defined |
|
391 |
|
392 In []: %debug |
|
393 > mymodule.py(2)test() |
|
394 0 print spam |
|
395 ipdb> |
|
396 \end{lstlisting} |
|
397 \inctime{15} |
|
398 \end{frame} |
|
399 |
|
400 \subsection{Exercise} |
|
401 \begin{frame}[fragile] |
|
402 \frametitle{Debugging: Exercise} |
|
403 \small |
|
404 \begin{lstlisting} |
|
405 science = {} |
|
406 |
|
407 for record in open('sslc1.txt'): |
|
408 fields = record.split(';') |
|
409 region_code = fields[0].strip() |
|
410 |
|
411 score_str = fields[6].strip() |
|
412 score = int(score_str) if score_str != 'AA' |
|
413 else 0 |
|
414 |
|
415 if score > 90: |
|
416 science[region_code] += 1 |
|
417 |
|
418 pie(science.values(), labels=science.keys()) |
|
419 savefig('science.png') |
|
420 \end{lstlisting} |
|
421 \inctime{10} |
|
422 \end{frame} |
|
423 |
|
424 %% \begin{frame} |
|
425 %% \frametitle{Testing} |
|
426 |
|
427 %% \begin{itemize} |
|
428 %% \item Writing tests is really simple! |
|
429 |
|
430 %% \item Using nose. |
|
431 |
|
432 %% \item Example! |
|
433 %% \end{itemize} |
|
434 %% \end{frame} |
|
435 |
|
436 \section{Test Driven Approach} |
|
437 \begin{frame} |
189 \begin{frame} |
438 \frametitle{Need for Testing!} |
190 \frametitle{Problem Set 5.3} |
439 |
191 Plot the cobweb plot as follows: |
440 \begin{itemize} |
192 \begin{enumerate} |
441 \item Quality |
193 \item Start at $(x_0, 0)$ ($\implies$ i=0) |
442 \item Regression |
194 \item Draw a line to $(x_i, f(x_i))$ |
443 \item Documentation |
195 \item Set $x_{i+1} = f(x_i)$ |
444 \end{itemize} |
196 \item Draw a line to $(x_{i+1}, x_{i+1})$ |
445 %% \vspace*{0.25in} |
197 \item $(i\implies i+1)$ |
446 %% \emphbar{It is to assure that section of code is working as it is supposed to work} |
198 \item Repeat from 2 for as long as you want |
447 \end{frame} |
199 \end{enumerate} |
448 |
200 \inctime{20} |
449 \begin{frame}[fragile] |
|
450 \frametitle{Example} |
|
451 \begin{block}{Problem Statement} |
|
452 Write a function to check whether a given input |
|
453 string is a palindrome. |
|
454 \end{block} |
|
455 \end{frame} |
|
456 |
|
457 \begin{frame}[fragile] |
|
458 \frametitle{Function: palindrome.py} |
|
459 \begin{lstlisting} |
|
460 def is_palindrome(input_str): |
|
461 return input_str == input_str[::-1] |
|
462 \end{lstlisting} |
|
463 \end{frame} |
|
464 |
|
465 \begin{frame}[fragile] |
|
466 \frametitle{Test for the palindrome: palindrome.py} |
|
467 \begin{lstlisting} |
|
468 def test_function_normal_words(): |
|
469 input = "noon" |
|
470 assert is_palindrome(input) == True |
|
471 |
|
472 if __name__ == "main'': |
|
473 test_function_normal_words() |
|
474 \end{lstlisting} |
|
475 \end{frame} |
|
476 |
|
477 \begin{frame}[fragile] |
|
478 \frametitle{Running the tests.} |
|
479 \begin{lstlisting} |
|
480 $ nosetests palindrome.py |
|
481 . |
|
482 ---------------------------------------------- |
|
483 Ran 1 test in 0.001s |
|
484 |
|
485 OK |
|
486 \end{lstlisting} |
|
487 \end{frame} |
|
488 |
|
489 \begin{frame}[fragile] |
|
490 \frametitle{Exercise: Including new tests.} |
|
491 \begin{lstlisting} |
|
492 def test_function_ignore_cases_words(): |
|
493 input = "Noon" |
|
494 assert is_palindrome(input) == True |
|
495 \end{lstlisting} |
|
496 \vspace*{0.25in} |
|
497 Check\\ |
|
498 \PythonCode{$ nosetests palindrome.py} \\ |
|
499 \begin{block}{Task} |
|
500 Tweak the code to pass this test. |
|
501 \end{block} |
|
502 \end{frame} |
|
503 |
|
504 %\begin{frame}[fragile] |
|
505 % \frametitle{Lets write some test!} |
|
506 %\begin{lstlisting} |
|
507 %#for form of equation y=mx+c |
|
508 %#given m and c for two equation, |
|
509 %#finding the intersection point. |
|
510 %def intersect(m1,c1,m2,c2): |
|
511 % x = (c2-c1)/(m1-m2) |
|
512 % y = m1*x+c1 |
|
513 % return (x,y) |
|
514 %\end{lstlisting} |
|
515 % |
|
516 %Create a simple test for this |
|
517 % |
|
518 %function which will make it fail. |
|
519 % |
|
520 %\inctime{15} |
|
521 %\end{frame} |
|
522 % |
|
523 |
|
524 %% \begin{frame}[fragile] |
|
525 %% \frametitle{Exercise} |
|
526 %% Based on Euclid's algorithm: |
|
527 %% \begin{center} |
|
528 %% $gcd(a,b)=gcd(b,b\%a)$ |
|
529 %% \end{center} |
|
530 %% gcd function can be written as: |
|
531 %% \begin{lstlisting} |
|
532 %% def gcd(a, b): |
|
533 %% if a%b == 0: return b |
|
534 %% return gcd(b, a%b) |
|
535 %% \end{lstlisting} |
|
536 %% \vspace*{-0.15in} |
|
537 %% \begin{block}{Task} |
|
538 %% \begin{itemize} |
|
539 %% \item Write at least |
|
540 %% two tests for above mentioned function. |
|
541 %% \item Write a non recursive implementation |
|
542 %% of gcd(), and test it using already |
|
543 %% written tests. |
|
544 %% \end{itemize} |
|
545 %% \end{block} |
|
546 |
|
547 %% \inctime{15} |
|
548 %% \end{frame} |
|
549 |
|
550 \begin{frame} |
|
551 \frametitle{Summary} |
|
552 We have coverd: |
|
553 \begin{itemize} |
|
554 \item Following and Resolving Error Messages. |
|
555 \item Exceptions. |
|
556 \item Handling exceptions |
|
557 \item Approach for Debugging. |
|
558 \item Writting and running tests. |
|
559 \end{itemize} |
|
560 \end{frame} |
201 \end{frame} |
561 |
202 |
562 \end{document} |
203 \end{document} |