1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
\section{Using Python modules}
\begin{frame}[fragile]
\frametitle{\texttt{hello.py}}
\begin{itemize}
\item Script to print `hello world' -- \texttt{hello.py}
\end{itemize}
\begin{lstlisting}
print "Hello world!"
\end{lstlisting}
\begin{itemize}
\item We have been running scripts from IPython
\end{itemize}
\begin{lstlisting}
In[]: %run -i hello.py
\end{lstlisting}
\begin{itemize}
\item Now, we run from the shell using python
\end{itemize}
\begin{lstlisting}
$ python hello.py
\end{lstlisting} %$
\end{frame}
\begin{frame}[fragile]
\frametitle{Simple plot}
\begin{itemize}
\item Save the following in \texttt{sine\_plot.py}
\end{itemize}
\begin{lstlisting}
x = linspace(-2*pi, 2*pi, 100)
plot(x, sin(x))
show()
\end{lstlisting}
\begin{itemize}
\item Now, let us run the script
\end{itemize}
\begin{lstlisting}
$ python sine_plot.py
\end{lstlisting} % $
\begin{itemize}
\item What's wrong?
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Importing}
\begin{itemize}
\item \texttt{-pylab} is importing a lot of functionality
\item Add the following to the top of your file
\end{itemize}
\begin{lstlisting}
from scipy import *
\end{lstlisting}
\begin{lstlisting}
$ python sine_plot.py
\end{lstlisting} % $
\begin{itemize}
\item Now, plot is not found
\item Add the following as the second line of your script
\end{itemize}
\begin{lstlisting}
from pylab import *
\end{lstlisting}
\begin{lstlisting}
$ python sine_plot.py
\end{lstlisting} % $
\begin{itemize}
\item It works!
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Importing \ldots}
\begin{itemize}
\item \texttt{*} imports everything from \texttt{scipy} and
\texttt{pylab}
\item But, It imports lot of unnecessary stuff
\item And two modules may contain the same name, causing a conflict
\item There are two ways out
\end{itemize}
\begin{lstlisting}
from scipy import linspace, pi, sin
from pylab import plot, show
\end{lstlisting}
\begin{itemize}
\item OR change the imports to following and
\item Replace \texttt{pi} with \texttt{scipy.pi}, etc.
\end{itemize}
\begin{lstlisting}
import scipy
import pylab
\end{lstlisting}
\end{frame}
\section{Writing modules}
\begin{frame}[fragile]
\frametitle{GCD script}
\begin{itemize}
\item Function that computes gcd of two numbers
\item Save it as \texttt{gcd\_script.py}
\end{itemize}
\begin{lstlisting}
def gcd(a, b):
while b:
a, b = b, a%b
return a
\end{lstlisting}
\begin{itemize}
\item Also add the tests to the file
\end{itemize}
\begin{lstlisting}
if gcd(40, 12) == 4 and gcd(12, 13) == 1:
print "Everything OK"
else:
print "The GCD function is wrong"
\end{lstlisting}
\begin{lstlisting}
$ python gcd_script.py
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{Python path}
\begin{itemize}
\item In IPython type the following
\end{itemize}
\begin{lstlisting}
import sys
sys.path
\end{lstlisting}
\begin{itemize}
\item List of locations where python searches for a module
\item \texttt{import sys} -- searches for file \texttt{sys.py} or
dir \texttt{sys} in all these locations
\item So, our own modules can be in any one of the locations
\item Current working directory is one of the locations
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{\_\_name\_\_}}
\begin{lstlisting}
import gcd_script
\end{lstlisting}
\begin{itemize}
\item The import is successful
\item But the test code, gets run
\item Add the tests to the following \texttt{if} block
\end{itemize}
\begin{lstlisting}
if __name__ == "__main__":
\end{lstlisting}
\begin{itemize}
\item Now the script runs properly
\item As well as the import works; test code not executed
\item \texttt{\_\_name\_\_} is local to every module and is equal
to \texttt{\_\_main\_\_} only when the file is run as a script.
\end{itemize}
\end{frame}
|