|
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] {Asokan Pichai\\Prabhu Ramachandran} |
|
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 \section{Test Driven Approach} |
|
146 |
|
147 \begin{frame} |
|
148 \frametitle{Testing code with \typ{nosetests}} |
|
149 |
|
150 \begin{itemize} |
|
151 \item Writing tests is really simple! |
|
152 |
|
153 \item Using nose. |
|
154 |
|
155 \item Example! |
|
156 \end{itemize} |
|
157 \end{frame} |
|
158 |
|
159 \begin{frame} |
|
160 \frametitle{Need of Testing!} |
|
161 |
|
162 \begin{itemize} |
|
163 \item Quality |
|
164 |
|
165 \item Regression |
|
166 |
|
167 \item Documentation |
|
168 \end{itemize} |
|
169 \end{frame} |
|
170 |
|
171 \begin{frame}[fragile] |
|
172 \frametitle{Nosetest} |
|
173 \begin{lstlisting} |
|
174 def gcd(a, b): |
|
175 """Returns gcd of a and b, |
|
176 handles only positive numbers.""" |
|
177 if a%b == 0: return b |
|
178 return gcd(b, a%b) |
|
179 def lcm(a, b): |
|
180 return a*b/gcd(a, b) |
|
181 |
|
182 if __name__ == '__main__': |
|
183 import nose |
|
184 nose.main() |
|
185 \end{lstlisting} |
|
186 \inctime{10} |
|
187 \end{frame} |
|
188 |
|
189 \begin{frame}[fragile] |
|
190 \frametitle{Example} |
|
191 \begin{block}{Problem Statement:} |
|
192 Write a function to check whether a given input |
|
193 string is a palindrome. |
|
194 \end{block} |
|
195 \end{frame} |
|
196 |
|
197 \begin{frame}[fragile] |
|
198 \frametitle{Function: palindrome.py} |
|
199 \begin{lstlisting} |
|
200 def is_palindrome(input_str): |
|
201 return input_str == input_str[::-1] |
|
202 \end{lstlisting} |
|
203 \end{frame} |
|
204 |
|
205 \begin{frame}[fragile] |
|
206 \frametitle{Test for the palindrome: palindrome.py} |
|
207 \begin{lstlisting} |
|
208 from plaindrome import is_palindrome |
|
209 def test_function_normal_words(): |
|
210 input = "noon" |
|
211 assert is_palindrome(input) == True |
|
212 \end{lstlisting} |
|
213 \end{frame} |
|
214 |
|
215 \begin{frame}[fragile] |
|
216 \frametitle{Running the tests.} |
|
217 \begin{lstlisting} |
|
218 $ nosetests test.py |
|
219 . |
|
220 ---------------------------------------------- |
|
221 Ran 1 test in 0.001s |
|
222 |
|
223 OK |
|
224 \end{lstlisting} |
|
225 \end{frame} |
|
226 |
|
227 \begin{frame}[fragile] |
|
228 \frametitle{Exercise: Including new tests.} |
|
229 \begin{lstlisting} |
|
230 def test_function_ignore_cases_words(): |
|
231 input = "Noon" |
|
232 assert is_palindrome(input) == True |
|
233 \end{lstlisting} |
|
234 \vspace*{0.25in} |
|
235 Check\\ |
|
236 \PythonCode{$ nosetests test.py} \\ |
|
237 \begin{block}{Task} |
|
238 Tweak the code to pass this test. |
|
239 \end{block} |
|
240 \end{frame} |
|
241 |
|
242 %\begin{frame}[fragile] |
|
243 % \frametitle{Lets write some test!} |
|
244 %\begin{lstlisting} |
|
245 %#for form of equation y=mx+c |
|
246 %#given m and c for two equation, |
|
247 %#finding the intersection point. |
|
248 %def intersect(m1,c1,m2,c2): |
|
249 % x = (c2-c1)/(m1-m2) |
|
250 % y = m1*x+c1 |
|
251 % return (x,y) |
|
252 %\end{lstlisting} |
|
253 % |
|
254 %Create a simple test for this |
|
255 % |
|
256 %function which will make it fail. |
|
257 % |
|
258 %\inctime{15} |
|
259 %\end{frame} |
|
260 % |
|
261 |
|
262 \begin{frame}[fragile] |
|
263 \frametitle{Exercise} |
|
264 Based on Euclid's theorem: |
|
265 $gcd(a,b)=gcd(b,b\%a)$\\ |
|
266 gcd function can be written as: |
|
267 \begin{lstlisting} |
|
268 def gcd(a, b): |
|
269 if a%b == 0: return b |
|
270 return gcd(b, a%b) |
|
271 \end{lstlisting} |
|
272 \begin{block}{Task} |
|
273 For given gcd implementation write |
|
274 at least two tests. |
|
275 \end{block} |
|
276 \begin{block}{Task} |
|
277 Write a non recursive implementation |
|
278 of gcd(), and test it using already |
|
279 written tests. |
|
280 \end{block} |
|
281 |
|
282 \inctime{15} |
|
283 \end{frame} |
|
284 |
|
285 \begin{frame}{In this session we have covered:} |
|
286 \begin{itemize} |
|
287 \item Need for visualization. |
|
288 \item Tools available. |
|
289 \item How to follow Test Driven Approach. |
|
290 \end{itemize} |
|
291 \end{frame} |
|
292 \begin{frame} |
|
293 \begin{center} |
|
294 \Huge |
|
295 Thank you! |
|
296 \end{center} |
|
297 \end{frame} |
|
298 |
|
299 \end{document} |