day2/session4.tex
changeset 263 8a4a1e5aec85
parent 256 a06196a05043
child 288 c4e25269a86c
--- a/day2/session4.tex	Sat Oct 31 01:33:41 2009 +0530
+++ b/day2/session4.tex	Wed Nov 04 09:40:28 2009 +0530
@@ -95,14 +95,12 @@
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Title page
-\title[]{Debugging and \\Test Driven Approach}
+\title[Python Development]{Python Development}
 
 \author[FOSSEE] {FOSSEE}
 
 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
-\date[] {11, October 2009}
-\date[] % (optional)
-
+\date[] {1 November, 2009\\Day 2, Session 3}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 %\pgfdeclareimage[height=0.75cm]{iitblogo}{iitblogo}
@@ -142,13 +140,147 @@
   \maketitle
 \end{frame}
 
+\section{Tests: Getting started}
+\begin{frame}[fragile] 
+  \frametitle{gcd revisited!}
+  \begin{itemize}
+  \item Open gcd.py
+  \end{itemize}  
+\begin{lstlisting}
+    def gcd(a, b):
+        if a % b == 0: 
+            return b
+        return gcd(b, a%b)
+
+    print gcd(15, 65)
+    print gcd(16, 76)
+\end{lstlisting}
+  \begin{itemize}
+  \item python gcd.py
+  \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile] 
+  \frametitle{Find lcm using our gcd module}
+  \begin{itemize}
+  \item Open lcm.py  
+  \item $lcm = \frac{a*b}{gcd(a,b)}$
+  \end{itemize}  
+\begin{lstlisting}
+    from gcd import gcd    
+    def lcm(a, b):
+        return (a * b) / gcd(a, b)
+    
+    print lcm(14, 56)
+\end{lstlisting}
+  \begin{itemize}
+  \item python lcm.py
+  \end{itemize}
+  \begin{lstlisting}
+5
+4
+56
+  \end{lstlisting}    
+\end{frame}
+
+\begin{frame}[fragile] 
+  \frametitle{Writing stand-alone module}  
+Edit gcd.py file to:
+\begin{lstlisting}
+    def gcd(a, b):
+        if a % b == 0: 
+            return b
+        return gcd(b, a%b)
+
+    if __name__ == "__main__":        
+        print gcd(15, 65)
+        print gcd(16, 76)
+\end{lstlisting}
+  \begin{itemize}
+  \item python gcd.py
+  \item python lcm.py
+  \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+  \frametitle{More use of main}
+  For automating tests.
+  \begin{lstlisting}
+if __name__ == '__main__':
+    for line in open('numbers.txt'):
+        numbers = line.split()
+        x = int(numbers[0])
+        y = int(numbers[1])
+        result = (int(numbers[2]))
+        assert gcd(x, y) == result
+  \end{lstlisting}  
+\end{frame}
+
+\section{Coding Style}
+\begin{frame}{Readability and Consistency}
+    \begin{itemize}
+        \item Readability Counts!\\Code is read more often than its written.
+        \item Consistency!
+        \item Know when to be inconsistent.
+      \end{itemize}
+\end{frame}
+
+\begin{frame}[fragile] \frametitle{A question of good style}
+  \begin{lstlisting}
+    amount = 12.68
+    denom = 0.05
+    nCoins = round(amount/denom)
+    rAmount = nCoins * denom
+  \end{lstlisting}
+  \pause
+  \begin{block}{Style Rule \#1}
+    Naming is 80\% of programming
+  \end{block}
+\end{frame}
+
+\begin{frame}[fragile]
+  \frametitle{Code Layout}
+  \begin{itemize}
+        \item Indentation
+        \item Tabs or Spaces??
+        \item Maximum Line Length
+        \item Blank Lines
+        \item Encodings
+   \end{itemize}
+\end{frame}
+
+\begin{frame}{Whitespaces in Expressions}
+  \begin{itemize}
+        \item When to use extraneous whitespaces??
+        \item When to avoid extra whitespaces??
+        \item Use one statement per line
+   \end{itemize}
+\end{frame}
+
+\begin{frame}{Comments}
+  \begin{itemize}
+        \item No comments better than contradicting comments
+        \item Block comments
+        \item Inline comments
+   \end{itemize}
+\end{frame}
+
+\begin{frame}{Docstrings}
+  \begin{itemize}
+        \item When to write docstrings?
+        \item Ending the docstrings
+        \item One liner docstrings
+   \end{itemize}
+More information at PEP8: http://www.python.org/dev/peps/pep-0008/
+\inctime{5}
+\end{frame}
 
 \section{Debugging}
 \subsection{Errors and Exceptions}
 \begin{frame}[fragile]
  \frametitle{Errors}
  \begin{lstlisting}
->>> while True print 'Hello world'
+In []: while True print 'Hello world'
  \end{lstlisting}
 \pause
   \begin{lstlisting}
@@ -162,7 +294,7 @@
 \begin{frame}[fragile]
  \frametitle{Exceptions}
  \begin{lstlisting}
->>> print spam
+In []: print spam
 \end{lstlisting}
 \pause
 \begin{lstlisting}
@@ -175,7 +307,7 @@
 \begin{frame}[fragile]
  \frametitle{Exceptions}
  \begin{lstlisting}
->>> 1 / 0
+In []: 1 / 0
 \end{lstlisting}
 \pause
 \begin{lstlisting}
@@ -186,6 +318,44 @@
 \end{lstlisting}
 \end{frame}
 
+\begin{frame}[fragile]
+  \frametitle{Handling Exceptions}
+  Python uses \typ{try} and \typ{except} clause.
+  %%Revisiting the raw\_input
+  \begin{lstlisting}
+a = raw_input('Enter number(Q to quit):')
+try:
+    num = int(a)
+    print num
+except:
+    if a == 'Q':
+        print 'Exiting...'
+    else:
+        print 'Wrong input!'      
+  \end{lstlisting}
+  
+  
+\end{frame}
+
+%% \begin{frame}[fragile]
+%%   \frametitle{Solving it with \typ{try} and \typ{except}}
+%% \vspace{-0.2in}
+%%   \begin{lstlisting}
+%% highest = 0
+%% for record in open('sslc1.txt'):
+%%     fields = record.split(';')
+%%     try:
+%%         total = 0
+%%         for score_str in fields[3:8]:
+%%             score = int(score_str)
+%%             total += score
+%%         if total > highest:
+%%             highest = total
+%%     except:        
+%%         pass
+%% print highest
+%%   \end{lstlisting}
+%% \end{frame}
 \subsection{Strategy}
 \begin{frame}[fragile]
     \frametitle{Debugging effectively}
@@ -209,8 +379,8 @@
 \frametitle{Debugging in IPython}
 \small
 \begin{lstlisting}
-In [1]: import mymodule
-In [2]: mymodule.test()
+In []: import mymodule
+In []: mymodule.test()
 ---------------------------------------------
 NameError   Traceback (most recent call last)
 <ipython console> in <module>()
@@ -219,7 +389,7 @@
 ----> 2     print spam
 NameError: global name 'spam' is not defined
 
-In [3]: %debug
+In []: %debug
 > mymodule.py(2)test()
       0     print spam
 ipdb> 
@@ -232,19 +402,21 @@
 \frametitle{Debugging: Exercise}
 \small
 \begin{lstlisting}
-import keyword
-f = open('/path/to/file')
+science = {}
+
+for record in open('sslc1.txt'):
+    fields = record.split(';')
+    region_code = fields[0].strip()
 
-freq = {}
-for line in f:
-    words = line.split()
-    for word in words:
-        key = word.strip(',.!;?()[]: ')
-        if keyword.iskeyword(key):
-            value = freq[key]
-            freq[key] = value + 1
+    score_str = fields[6].strip()
+    score = int(score_str) if score_str != 'AA' 
+                           else 0
 
-print freq
+    if score > 90:
+        science[region_code] += 1
+
+pie(science.values(), labels=science.keys())
+savefig('science.png')
 \end{lstlisting}
 \inctime{10}
 \end{frame}
@@ -263,7 +435,7 @@
 
 \section{Test Driven Approach}
 \begin{frame}
-    \frametitle{Need of Testing!}
+    \frametitle{Need for Testing!}
    
     \begin{itemize}
         \item Quality
@@ -293,17 +465,19 @@
 \begin{frame}[fragile]
     \frametitle{Test for the palindrome: palindrome.py}
 \begin{lstlisting}    
-from plaindrome import is_palindrome
 def test_function_normal_words():
   input = "noon"
   assert is_palindrome(input) == True
+
+if __name__ == "main'':
+  test_function_normal_words()
 \end{lstlisting}    
 \end{frame}
 
 \begin{frame}[fragile]
     \frametitle{Running the tests.}
 \begin{lstlisting}    
-$ nosetests test.py 
+$ nosetests palindrome.py 
 .
 ----------------------------------------------
 Ran 1 test in 0.001s
@@ -321,7 +495,7 @@
 \end{lstlisting}
      \vspace*{0.25in}
      Check\\
-     \PythonCode{$ nosetests test.py} \\
+     \PythonCode{$ nosetests palindrome.py} \\
      \begin{block}{Task}
      Tweak the code to pass this test.
      \end{block}
@@ -347,37 +521,39 @@
 %\end{frame}
 %
 
-\begin{frame}[fragile]
-    \frametitle{Exercise}
-    Based on Euclid's algorithm:
-    \begin{center}
-    $gcd(a,b)=gcd(b,b\%a)$
-    \end{center}
-    gcd function can be written as:
-    \begin{lstlisting}
-    def gcd(a, b):
-      if a%b == 0: return b
-      return gcd(b, a%b)
-    \end{lstlisting}
-    \vspace*{-0.15in}
-    \begin{block}{Task}
-      \begin{itemize}
-      \item Write at least 
-        two tests for above mentioned function.
-      \item Write a non recursive implementation
-      of gcd(), and test it using already 
-      written tests.
-      \end{itemize}
-    \end{block}
+%% \begin{frame}[fragile]
+%%     \frametitle{Exercise}
+%%     Based on Euclid's algorithm:
+%%     \begin{center}
+%%     $gcd(a,b)=gcd(b,b\%a)$
+%%     \end{center}
+%%     gcd function can be written as:
+%%     \begin{lstlisting}
+%%     def gcd(a, b):
+%%       if a%b == 0: return b
+%%       return gcd(b, a%b)
+%%     \end{lstlisting}
+%%     \vspace*{-0.15in}
+%%     \begin{block}{Task}
+%%       \begin{itemize}
+%%       \item Write at least 
+%%         two tests for above mentioned function.
+%%       \item Write a non recursive implementation
+%%       of gcd(), and test it using already 
+%%       written tests.
+%%       \end{itemize}
+%%     \end{block}
     
-\inctime{15} 
-\end{frame}
+%% \inctime{15} 
+%% \end{frame}
 
 \begin{frame}
-  \frametitle{We have learned}
+  \frametitle{Summary}
+We have coverd:
   \begin{itemize}
   \item Following and Resolving Error Messages.
   \item Exceptions.
+  \item Handling exceptions
   \item Approach for Debugging.
   \item Writting and running tests.
   \end{itemize}