diff options
author | Jovina | 2011-07-14 16:34:42 +0530 |
---|---|---|
committer | Jovina | 2011-07-14 16:34:42 +0530 |
commit | 177a9aa8bf9a84327142d0dbd5d73194b85eb4c8 (patch) | |
tree | 893918cddabaf6992128a4683e9612a54e049d25 /writing_python_scripts | |
parent | e495e0664285744e17cdfb53000c1e58f069383d (diff) | |
download | st-scripts-177a9aa8bf9a84327142d0dbd5d73194b85eb4c8.tar.gz st-scripts-177a9aa8bf9a84327142d0dbd5d73194b85eb4c8.tar.bz2 st-scripts-177a9aa8bf9a84327142d0dbd5d73194b85eb4c8.zip |
Major changes to script & slides of 'writing python scripts'.
Diffstat (limited to 'writing_python_scripts')
-rw-r--r-- | writing_python_scripts/script.rst | 241 | ||||
-rw-r--r-- | writing_python_scripts/slides.org | 133 | ||||
-rw-r--r-- | writing_python_scripts/slides.tex | 165 |
3 files changed, 471 insertions, 68 deletions
diff --git a/writing_python_scripts/script.rst b/writing_python_scripts/script.rst index 0729efd..e537859 100644 --- a/writing_python_scripts/script.rst +++ b/writing_python_scripts/script.rst @@ -20,22 +20,51 @@ Script ------ -{{{ Show the slide containing title }}} +.. L1 -Hello friends and welcome to the tutorial on "Writing Python scripts" +{{{ Show the first slide containing title, name of the production +team along with the logo of MHRD }}} -{{{ Show the slide containing the outline slide }}} +.. R1 -In this tutorial, we shall learn +Hello friends and welcome to the tutorial on "Writing Python scripts". - * How to write Python scripts +.. L2 -Often we will have to reuse the code that we haave written. We do that by -writing functions. Functions are bundled into packages and are imported as and -when required in other scripts. +{{{ Show the slide containing the objectives }}} -Let us first write a function that computes the gcd of two numbers and save it -in a script. +..R2 + +At the end of this tutorial, you will be able to, + + 1. Understand what is importing. + #. Write your own Python modules. + #. Understand the ``__name__=="__main__"`` idiom + +.. L3 + +{{{ Switch to the pre-requisite slide }}} + +.. R3 + +Before beginning this tutorial,we would suggest you to complete the +tutorial on "Using Python modules". + +.. R4 + +Often we will have to reuse the code that we have written. We do that by +writing functions. Functions are bundled into packages and are imported +as and when required in other scripts. + +.. L4 + +.. R5 + +Let us first write a function that computes the gcd of two numbers and +save it in a script.Open an editor and type the code.Please take care +of the indentation. + +.. L5 {{{ Open an editor and start typing out the following code }}} :: @@ -47,11 +76,14 @@ in a script. return a -We shall write a test function in the script that tests the gcd function every -time the script is run. +.. R6 + +We shall write a test function in the script that tests the gcd function +every time the script is run. -{{{ Add to the script }}} +.. L6 +{{{ Add the following lines to the script }}} :: if gcd(40, 12) == 4: @@ -59,26 +91,57 @@ time the script is run. else: print "The GCD function is wrong" +.. L7 + +{{{ Show slide containing GCD code }}} +{{{ Keep open for sometime and then continue }}} + +.. R7 + +.. R8 + Let us save the file as script.py in ``/home/fossee/gcd_script.py`` +.. L8 + +{{{ Save the script as ``gcd_script.py`` }}} + +.. R9 + We shall run the script by typing + +.. L9 + +{{{ Open a terminal }}} :: - $ python /home/fossee/gcd_script.py + python /home/fossee/gcd_script.py + +.. R10 We can see that the script is executed and everything is fine. -What if we want to use the gcd function in some of our other scripts. This -is also possible since every python file can be used as a module. +What if we want to use the gcd function in some of our other scripts. +This is also possible since every python file can be used as a module. But first, we shall understand what happens when you import a module. +.. L10 + +.. R11 + Open IPython and type + +.. L11 + +{{{ Open another terminal and type ipython }}} :: import sys sys.path +.. R12 + This is a list of locations where python searches for a module when it encounters an import statement. @@ -94,40 +157,83 @@ working directory is also searched. Alternatively, we can also import the module if we are working in same directory where the script exists. +.. L12 + +.. L13 + +{{{ Close the current terminal }}} + +.. R13 + +.. R14 + Since we are in /home/fossee, we can simply do + +.. L14 + +{{{ Switch to the first terminal }}} :: - import gcd_script + import gcd_script.py + +.. R15 -We can see that the gcd_script is imported. But the test code that we added at -the end of the file is also executed. +We can see that the gcd_script is imported. But the test code that we +added at the end of the file is also executed. -But we want the test code to be executed only when the file is run as a python -script and not when it is imported. +But we want the test code to be executed only when the file is run as +a python script and not when it is imported. This is possible by using ``__name__`` variable. -First, we shall look at how to use the idiom and then understand how it works. +First, we shall look at how to use the idiom and then understand how +it works. + +.. L15 + +.. L16 + +{{{ Switch to slide, __name__ variable }}} -Go to the file and add +.. R16 + +Go to the file and add this line as the beginning of the code +and indent the code accordingly. + +.. L17 + +{{{ Switch to gcd_script.py and add this line after the + line ``return a`` }}} :: if __name__ == "__main__": - -before the test code and indent the test code. + +.. R17 +.. R18 + Let us first run the code. + +.. L18 + +{{{ Switch back to the terminal }}} :: - $ python gcd_script.py + python gcd_script.py + +.. R19 We can see that the test runs successfully. Now we shall import the file + +.. L19 :: import gcd_script +.. R20 + We see that now the test code is not executed. The ``__name__`` variable is local to every module and it is equal to @@ -137,19 +243,86 @@ Hence, all the code that goes in to the if block, ``if __name__ == "__main__":`` is executed only when the file is run as a python script. +.. L20 + +.. L21 + {{{ Show summary slide }}} -This brings us to the end of the tutorial. -we have learnt +.. R21 + +This brings us to the end of the tutorial.In this tutorial, we have +learnt to, + + 1. Know what happens when we import a module. + #. Use a script as a module. + #. Write test functions using the ``__name__`` idiom. + +.. L22 + +{{{Show self assessment questions slide}}} + +.. R22 + +Here are some self assessment questions for you to solve + +1. Which of the following variables contains the locations to search for + python modules + + - sys.pythonpath + - sys.path + - os.pythonpath + - os.path + + +2. A module should contain only functions. + - True + - False + + +3. The script ``utils.py`` is in one of locations of PYTHONPATH and + contains the following code +:: + + def show(x): + print x + + show("Hello World") + + if __name__ == "__main__": + + show("Hello Test") + + How do you use the ``show`` function after doing ``import utils`` + + - utils.show("hey") + - show("hey") + - utils.py.show("hey") + - utils.py.show.py("hey") + +.. L23 + +{{{solution of self assessment questions on slide}}} + +.. R23 + +And the answers, + +1. ``sys.path`` contains the locations to search for python modules. + +2. False. A module can contain a wide range of functions. + +3. After doing ``import utils``, we can use the function ``show()`` as, +:: + + utils.show("hey") - * What happens when we import a module - * How to use a script as a module - * How to write test functions using the __name__ idiom +.. L24 -{{{ Show the "sponsored by FOSSEE" slide }}} +{{{ Show the Thank you slide }}} -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India +.. R24 -Hope you have enjoyed and found it useful. +Hope you have enjoyed this tutorial and found it useful. Thank you! diff --git a/writing_python_scripts/slides.org b/writing_python_scripts/slides.org new file mode 100644 index 0000000..cf423fb --- /dev/null +++ b/writing_python_scripts/slides.org @@ -0,0 +1,133 @@ +#+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: +#+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 + +* +#+begin_latex +\begin{center} +\vspace{12pt} +\textcolor{blue}{\huge Writing python scripts} +\end{center} +\vspace{18pt} +\begin{center} +\vspace{10pt} +\includegraphics[scale=0.95]{../images/fossee-logo.png}\\ +\vspace{5pt} +\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\ +\scriptsize Funded by National Mission on Education through ICT\\ +\scriptsize MHRD,Govt. of India\\ +\includegraphics[scale=0.30]{../images/iitb-logo.png}\\ +\end{center} +#+end_latex + +* Objectives +At the end of this tutorial, you will be able to, + + - Understand what is importing. + - Write your own Python modules. + - Understand the \_\_name\_\_==``\_\_main\_\_" idiom +* Pre-requisite +Spoken tutorial on - +- Using Python modules +* GCD +#+begin_src Python +def gcd(a, b): + + while b: + a, b = b, a%b + + return a + if gcd(40, 12) == 4: + print "Everything OK" + else: + print "The GCD function is wrong" +#+end_src +* \_\_name\_\_ variable +#+begin_src Python +if __name__ == "__main__": +#+end_src +* Summary +In this tutorial, we have learnt to, + + - Know what happens when we import a module. + - Use a script as a module. + - Write test functions using the ``\_\_name\_\_'' idiom. +* Evaluation +1. Which of the following variables contains the locations to search for + python modules + + - sys.pythonpath + - sys.path + - os.pythonpath + - os.path + +2. A module should contain only functions. + - True + - False + +* Evaluation contd.. +- The script ``utils.py`` is in one of locations of PYTHONPATH and contains + the following code +#+begin_src Python +def show(x): + print x + +show("Hello World") + +if __name__ == "__main__": + + show("Hello Test") +#+end_src + How do you use the ``show`` function after doing ``import utils`` + + - utils.show("hey") + - show("hey") + - utils.py.show("hey") + - utils.py.show.py("hey") +* Solutions +1. sys.path + +2. False + +3. utils.show("hey") +* +#+begin_latex + \begin{block}{} + \begin{center} + \textcolor{blue}{\Large THANK YOU!} + \end{center} + \end{block} +\begin{block}{} + \begin{center} + For more Information, visit our website\\ + \url{http://fossee.in/} + \end{center} + \end{block} +#+end_latex diff --git a/writing_python_scripts/slides.tex b/writing_python_scripts/slides.tex index b1b2eda..a68798e 100644 --- a/writing_python_scripts/slides.tex +++ b/writing_python_scripts/slides.tex @@ -1,4 +1,4 @@ -% Created 2010-10-10 Sun 23:53 +% Created 2011-07-14 Thu 16:14 \documentclass[presentation]{beamer} \usepackage[latin1]{inputenc} \usepackage[T1]{fontenc} @@ -23,14 +23,13 @@ commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, showstringspaces=false, keywordstyle=\color{blue}\bfseries} \providecommand{\alert}[1]{\textbf{#1}} -\title{Sets} +\title{} \author{FOSSEE} \date{} \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} \begin{document} -\maketitle @@ -40,65 +39,163 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries} + +\begin{frame} + +\begin{center} +\vspace{12pt} +\textcolor{blue}{\huge Writing python scripts} +\end{center} +\vspace{18pt} +\begin{center} +\vspace{10pt} +\includegraphics[scale=0.95]{../images/fossee-logo.png}\\ +\vspace{5pt} +\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\ +\scriptsize Funded by National Mission on Education through ICT\\ +\scriptsize MHRD,Govt. of India\\ +\includegraphics[scale=0.30]{../images/iitb-logo.png}\\ +\end{center} +\end{frame} \begin{frame} -\frametitle{Outline} -\label{sec-1} +\frametitle{Objectives} +\label{sec-2} + +At the end of this tutorial, you will be able to, + \begin{itemize} -\item Defining Sets -\item Operations on sets +\item Understand what is importing. +\item Write your own Python modules. +\item Understand the \_\_name\_\_==``\_\_main\_\_'' idiom \end{itemize} \end{frame} \begin{frame} -\frametitle{Question 1} -\label{sec-2} +\frametitle{Pre-requisite} +\label{sec-3} - Given a list of marks, \texttt{marks = [20, 23, 22, 23, 20, 21, 23]} list - all the duplicates +Spoken tutorial on - + +\begin{itemize} +\item Using Python modules +\end{itemize} \end{frame} \begin{frame}[fragile] -\frametitle{Solution 1} -\label{sec-3} +\frametitle{GCD} +\label{sec-4} \lstset{language=Python} \begin{lstlisting} -marks = [20, 23, 22, 23, 20, 21, 23] -marks_set = set(marks) -for mark in marks_set: - marks.remove(mark) +def gcd(a, b): -# we are now left with only duplicates in the list marks -duplicates = set(marks) + while b: + a, b = b, a%b + + return a + if gcd(40, 12) == 4: + print "Everything OK" + else: + print "The GCD function is wrong" +\end{lstlisting} +\end{frame} +\begin{frame}[fragile] +\frametitle{\_\_name\_\_ variable} +\label{sec-5} + +\lstset{language=Python} +\begin{lstlisting} +if __name__ == "__main__": \end{lstlisting} \end{frame} \begin{frame} \frametitle{Summary} -\label{sec-4} +\label{sec-6} + +In this tutorial, we have learnt to, + - You should now be able to -- \begin{itemize} -\item make sets from lists -\item input sets directly -\item perform operations like union, intersection, symmetric difference -\item check if a subset of another -\item check containership, length and other properties similar to lists +\item Know what happens when we import a module. +\item Use a script as a module. +\item Write test functions using the ``\_\_name\_\_'' idiom. \end{itemize} \end{frame} \begin{frame} -\frametitle{Thank you!} -\label{sec-5} +\frametitle{Evaluation} +\label{sec-7} + + +\begin{enumerate} +\item Which of the following variables contains the locations to search for + python modules +\begin{itemize} +\item sys.pythonpath +\item sys.path +\item os.pythonpath +\item os.path +\end{itemize} +\vspace{8pt} +\item A module should contain only functions. +\begin{itemize} +\item True +\item False +\end{itemize} +\end{enumerate} +\end{frame} +\begin{frame}[fragile] +\frametitle{Evaluation contd..} +\label{sec-8} + + +\begin{itemize} +\item The script ``utils.py'' is in one of locations of PYTHONPATH and contains + the following code +\lstset{language=Python} +\begin{lstlisting} +def show(x): + print x + +show("Hello World") + +if __name__ == "__main__": + + show("Hello Test") +\end{lstlisting} + How do you use the ``show'' function after doing ``import utils'' +\begin{itemize} +\item utils.show(``hey'') +\item show(``hey'') +\item utils.py.show(``hey'') +\item utils.py.show.py(``hey'') +\end{itemize} +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Solutions} +\label{sec-9} + + +\begin{enumerate} +\item sys.path +\vspace{12pt} +\item False +\vspace{12pt} +\item utils.show(``hey'') +\end{enumerate} +\end{frame} +\begin{frame} \begin{block}{} \begin{center} - This spoken tutorial has been produced by the - \textcolor{blue}{FOSSEE} team, which is funded by the + \textcolor{blue}{\Large THANK YOU!} \end{center} + \end{block} +\begin{block}{} \begin{center} - \textcolor{blue}{National Mission on Education through \\ - Information \& Communication Technology \\ - MHRD, Govt. of India}. + For more Information, visit our website\\ + \url{http://fossee.in/} \end{center} \end{block} \end{frame} -\end{document} +\end{document}
\ No newline at end of file |