day2/session4.tex
changeset 297 a835affb1447
parent 289 884d42eff66d
child 330 46533051b9d3
equal deleted inserted replaced
296:2d08c45681a1 297:a835affb1447
   142 
   142 
   143 \section{Tests: Getting started}
   143 \section{Tests: Getting started}
   144 \begin{frame}[fragile] 
   144 \begin{frame}[fragile] 
   145   \frametitle{gcd revisited!}
   145   \frametitle{gcd revisited!}
   146   \begin{itemize}
   146   \begin{itemize}
   147   \item Open gcd.py
   147   \item Open \texttt{gcd.py}
   148   \end{itemize}  
   148   \end{itemize}  
   149 \begin{lstlisting}
   149 \begin{lstlisting}
   150     def gcd(a, b):
   150     def gcd(a, b):
   151         if a % b == 0: 
   151         if a % b == 0: 
   152             return b
   152             return b
   154 
   154 
   155     print gcd(15, 65)
   155     print gcd(15, 65)
   156     print gcd(16, 76)
   156     print gcd(16, 76)
   157 \end{lstlisting}
   157 \end{lstlisting}
   158   \begin{itemize}
   158   \begin{itemize}
   159   \item python gcd.py
   159   \item \texttt{python gcd.py}
   160   \end{itemize}
   160   \end{itemize}
   161 \end{frame}
   161 \end{frame}
   162 
   162 
   163 \begin{frame}[fragile] 
   163 \begin{frame}[fragile] 
   164   \frametitle{Find lcm using our gcd module}
   164   \frametitle{Find lcm using our gcd module}
   165   \begin{itemize}
   165   \begin{itemize}
   166   \item Open lcm.py  
   166   \item Open \texttt{lcm.py}  
   167   \item $lcm = \frac{a*b}{gcd(a,b)}$
   167   \item $lcm = \frac{a * b}{gcd(a,b)}$
   168   \end{itemize}  
   168   \end{itemize}  
   169 \begin{lstlisting}
   169 \begin{lstlisting}
   170     from gcd import gcd    
   170     from gcd import gcd    
   171     def lcm(a, b):
   171     def lcm(a, b):
   172         return (a * b) / gcd(a, b)
   172         return (a * b) / gcd(a, b)
   173     
   173     
   174     print lcm(14, 56)
   174     print lcm(14, 56)
   175 \end{lstlisting}
   175 \end{lstlisting}
   176   \begin{itemize}
   176   \begin{itemize}
   177   \item python lcm.py
   177   \item \texttt{python lcm.py}
   178   \end{itemize}
   178   \end{itemize}
   179   \begin{lstlisting}
   179   \begin{lstlisting}
   180 5
   180 5
   181 4
   181 4
   182 56
   182 56
   183   \end{lstlisting}    
   183   \end{lstlisting}    
   184 \end{frame}
   184 \end{frame}
   185 
   185 
   186 \begin{frame}[fragile] 
   186 \begin{frame}[fragile] 
   187   \frametitle{Writing stand-alone module}  
   187   \frametitle{Writing stand-alone module}  
   188 Edit gcd.py file to:
   188 Edit \texttt{gcd.py} file to:
   189 \begin{lstlisting}
   189 \begin{lstlisting}
   190     def gcd(a, b):
   190     def gcd(a, b):
   191         if a % b == 0: 
   191         if a % b == 0: 
   192             return b
   192             return b
   193         return gcd(b, a%b)
   193         return gcd(b, a%b)
   195     if __name__ == "__main__":        
   195     if __name__ == "__main__":        
   196         print gcd(15, 65)
   196         print gcd(15, 65)
   197         print gcd(16, 76)
   197         print gcd(16, 76)
   198 \end{lstlisting}
   198 \end{lstlisting}
   199   \begin{itemize}
   199   \begin{itemize}
   200   \item python gcd.py
   200   \item \texttt{python gcd.py}
   201   \item python lcm.py
   201   \item \texttt{python lcm.py}
   202   \end{itemize}
   202   \end{itemize}
   203 \end{frame}
   203 \end{frame}
   204 
   204 
   205 \begin{frame}[fragile]
   205 \begin{frame}[fragile]
   206   \frametitle{More use of main}
   206   \frametitle{Automating tests}
   207   For automating tests.
       
   208   \begin{lstlisting}
   207   \begin{lstlisting}
   209 if __name__ == '__main__':
   208 if __name__ == '__main__':
   210     for line in open('numbers.txt'):
   209     for line in open('numbers.txt'):
   211         numbers = line.split()
   210         numbers = line.split()
   212         x = int(numbers[0])
   211         x = int(numbers[0])
   213         y = int(numbers[1])
   212         y = int(numbers[1])
   214         result = (int(numbers[2]))
   213         result = int(numbers[2])
   215         assert gcd(x, y) == result
   214         if gcd(x, y) != result:
       
   215             print "Failed gcd test
       
   216                           for", x, y
   216   \end{lstlisting}  
   217   \end{lstlisting}  
   217 \end{frame}
   218 \end{frame}
   218 
   219 
   219 \section{Coding Style}
   220 \section{Coding Style}
   220 \begin{frame}{Readability and Consistency}
   221 \begin{frame}{Readability and Consistency}
   240 
   241 
   241 \begin{frame}[fragile]
   242 \begin{frame}[fragile]
   242   \frametitle{Code Layout}
   243   \frametitle{Code Layout}
   243   \begin{itemize}
   244   \begin{itemize}
   244         \item Indentation
   245         \item Indentation
   245         \item Tabs or Spaces??
   246         \item Tabs or Spaces?
   246         \item Maximum Line Length
   247         \item Maximum Line Length
   247         \item Blank Lines
   248         \item Blank Lines
   248         \item Encodings
   249         \item Encodings
   249    \end{itemize}
   250    \end{itemize}
   250 \end{frame}
   251 \end{frame}
   251 
   252 
   252 \begin{frame}{Whitespaces in Expressions}
   253 \begin{frame}{Whitespaces in Expressions}
   253   \begin{itemize}
   254   \begin{itemize}
   254         \item When to use extraneous whitespaces??
   255         \item When to use extraneous whitespaces?
   255         \item When to avoid extra whitespaces??
   256         \item When to avoid extra whitespaces?
   256         \item Use one statement per line
   257         \item Use one statement per line
   257    \end{itemize}
   258    \end{itemize}
   258 \end{frame}
   259 \end{frame}
   259 
   260 
   260 \begin{frame}{Comments}
   261 \begin{frame}{Comments}
   317 or modulo by zero
   318 or modulo by zero
   318 \end{lstlisting}
   319 \end{lstlisting}
   319 \end{frame}
   320 \end{frame}
   320 
   321 
   321 \begin{frame}[fragile]
   322 \begin{frame}[fragile]
       
   323   \frametitle{Processing user input}
       
   324   \begin{lstlisting}
       
   325 prompt = 'Enter a number(Q to quit): '
       
   326 
       
   327 a = raw_input(prompt)
       
   328 
       
   329 num = int(a) if a != 'Q' else 0
       
   330   \end{lstlisting}
       
   331   \emphbar{What if the user enters some other alphabet?}
       
   332 \end{frame}
       
   333 
       
   334 
       
   335 \begin{frame}[fragile]
   322   \frametitle{Handling Exceptions}
   336   \frametitle{Handling Exceptions}
   323   Python uses \typ{try} and \typ{except} clause.
   337   Python provides \typ{try} and \typ{except} clause.
   324   %%Revisiting the raw\_input
       
   325   \begin{lstlisting}
   338   \begin{lstlisting}
   326 a = raw_input('Enter number(Q to quit):')
   339 prompt = 'Enter a number(Q to quit): '
       
   340 
       
   341 a = raw_input(prompt)
   327 try:
   342 try:
   328     num = int(a)
   343     num = int(a)
   329     print num
   344     print num
   330 except:
   345 except:
   331     if a == 'Q':
   346     if a == 'Q':
   332         print 'Exiting...'
   347         print "Exiting ..."
   333     else:
   348     else:
   334         print 'Wrong input!'      
   349         print "Wrong input ..."
   335   \end{lstlisting}
   350   \end{lstlisting}  
   336   
   351 \end{frame}
   337   
   352 
   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}
   353 \subsection{Strategy}
   360 \begin{frame}[fragile]
   354 \begin{frame}[fragile]
   361     \frametitle{Debugging effectively}
   355     \frametitle{Debugging effectively}
   362     \begin{itemize}
   356     \begin{itemize}
   363         \item \typ{print} based strategy
   357         \item \typ{print} based strategy
   419 savefig('science.png')
   413 savefig('science.png')
   420 \end{lstlisting}
   414 \end{lstlisting}
   421 \inctime{10}
   415 \inctime{10}
   422 \end{frame}
   416 \end{frame}
   423 
   417 
       
   418 \begin{frame}
       
   419   \frametitle{Summary}
       
   420 We have covered:
       
   421   \begin{itemize}
       
   422   \item Following and Resolving Error Messages.
       
   423   \item Exceptions.
       
   424   \item Handling exceptions
       
   425   \item Approach for Debugging.
       
   426 %  \item Writting and running tests.
       
   427   \end{itemize}
       
   428 \end{frame}
       
   429 
       
   430 \end{document}
       
   431 
   424 %% \begin{frame}
   432 %% \begin{frame}
   425 %%     \frametitle{Testing}
   433 %%     \frametitle{Testing}
   426    
   434    
   427 %%     \begin{itemize}
   435 %%     \begin{itemize}
   428 %%         \item Writing tests is really simple!
   436 %%         \item Writing tests is really simple!
   431 
   439 
   432 %%         \item Example!
   440 %%         \item Example!
   433 %%     \end{itemize}
   441 %%     \end{itemize}
   434 %% \end{frame}
   442 %% \end{frame}
   435 
   443 
   436 \section{Test Driven Approach}
   444 % \section{Test Driven Approach}
   437 \begin{frame}
   445 % \begin{frame}
   438     \frametitle{Need for Testing!}
   446 %     \frametitle{Need for Testing!}
   439    
   447 %    
   440     \begin{itemize}
   448 %     \begin{itemize}
   441         \item Quality
   449 %         \item Quality
   442         \item Regression
   450 %         \item Regression
   443         \item Documentation
   451 %         \item Documentation
   444     \end{itemize}
   452 %     \end{itemize}
   445     %% \vspace*{0.25in}
   453 %     %% \vspace*{0.25in}
   446     %% \emphbar{It is to assure that section of code is working as it is supposed to work}
   454 %     %% \emphbar{It is to assure that section of code is working as it is supposed to work}
   447 \end{frame}
   455 % \end{frame}
   448 
   456 % 
   449 \begin{frame}[fragile]
   457 % \begin{frame}[fragile]
   450     \frametitle{Example}
   458 %     \frametitle{Example}
   451     \begin{block}{Problem Statement}
   459 %     \begin{block}{Problem Statement}
   452       Write a function to check whether a given input
   460 %       Write a function to check whether a given input
   453       string is a palindrome.
   461 %       string is a palindrome.
   454     \end{block}
   462 %     \end{block}
   455 \end{frame}
   463 % \end{frame}
   456 
   464 % 
   457 \begin{frame}[fragile]
   465 % \begin{frame}[fragile]
   458     \frametitle{Function: palindrome.py}
   466 %     \frametitle{Function: palindrome.py}
   459 \begin{lstlisting}    
   467 % \begin{lstlisting}    
   460 def is_palindrome(input_str):
   468 % def is_palindrome(input_str):
   461   return input_str == input_str[::-1]
   469 %   return input_str == input_str[::-1]
   462 \end{lstlisting}    
   470 % \end{lstlisting}    
   463 \end{frame}
   471 % \end{frame}
   464 
   472 % 
   465 \begin{frame}[fragile]
   473 % \begin{frame}[fragile]
   466     \frametitle{Test for the palindrome: palindrome.py}
   474 %     \frametitle{Test for the palindrome: palindrome.py}
   467 \begin{lstlisting}    
   475 % \begin{lstlisting}    
   468 def test_function_normal_words():
   476 % def test_function_normal_words():
   469   input = "noon"
   477 %   input = "noon"
   470   assert is_palindrome(input) == True
   478 %   assert is_palindrome(input) == True
   471 
   479 % 
   472 if __name__ == "main'':
   480 % if __name__ == "main'':
   473   test_function_normal_words()
   481 %   test_function_normal_words()
   474 \end{lstlisting}    
   482 % \end{lstlisting}    
   475 \end{frame}
   483 % \end{frame}
   476 
   484 % 
   477 \begin{frame}[fragile]
   485 % \begin{frame}[fragile]
   478     \frametitle{Running the tests.}
   486 %     \frametitle{Running the tests.}
   479 \begin{lstlisting}    
   487 % \begin{lstlisting}    
   480 $ nosetests palindrome.py 
   488 % $ nosetests palindrome.py 
   481 .
   489 % .
   482 ----------------------------------------------
   490 % ----------------------------------------------
   483 Ran 1 test in 0.001s
   491 % Ran 1 test in 0.001s
   484 
   492 % 
   485 OK
   493 % OK
   486 \end{lstlisting}    
   494 % \end{lstlisting}    
   487 \end{frame}
   495 % \end{frame}
   488 
   496 % 
   489 \begin{frame}[fragile]
   497 % \begin{frame}[fragile]
   490     \frametitle{Exercise: Including new tests.}
   498 %     \frametitle{Exercise: Including new tests.}
   491 \begin{lstlisting}    
   499 % \begin{lstlisting}    
   492 def test_function_ignore_cases_words():
   500 % def test_function_ignore_cases_words():
   493   input = "Noon"
   501 %   input = "Noon"
   494   assert is_palindrome(input) == True
   502 %   assert is_palindrome(input) == True
   495 \end{lstlisting}
   503 % \end{lstlisting}
   496      \vspace*{0.25in}
   504 %      \vspace*{0.25in}
   497      Check\\
   505 %      Check\\
   498      \PythonCode{$ nosetests palindrome.py} \\
   506 %      \PythonCode{$ nosetests palindrome.py} \\
   499      \begin{block}{Task}
   507 %      \begin{block}{Task}
   500      Tweak the code to pass this test.
   508 %      Tweak the code to pass this test.
   501      \end{block}
   509 %      \end{block}
   502 \end{frame}
   510 % \end{frame}
   503 
   511 % 
   504 %\begin{frame}[fragile]
   512 % %\begin{frame}[fragile]
   505 %    \frametitle{Lets write some test!}
   513 % %    \frametitle{Lets write some test!}
   506 %\begin{lstlisting}    
   514 % %\begin{lstlisting}    
   507 %#for form of equation y=mx+c
   515 % %#for form of equation y=mx+c
   508 %#given m and c for two equation,
   516 % %#given m and c for two equation,
   509 %#finding the intersection point.
   517 % %#finding the intersection point.
   510 %def intersect(m1,c1,m2,c2):
   518 % %def intersect(m1,c1,m2,c2):
   511 %    x = (c2-c1)/(m1-m2)
   519 % %    x = (c2-c1)/(m1-m2)
   512 %    y = m1*x+c1
   520 % %    y = m1*x+c1
   513 %    return (x,y)
   521 % %    return (x,y)
   514 %\end{lstlisting}
   522 % %\end{lstlisting}
   515 %
   523 % %
   516 %Create a simple test for this
   524 % %Create a simple test for this
   517 %
   525 % %
   518 %function which will make it fail.
   526 % %function which will make it fail.
   519 %
   527 % %
   520 %\inctime{15} 
   528 % %\inctime{15} 
   521 %\end{frame}
   529 % %\end{frame}
   522 %
   530 % %
   523 
   531 % 
   524 %% \begin{frame}[fragile]
   532 % %% \begin{frame}[fragile]
   525 %%     \frametitle{Exercise}
   533 % %%     \frametitle{Exercise}
   526 %%     Based on Euclid's algorithm:
   534 % %%     Based on Euclid's algorithm:
   527 %%     \begin{center}
   535 % %%     \begin{center}
   528 %%     $gcd(a,b)=gcd(b,b\%a)$
   536 % %%     $gcd(a,b)=gcd(b,b\%a)$
   529 %%     \end{center}
   537 % %%     \end{center}
   530 %%     gcd function can be written as:
   538 % %%     gcd function can be written as:
   531 %%     \begin{lstlisting}
   539 % %%     \begin{lstlisting}
   532 %%     def gcd(a, b):
   540 % %%     def gcd(a, b):
   533 %%       if a%b == 0: return b
   541 % %%       if a%b == 0: return b
   534 %%       return gcd(b, a%b)
   542 % %%       return gcd(b, a%b)
   535 %%     \end{lstlisting}
   543 % %%     \end{lstlisting}
   536 %%     \vspace*{-0.15in}
   544 % %%     \vspace*{-0.15in}
   537 %%     \begin{block}{Task}
   545 % %%     \begin{block}{Task}
   538 %%       \begin{itemize}
   546 % %%       \begin{itemize}
   539 %%       \item Write at least 
   547 % %%       \item Write at least 
   540 %%         two tests for above mentioned function.
   548 % %%         two tests for above mentioned function.
   541 %%       \item Write a non recursive implementation
   549 % %%       \item Write a non recursive implementation
   542 %%       of gcd(), and test it using already 
   550 % %%       of gcd(), and test it using already 
   543 %%       written tests.
   551 % %%       written tests.
   544 %%       \end{itemize}
   552 % %%       \end{itemize}
   545 %%     \end{block}
   553 % %%     \end{block}
   546     
   554 %     
   547 %% \inctime{15} 
   555 % %% \inctime{15} 
   548 %% \end{frame}
   556 % %% \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}
       
   561 
       
   562 \end{document}