# HG changeset patch # User Bhanukiran # Date 1289995210 -19800 # Node ID 70faad10e8540a0dfe6b099e0e6b6f9c36762d91 # Parent 33ad94cee1fbf7518705e66e501ca439e48fa1b6# Parent bc8d01c3c9b392183367eda23cf23f1c17e12348 Merged heads. diff -r 33ad94cee1fb -r 70faad10e854 plotting-data/quickref.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plotting-data/quickref.tex Wed Nov 17 17:30:10 2010 +0530 @@ -0,0 +1,15 @@ +Creating a Sequence:\\ +{\ex \lstinline| L = [0.1, 0.2, 0.3] |} + +Squaring a sequence:\\ +{\ex \lstinline| tsquare=square(t) |} + +Plotting two list using small dots:\\ +{\ex \lstinline| plot(L,tsquare,'.') |} + +Plotting two list using big dots:\\ +{\ex \lstinline| plot(L,tsquare,'o') |} + +Plotting an errorbar in blue color:\\ +{\ex \lstinline| errorbar(L,tsquare,xerr=delta_L, yerr=delta_T, fmt='b.') |} + diff -r 33ad94cee1fb -r 70faad10e854 testing-debugging/questions.rst --- a/testing-debugging/questions.rst Tue Nov 16 23:28:06 2010 +0530 +++ b/testing-debugging/questions.rst Wed Nov 17 17:30:10 2010 +0530 @@ -3,20 +3,49 @@ .. A mininum of 8 questions here (along with answers) -1. Question 1 +1. Why do we do Software Testing? + + Answer: To evaluate a program and determine that it meets required results. + + - Answer: Answer 1 + +2. What is proper indentation for python code according to style guidelines? + + Answer: Four Space Indentation - OR +3. What is the idiom used for running python scripts in a standalone manner? - Answer:: + Answer: + if __name__ == '__main__': + +4. What constitutes a test case? + + Answer: A set of inputs and expected result + +5. How do you start the debugger on ipython? + + Answer: %debug + +6. What idiom do you use for catching and exception? + + Answer: try,catch. + +7. What kind of exception is 0/0? + + Answer Zero Division Error + +8. a = 12.68 + b = 0.05 + c = round(a/b) + d = c * b + + What would you recommend to the programmer who wrote + above written piece of code. + - answer code line 1 - answer code line 2 - answer code line 3 -2. Question 2 -3. Question 3 + Larger Questions @@ -24,5 +53,8 @@ .. A minimum of 2 questions here (along with answers) -1. Question 1 -2. Question 2 +1. Write a program for checking if two numbers are coprime + Create test cases for it and automate it. +2. Write a program that divides two numbers. Take the numbers from the user. Also make sure the code tell the user that input is invalid incase the divisor is 0. + + diff -r 33ad94cee1fb -r 70faad10e854 testing-debugging/quickref.tex --- a/testing-debugging/quickref.tex Tue Nov 16 23:28:06 2010 +0530 +++ b/testing-debugging/quickref.tex Wed Nov 17 17:30:10 2010 +0530 @@ -1,8 +1,28 @@ -Creating a linear array:\\ -{\ex \lstinline| x = linspace(0, 2*pi, 50)|} +Skeleton of a test:\\ +{\ex \lstinline| if __name__ == '__main__':|} +{\ex \lstinline| result = gcd(48, 64) |} +{\ex \lstinline| if result != 16: |} +{\ex \lstinline| print ``Test Failed'' |} +{\ex \lstinline| print ``Test Passed'' |} + + +\textbf{Testing} + +Get results from function or unit of code being tested.Compare it to original output. Test passed if they match else failed. -Plotting two variables:\\ -{\ex \lstinline| plot(x, sin(x))|} +\textbf{Code Style} +Four Space Indentation +79 character limit on a line +Funtions should be seperated by +blank line +Use Docstring +White space around operators + +Skeleton of try catch:\\ +{\ex \lstinline| try: |} +{\ex \lstinline| num = int(a) |} +{\ex \lstinline| except: |} +{\ex \lstinline| print ``Wrong input...'' |} -Plotting two lists of equal length x, y:\\ -{\ex \lstinline| plot(x, y)|} +Starting debugger in ipython:\\ +{\ex \lstinline| %debug |} diff -r 33ad94cee1fb -r 70faad10e854 testing-debugging/script.rst --- a/testing-debugging/script.rst Tue Nov 16 23:28:06 2010 +0530 +++ b/testing-debugging/script.rst Wed Nov 17 17:30:10 2010 +0530 @@ -68,7 +68,7 @@ if __name__ == '__main__': result = gcd(48, 64) if result != 16: - print "Test failed for the case a=48 and b=64. Expected 16. Obtained %d instead." % result + print "Test failed" print "Test Passed" Note that we have introduced a new semantic which uses two new magic names @@ -173,15 +173,15 @@ 1.Four Space Indentation 2.Limit to 79 characters a line, but readability should come first. 3.Functions and methods should be separated with two blank lines. - Class definitions with three blank lines. 4.No inline comments, comments should be above the line they comment. 5.Use Docstring to explain units of code performing specific task like functions. 6.We should always have whitespace around operators and after punctuation. %% %% Pause and do the following exercise -%% %% Give meaningful names to the variables in the gcd code . - +%% %% Give meaningful names to the variables in following +code + c=a/b This will help enormously towards making our program more readable. @@ -217,10 +217,10 @@ Lets see why and how we can use Exception in our programs. -{{{ Slide with code snippet }}} + Type on your interpreter:: - + a = raw_input("Enter a number:") num = int(a) @@ -258,7 +258,7 @@ Test if it is correct by changing the code. And refine the hypothesis on the basis of our result. -{{{ Slide with code snippet }}} + Lets see another example of debugging. Create a file mymodule.py and add the following code:: @@ -267,13 +267,16 @@ total=1+1 print spam -{{{ Slide with code snippet }}} + Lets now try and run this code :: import mymodule mymodule.test() + +{{{ Slide with idb and total being accessed }}} + Interpreter gives us an error because spam is not defined but lets now do %debug on ipython interpreter. The prompt on the shell has changed to ipdb. This is debugger here you can access variables in that code block for example 'total'unlike the normal interpreter. diff -r 33ad94cee1fb -r 70faad10e854 testing-debugging/slides.org --- a/testing-debugging/slides.org Tue Nov 16 23:28:06 2010 +0530 +++ b/testing-debugging/slides.org Wed Nov 17 17:30:10 2010 +0530 @@ -36,23 +36,18 @@ - Need for coding style and some of the standards followed by the Python Community. - Handling Errors and Exceptions. - * gcd function - Create gcd.py file with: -#+begin_LaTeX -\begin{lstlisting}[language=python] +#+begin_src python def gcd(a, b): if a % b == 0: return b return gcd(b, a%b) -\end{lstlisting} -#+end_LaTeX +#+end_src python * Test for gcd.py - Edit gcd.py file -#+begin_LaTeX -\begin{lstlisting}[language=python] - +#+begin_src python def gcd(a, b): if b == 0: return a @@ -63,13 +58,11 @@ if result != 16: print "Test failed" print "Test Passed" -\end{lstlisting} -#+end_LaTeX + +#+end_src * Automating tests -#+begin_LaTeX -\begin{lstlisting}[language=python] - +#+begin_src python if __name=__='__main__': for line in open('numbers.txt'): numbers = line.split() @@ -79,8 +72,138 @@ if gcd(x, y) != result: print "Failed gcd test for", x, y +#+end_src + +* Question 1 + For the same inputs as gcd write automated tests for LCM. +* Solution 1 +#+begin_src python + def gcd(a, b): + if a % b == 0: + return b + return gcd(b, a%b) + + def lcm(a, b): + return (a * b) / gcd(a, b) + + if __name__ == '__main__': + for line in open('lcmtestcases.txt'): + numbers = line.split() + x = int(numbers[0]) + y = int(numbers[1]) + result = int(numbers[2]) + if lcm(x, y) != result: + print "Failed lcm test for", x, y + +#+end_src + +* Meaning full names +#+begin_src python + + amount = 12.68 + denom = 0.05 + nCoins = round(amount / denom) + rAmount = nCoins * denom + +#+end_src + +* Code style + - Four Space Indentation + - 79 character limit on a line + - Funtions should be seperated by + blank line + - Use Docstring + - White space around operators + - l = 32 % 4 + +* Question 2 + - Give meaningful names to the variables in following + code + + - c = a / b + +* Solution 2 +#+begin_src python + + quotient = dividend / divisor + +#+end_src + +* Code Snippet +#+begin_src python + + while True print 'Hello world' + +#+end_src + +* Error +#+begin_latex +\begin{lstlisting} + while True print 'Hello world' + \end{lstlisting} + \begin{lstlisting} + File "", line 1, in ? + while True print 'Hello world' +SyntaxError: invalid syntax \end{lstlisting} -#+end_LaTeX +#+end_latex + +* Code Snippet +#+begin_src python + a = raw_input("Enter a number") + try: + num = int(a) + except: + print "Wrong input ..." + +#+end_src + +* Using idb +#+begin_latex +\small +\begin{lstlisting} +In []: import mymodule +In []: mymodule.test() +--------------------------------------------- +NameError Traceback (most recent call last) + in () +mymodule.py in test() + 1 def test(): + 2 total=1+1 +----> 3 print spam +NameError: global name 'spam' is not defined + +In []: %debug +> mymodule.py(2)test() + 0 print spam +ipdb> total +2 +\end{lstlisting} + +#+end_latex +* Summary + - Create simple tests for a function. + - Learn to Automate tests using many predefined test cases. + - Good coding standards. + - Difference between syntax error and exception. + - Handling exception using try and except. + - Using %debug for debugging on ipython. + +* Thank you! +#+begin_latex + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +#+end_latex + diff -r 33ad94cee1fb -r 70faad10e854 testing-debugging/slides.tex --- a/testing-debugging/slides.tex Tue Nov 16 23:28:06 2010 +0530 +++ b/testing-debugging/slides.tex Wed Nov 17 17:30:10 2010 +0530 @@ -1,4 +1,4 @@ -% Created 2010-11-07 Sun 18:57 +% Created 2010-11-12 Fri 02:00 \documentclass[presentation]{beamer} \usepackage[latin1]{inputenc} \usepackage[T1]{fontenc} @@ -61,12 +61,12 @@ \item Create gcd.py file with: \end{itemize} -\begin{lstlisting}[language=python] - def gcd(a, b): - if a % b == 0: - return b - return gcd(b, a%b) -\end{lstlisting} +\begin{verbatim} +def gcd(a, b): + if a % b == 0: + return b + return gcd(b, a%b) +\end{verbatim} \end{frame} \begin{frame}[fragile] \frametitle{Test for gcd.py} @@ -76,45 +76,203 @@ \item Edit gcd.py file \end{itemize} -\begin{lstlisting}[language=python] +\begin{verbatim} +def gcd(a, b): + if b == 0: + return a + return gcd(b, a%b) - def gcd(a, b): - if b == 0: - return a - return gcd(b, a%b) - - if __name__=='__main__': - result = gcd(48, 64) - if result != 16: - print "Test failed" - print "Test Passed" -\end{lstlisting} +if __name__=='__main__': + result = gcd(48, 64) + if result != 16: + print "Test failed" + print "Test Passed" +\end{verbatim} \end{frame} \begin{frame}[fragile] \frametitle{Automating tests} \label{sec-4} -\begin{lstlisting}[language=python] +\begin{verbatim} +if __name=__='__main__': +for line in open('numbers.txt'): + numbers = line.split() + x = int(numbers[0]) + y = int(numbers[1]) + result = int(numbers[2]) + if gcd(x, y) != result: + print "Failed gcd test + for", x, y +\end{verbatim} +\end{frame} +\begin{frame} +\frametitle{Question 1} +\label{sec-5} + + For the same inputs as gcd write automated tests for LCM. +\end{frame} +\begin{frame}[fragile] +\frametitle{Solution 1} +\label{sec-6} + +\begin{verbatim} +def gcd(a, b): + if a % b == 0: + return b + return gcd(b, a%b) + + def lcm(a, b): + return (a * b) / gcd(a, b) + + if __name__ == '__main__': + for line in open('lcmtestcases.txt'): + numbers = line.split() + x = int(numbers[0]) + y = int(numbers[1]) + result = int(numbers[2]) + if lcm(x, y) != result: + print "Failed lcm test for", x, y +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Meaning full names} +\label{sec-7} + +\begin{verbatim} + +amount = 12.68 +denom = 0.05 +nCoins = round(amount / denom) +rAmount = nCoins * denom +\end{verbatim} +\end{frame} +\begin{frame} +\frametitle{Code style} +\label{sec-8} + +\begin{itemize} +\item Four Space Indentation +\item 79 character limit on a line +\item Funtions should be seperated by + blank line +\item Use Docstring +\item White space around operators + +\begin{itemize} +\item l = 32 \% 4 +\end{itemize} + +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Question 2} +\label{sec-9} + +\begin{itemize} +\item Give meaningful names to the variables in following + code + +\begin{itemize} +\item c = a / b +\end{itemize} + +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Solution 2} +\label{sec-10} - if __name=__='__main__': - for line in open('numbers.txt'): - numbers = line.split() - x = int(numbers[1]) - y = int(numbers[2]) - result = int(numbers[3]) - if gcd(x, y) != result: - print "Failed gcd test - for", x, y +\begin{verbatim} + +quotient = dividend / divisor +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Code Snippet} +\label{sec-11} + +\begin{verbatim} + +while True print 'Hello world' +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Error} +\label{sec-12} + +\begin{lstlisting} + while True print 'Hello world' + \end{lstlisting} + \begin{lstlisting} + File "", line 1, in ? + while True print 'Hello world' +SyntaxError: invalid syntax \end{lstlisting} - +\end{frame} +\begin{frame}[fragile] +\frametitle{Code Snippet} +\label{sec-13} +\begin{verbatim} +a = raw_input("Enter a number") +try: + num = int(a) + except: + print "Wrong input ..." +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Using idb} +\label{sec-14} +\small +\begin{lstlisting} +In []: import mymodule +In []: mymodule.test() +--------------------------------------------- +NameError Traceback (most recent call last) + in () +mymodule.py in test() + 1 def test(): + 2 total=1+1 +----> 3 print spam +NameError: global name 'spam' is not defined -$^{1}$ FOOTNOTE DEFINITION NOT FOUND: 0 +In []: %debug +> mymodule.py(2)test() + 0 print spam +ipdb> total +2 +\end{lstlisting} +\end{frame} +\begin{frame} +\frametitle{Summary} +\label{sec-15} -$^{2}$ FOOTNOTE DEFINITION NOT FOUND: 1 +\begin{itemize} +\item Create simple tests for a function. +\item Learn to Automate tests using many predefined test cases. +\item Good coding standards. +\item Difference between syntax error and exception. +\item Handling exception using try and except. +\item Using \%debug for debugging on ipython. +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Thank you!} +\label{sec-16} -$^{3}$ FOOTNOTE DEFINITION NOT FOUND: 2 + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} \end{frame} \end{document}