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} |
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 |
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} |
|