summaryrefslogtreecommitdiff
path: root/day2
diff options
context:
space:
mode:
Diffstat (limited to 'day2')
-rw-r--r--day2/session3.tex45
-rw-r--r--day2/session4.tex280
-rw-r--r--day2/session5.tex7
3 files changed, 271 insertions, 61 deletions
diff --git a/day2/session3.tex b/day2/session3.tex
index 95b5081..bee8030 100644
--- a/day2/session3.tex
+++ b/day2/session3.tex
@@ -95,12 +95,12 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title page
-\title[]{3D data Visualization}
+\title[3D Plotting]{3D data Visualization}
\author[FOSSEE] {FOSSEE}
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
-\date[] {1, November 2009\\Day 2, Session 3}
+\date[] {1 November, 2009\\Day 2, Session 5}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -279,10 +279,9 @@ In []: mlab.test_contour3d??
\end{columns}
\begin{lstlisting}
-In []: from numpy import *
In []: t = linspace(0, 2*pi, 50)
In []: u = cos(t) * pi
-in []: x, y, z = sin(u), cos(u), sin(t)
+In []: x, y, z = sin(u), cos(u), sin(t)
\end{lstlisting}
\emphbar{\PythonCode{In []: mlab.points3d(x, y, z)}}
\end{frame}
@@ -319,6 +318,40 @@ In []: z = sin(x*x + y*y)
\end{frame}
\begin{frame}[fragile]
+ \frametitle{mgrid}
+ \begin{lstlisting}
+In []: mgrid[0:3,0:3]
+Out[]:
+array([[[0, 0, 0],
+ [1, 1, 1],
+ [2, 2, 2]],
+
+ [[0, 1, 2],
+ [0, 1, 2],
+ [0, 1, 2]]])
+
+In []: mgrid[-1:1:5j]
+Out[]: array([-1., -0.5, 0., 0.5, 1.])
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Example}
+ \begin{lstlisting}
+In []: x, y = mgrid[-1:1:5j, -1:1:5j]
+In []: z = x*x + y*y
+
+In []: z
+Out[]:
+array([[ 2. , 1.25, 1. , 1.25, 2. ],
+ [ 1.25, 0.5 , 0.25, 0.5 , 1.25],
+ [ 1. , 0.25, 0. , 0.25, 1. ],
+ [ 1.25, 0.5 , 0.25, 0.5 , 1.25],
+ [ 2. , 1.25, 1. , 1.25, 2. ]])
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
\myemph{\Large 2D data: \texttt{mlab.mesh}}
\vspace*{0.25in}
@@ -328,7 +361,7 @@ In []: z = sin(x*x + y*y)
\vspace*{0.25in}
\begin{lstlisting}
-In []: phi, theta = numpy.mgrid[0:pi:20j,
+In []: phi, theta = mgrid[0:pi:20j,
... 0:2*pi:20j]
In []: x = sin(phi)*cos(theta)
In []: y = sin(phi)*sin(theta)
@@ -349,7 +382,7 @@ In []: mlab.mesh(x, y, z,
\pgfimage[width=1.5in]{MEDIA/m2/mlab/contour3d}\\
\end{columns}
\begin{lstlisting}
-In []: x, y, z = ogrid[-5:5:64j,
+In []: x, y, z = mgrid[-5:5:64j,
... -5:5:64j,
... -5:5:64j]
In []: mlab.contour3d(x*x*0.5 + y*y +
diff --git a/day2/session4.tex b/day2/session4.tex
index 0ed5ccc..577841f 100644
--- a/day2/session4.tex
+++ b/day2/session4.tex
@@ -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 @@ SyntaxError: invalid syntax
\begin{frame}[fragile]
\frametitle{Exceptions}
\begin{lstlisting}
->>> print spam
+In []: print spam
\end{lstlisting}
\pause
\begin{lstlisting}
@@ -175,7 +307,7 @@ NameError: name 'spam' is not defined
\begin{frame}[fragile]
\frametitle{Exceptions}
\begin{lstlisting}
->>> 1 / 0
+In []: 1 / 0
\end{lstlisting}
\pause
\begin{lstlisting}
@@ -186,6 +318,44 @@ or modulo by zero
\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 @@ or modulo by zero
\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 @@ mymodule.py in test()
----> 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 @@ ipdb>
\frametitle{Debugging: Exercise}
\small
\begin{lstlisting}
-import keyword
-f = open('/path/to/file')
-
-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
-
-print freq
+science = {}
+
+for record in open('sslc1.txt'):
+ fields = record.split(';')
+ region_code = fields[0].strip()
+
+ score_str = fields[6].strip()
+ score = int(score_str) if score_str != 'AA'
+ else 0
+
+ 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 @@ print freq
\section{Test Driven Approach}
\begin{frame}
- \frametitle{Need of Testing!}
+ \frametitle{Need for Testing!}
\begin{itemize}
\item Quality
@@ -293,17 +465,19 @@ def is_palindrome(input_str):
\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 @@ def test_function_ignore_cases_words():
\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 @@ def test_function_ignore_cases_words():
%\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}
diff --git a/day2/session5.tex b/day2/session5.tex
index 4fe9cd5..ddb1599 100644
--- a/day2/session5.tex
+++ b/day2/session5.tex
@@ -78,7 +78,7 @@
\author[FOSSEE] {FOSSEE}
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
-\date[] {1, November 2009\\Day 2}
+\date[] {1 November, 2009\\Day 2, Session 4}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
@@ -111,12 +111,13 @@
\end{frame}
\begin{frame}{Problem 1.1}
- The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. For example, aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.\\
+ The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. \\For example:
+\center{aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.}\\
Write a function that returns the aliquot number of a given number.
\end{frame}
\begin{frame}{Problem 1.2}
- A pair of numbers (a, b) is said to be \alert{amicable} if the aliquot number of a is b and the aliquot number of b is a.\\
+ Pair of numbers (a, b) is said to be \alert{amicable} if aliquot number of a is b and aliquot number of b is a.\\
Example: \texttt{220, 284}\\
Write a program that prints all four digit amicable pairs.