diff options
Diffstat (limited to 'using_python_modules')
-rw-r--r-- | using_python_modules/four_plot.png | bin | 0 -> 54760 bytes | |||
-rw-r--r-- | using_python_modules/four_plot.py | 11 | ||||
-rw-r--r-- | using_python_modules/quickref.tex | 16 | ||||
-rw-r--r-- | using_python_modules/script.rst | 247 | ||||
-rw-r--r-- | using_python_modules/sine.py | 11 | ||||
-rw-r--r-- | using_python_modules/slides.org | 125 | ||||
-rw-r--r-- | using_python_modules/slides.tex | 227 |
7 files changed, 637 insertions, 0 deletions
diff --git a/using_python_modules/four_plot.png b/using_python_modules/four_plot.png Binary files differnew file mode 100644 index 0000000..00a3a7a --- /dev/null +++ b/using_python_modules/four_plot.png diff --git a/using_python_modules/four_plot.py b/using_python_modules/four_plot.py new file mode 100644 index 0000000..b158717 --- /dev/null +++ b/using_python_modules/four_plot.py @@ -0,0 +1,11 @@ +x=linspace(-5*pi, 5*pi, 500) +plot(x, x, 'b') +plot(x, -x, 'b') +plot(x, sin(x), 'g', linewidth=2) +plot(x, x*sin(x), 'r', linewidth=3) +legend(['x', '-x', 'sin(x)', 'xsin(x)']) +annotate('origin', xy = (0, 0)) +title('Four Plot') +xlim(-5*pi, 5*pi) +ylim(-5*pi, 5*pi) +#show() diff --git a/using_python_modules/quickref.tex b/using_python_modules/quickref.tex new file mode 100644 index 0000000..cfa992a --- /dev/null +++ b/using_python_modules/quickref.tex @@ -0,0 +1,16 @@ +Import all definitions to current name-space:\\ +{\ex \lstinline| from scipy import *|} + +Import few definitions to current name-space:\\ +{\ex \lstinline| from scipy import linspace, pi, sin|} + +Import a module as a new name-space:\\ +{\ex \lstinline| import scipy|} + +Few standard python modules:\\ +{\ex \lstinline| Math: math, random|} +{\ex \lstinline| Internet: urllib2, smtplib|} +{\ex \lstinline| System: sys|} +{\ex \lstinline| OS: os|} +{\ex \lstinline| Regular Expression: re|} +{\ex \lstinline| Compression: gzip, zipfile, tarfile|} diff --git a/using_python_modules/script.rst b/using_python_modules/script.rst new file mode 100644 index 0000000..0b0858b --- /dev/null +++ b/using_python_modules/script.rst @@ -0,0 +1,247 @@ +.. Objectives +.. ---------- + +.. At the end of this tutorial, you will be able to + +.. 1. Execute python scripts from command line. +.. #. Use import in scripts. +.. #. Import scipy and pylab modules +.. #. Use python standard modules and 3rd party modules. + + +.. Prerequisites +.. ------------- + +.. 1. should have ``pylab`` installed. +.. #. using plot command interactively. +.. #. embellishing a plot. +.. #. saving plots. + +.. Author : Anoop Jacob Thomas <anoop@fossee.in> + Internal Reviewer : Puneeth + External Reviewer : + Language Reviewer : Bhanukiran + Checklist OK? : <11-11-2010, Anand, OK> [2010-10-05] + + +==================== +Using Python modules +==================== +{{{ show the welcome slide }}} + +Welcome to the spoken tutorial on Using Python Modules. + +{{{ switch to next slide, outline slide }}} + +In this tutorial, we will see how to run python scripts from command +line. We'll see how to import modules, importing scipy and pylab +modules and have a look at the Python standard library. + +{{{ switch to next slide on executing python scripts from command line }}} + +Let us create a simple python script to print hello world. Open your +text editor and type the following, + +{{{ open the text editor and type the following }}} +:: + + print "Hello world!" + print + +and save the script as ``hello.py``, + +{{{ save the script as hello.py }}} + +Till now we saw how to run a script using the IPython interpreter +using the +:: + + %run -i hello.py + +option, but that is not the correct way of running a python +script. + +The correct method is to run it using the Python interpreter. Open the +terminal and navigate to the directory where hello.py is, + +{{{ open terminal and navigate to directory where hello.py was saved }}} + +{{{ switch to next slide }}} + +now run the Python script as, +:: + + python hello.py + +It executed the script and we got the output ``Hello World!``. + +{{{ highlight ``python filename`` syntax on slide while narrating }}} + +The syntax is python space filename. + +{{{ switch to next slide, four plot problem }}} + +Now recall the four plot problem where we plotted four plots in a single +figure. Let us run that script from command line. + +If you don't have the script, + +{{{ open the four_plot.py file in text editor }}} + +just pause here and create a python script with the following lines +and save it as four_plot.py. + +Now let us run four_plot.py as a python script. +:: + + python four_plot.py + +Oops! even though it was supposed to work, it didn't. It gave an error +``linspace()`` is not defined, which means that the function +``linspace()`` is not available in the current name-space. + +But if you try to run the same script using ``%run -i four_plot.py`` +in your IPython interpreter started with the option ``-pylab`` it will +work, because the ``-pylab`` option does some work for us by importing +the required modules to our name-space when ipython interpreter +starts. And thus we don't have to explicitly import modules. + +So now let us try to fix the problem and run the script in command +line, + +{{{ switch to next slide, fix ``linspace`` problem }}} + +add the following line as the first line in the script, +{{{ add the line as first line in four_plot.py and save }}} +:: + + from scipy import * + +Now let us run the script again, +:: + + python four_plot.py + +Now it gave another error -- plot not defined, let us edit the file +again and add the line below the line we just added, + +{{{ switch to next slide, fix ``plot`` problem }}} + +{{{ add the line as second line in four_plot.py and save }}} +:: + + from pylab import * + +And run the script, +:: + + python four_plot.py + +Yes! it worked. So what did we do? + +We actually imported the required modules using the keyword ``import``. +It could have also be done as, + +{{{ switch to next slide, better way of fixing }}} + +{{{ highlight the following in slide and say it loud }}} +:: + + from scipy import linspace + +instead of, +:: + + from scipy import * + +So in practice it is always good to use function names instead of +asterisk or star. If we use asterisk to import from a particular +module then it will replace any existing functions with the same name +in our name-space. + +{{{ switch to next slide, Instead of ``*`` }}} + +So let us modify four_plot.py as, +{{{ delete the first two lines and add the following }}} +:: + + from scipy import linspace, pi, sin + from pylab import plot, legend, annotate + from pylab import xlim, ylim, title, show + +Now let us try running the code again as, +:: + + python four_plot.py + +It works! In this method we actually imported the functions to the +current name-space, and there is another method of doing it. And that +is, + +{{{ switch to next slide }}} + +Notice that we use ``scipy.pi`` instead of just ``pi`` as in the +previous method, and the functions are called as ``pylab.plot()`` and +``pylab.annotate()`` and not as ``plot()`` and ``annotate()``. + +{{{ switch to next slide, problem statement }}} + +%% %% Write a script to plot a sine wave from minus two pi to two pi. + +Pause here and try to solve the problem yourself before looking at the +solution. + +It can solved as, + +{{{ open sine.py and show it }}} + +the first line we import the required functions ``linspace()`` and +``sin()`` and constant ``pi`` from the module scipy. the second and +third line we import the functions ``plot()``, ``legend()``, +``show()``, ``title()``, ``xlabel()`` and ``ylabel()``. And the rest +the code to generate the plot. + +We can run it as, +{{{ now switch focus to terminal and run the script }}} +:: + + python sine.py + +{{{ switch to next slide, What is a module? }}} + +Until now we have been learning about importing modules, now what is a +module? + +A module is simply a file containing Python definitions and +statements. Definitions from a module can be imported into other +modules or into the main module. + +{{{ switch to next slide, Python standard library }}} + +Python has a very rich standard library of modules. It is very +extensive, offering a wide range of facilities. Some of the standard +modules are, + +for Math: math, random +for Internet access: urllib2, smtplib +for System, Command line arguments: sys +for Operating system interface: os +for regular expressions: re +for compression: gzip, zipfile, tarfile +And there are lot more. + +Find more information at Python Library reference, +``http://docs.python.org/library/`` + +There are a lot of other modules like pylab, scipy, Mayavi, etc which +are not part of the standard python library. + +{{{ switch to next slide, summary }}} + +This brings us to the end of this tutorial, in this tutorial we +learned running scripts from command line, learned about modules, saw +the python standard library. + +{{{ switch to next slide, thank you slide }}} + +Thank you! diff --git a/using_python_modules/sine.py b/using_python_modules/sine.py new file mode 100644 index 0000000..109308e --- /dev/null +++ b/using_python_modules/sine.py @@ -0,0 +1,11 @@ +from scipy import linspace, pi, sin +from pylab import plot, legend, show, title +from pylab import xlabel, ylabel + +x = linspace(-2*pi,2*pi,100) +plot(x,sin(x)) +legend(['sin(x)']) +title('Sine plot') +xlabel('x') +ylabel('sin(x)') +show() diff --git a/using_python_modules/slides.org b/using_python_modules/slides.org new file mode 100644 index 0000000..46f6dc9 --- /dev/null +++ b/using_python_modules/slides.org @@ -0,0 +1,125 @@ +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] +#+BEAMER_FRAME_LEVEL: 1 + +#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) +#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC + +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] + +#+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl} +#+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} + +#+LaTeX_HEADER: \usepackage{listings} + +#+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries, +#+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, +#+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} + +#+TITLE: Using python modules +#+AUTHOR: FOSSEE +#+EMAIL: +#+DATE: + +#+DESCRIPTION: +#+KEYWORDS: +#+LANGUAGE: en +#+OPTIONS: H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t +#+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc + +* Outline + - Running python scripts from command line + - Importing python modules + - Importing scipy \& pylab modules + - About python standard library. +* Running Python script from command line + - Create a script, open text editor and type the following + : print "hello world!" + : print + - Save the script as ~hello.py~ +* Running Python script from command line (cont'd) + - Run the script + : $ python hello.py + /Syntax :/ *python filename* +* Four plot problem + #+begin_latex + \begin{center} + \includegraphics[scale=0.4]{four_plot} + \end{center} + #+end_latex +* Fix ~linspace()~ problem + : from scipy import * +* Fix ~plot()~ problem + : from pylab import * +* Better way of fixing + : from scipy import linspace + instead of + : from scipy import * + ~*~ means import all functions from name-space ~scipy~. +* Instead of ~*~ + : from scipy import linspace, pi, sin + : from pylab import plot, legend, annotate + : from pylab import xlim, ylim, title, show + Is better than, ~from scipy import *~ \& ~from pylab import *~. +* Another Fix + #+begin_src python + import scipy + import pylab + x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500) + pylab.plot(x, x, 'b') + pylab.plot(x, -x, 'b') + pylab.plot(x, scipy.sin(x), 'g', linewidth=2) + pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3) + pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)']) + pylab.annotate('origin', xy = (0, 0)) + pylab.xlim(-5*scipy.pi, 5*scipy.pi) + pylab.ylim(-5*scipy.pi, 5*scipy.pi) + #+end_src +* Exercise 1 + Write a python script to plot a sine wave from + #+begin_latex + $-2\Pi$ + #+end_latex + to + #+begin_latex + $2\Pi$ + #+end_latex + . +* What is a module? + Module is simply a file containing Python definitions and + statements. Definitions from a module can be imported into other + modules or into the main module. +* Python standard library + Python has a very rich standard library of modules. + - Few libraries + - Math: ~math~, ~random~ + - Internet access: ~urllib2~, ~smtplib~ + - System, Command line arguments: ~sys~ + - Operating system interface: ~os~ + - regular expressions: ~re~ + - compression: ~gzip~, ~zipfile~, ~tarfile~ + - More information + - [[http://docs.python.org/library]] +* Summary + - Running scripts from command line + - Learned about modules + - importing modules + - Python standard library +* 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 --git a/using_python_modules/slides.tex b/using_python_modules/slides.tex new file mode 100644 index 0000000..08c954b --- /dev/null +++ b/using_python_modules/slides.tex @@ -0,0 +1,227 @@ +% Created 2010-10-12 Tue 17:12 +\documentclass[presentation]{beamer} +\usepackage[latin1]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{fixltx2e} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{float} +\usepackage{wrapfig} +\usepackage{soul} +\usepackage{t1enc} +\usepackage{textcomp} +\usepackage{marvosym} +\usepackage{wasysym} +\usepackage{latexsym} +\usepackage{amssymb} +\usepackage{hyperref} +\tolerance=1000 +\usepackage[english]{babel} \usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} +\usepackage{listings} +\lstset{language=Python, basicstyle=\ttfamily\bfseries, +commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, +showstringspaces=false, keywordstyle=\color{blue}\bfseries} +\providecommand{\alert}[1]{\textbf{#1}} + +\title{Using python modules} +\author{FOSSEE} +\date{} + +\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +\begin{document} + +\maketitle + + + + + + + + + +\begin{frame} +\frametitle{Outline} +\label{sec-1} + +\begin{itemize} +\item Running python scripts from command line +\item Importing python modules +\item Importing scipy \& pylab modules +\item About python standard library. +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Running Python script from command line} +\label{sec-2} + +\begin{itemize} +\item Create a script, open text editor and type the following +\begin{verbatim} + print "hello world!" + print +\end{verbatim} + +\item Save the script as \texttt{hello.py} +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Running Python script from command line (cont'd)} +\label{sec-3} + +\begin{itemize} +\item Run the script +\begin{verbatim} + $ python hello.py +\end{verbatim} + +\end{itemize} + + \emph{Syntax :} \textbf{python filename} +\end{frame} +\begin{frame} +\frametitle{Four plot problem} +\label{sec-4} + + \begin{center} + \includegraphics[scale=0.4]{four_plot} + \end{center} +\end{frame} +\begin{frame}[fragile] +\frametitle{Fix \texttt{linspace()} problem} +\label{sec-5} + +\begin{verbatim} + from scipy import * +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Fix \texttt{plot()} problem} +\label{sec-6} + +\begin{verbatim} + from pylab import * +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Better way of fixing} +\label{sec-7} + +\begin{verbatim} + from scipy import linspace +\end{verbatim} + + instead of +\begin{verbatim} + from scipy import * +\end{verbatim} + + \texttt{*} means import all functions from name-space \texttt{scipy}. +\end{frame} +\begin{frame}[fragile] +\frametitle{Instead of \texttt{*}} +\label{sec-8} + +\begin{verbatim} + from scipy import linspace, pi, sin + from pylab import plot, legend, annotate + from pylab import xlim, ylim, title, show +\end{verbatim} + + Is better than, \texttt{from scipy import *} \& \texttt{from pylab import *}. +\end{frame} +\begin{frame}[fragile] +\frametitle{Another Fix} +\label{sec-9} + +\begin{verbatim} +import scipy +import pylab +x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500) +pylab.plot(x, x, 'b') +pylab.plot(x, -x, 'b') +pylab.plot(x, scipy.sin(x), 'g', linewidth=2) +pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3) +pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)']) +pylab.annotate('origin', xy = (0, 0)) +pylab.xlim(-5*scipy.pi, 5*scipy.pi) +pylab.ylim(-5*scipy.pi, 5*scipy.pi) +\end{verbatim} +\end{frame} +\begin{frame} +\frametitle{Exercise 1} +\label{sec-10} + + Write a python script to plot a sine wave from + $-2\Pi$ + to + $2\Pi$ + . +\end{frame} +\begin{frame} +\frametitle{What is a module?} +\label{sec-11} + + Module is simply a file containing Python definitions and + statements. Definitions from a module can be imported into other + modules or into the main module. +\end{frame} +\begin{frame} +\frametitle{Python standard library} +\label{sec-12} + + Python has a very rich standard library of modules. +\begin{itemize} +\item Few libraries + +\begin{itemize} +\item Math: \texttt{math}, \texttt{random} +\item Internet access: \texttt{urllib2}, \texttt{smtplib} +\item System, Command line arguments: \texttt{sys} +\item Operating system interface: \texttt{os} +\item regular expressions: \texttt{re} +\item compression: \texttt{gzip}, \texttt{zipfile}, \texttt{tarfile} +\end{itemize} + +\item More information + +\begin{itemize} +\item \href{http://docs.python.org/library}{http://docs.python.org/library} +\end{itemize} + +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Summary} +\label{sec-13} + +\begin{itemize} +\item Running scripts from command line +\item Learned about modules + +\begin{itemize} +\item importing modules +\end{itemize} + +\item Python standard library +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Thank you!} +\label{sec-14} + + \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} |