using python modules/script.rst
changeset 261 c7f0069d698a
child 309 9d8fd5ea64b2
equal deleted inserted replaced
260:25b4e962b55e 261:c7f0069d698a
       
     1 .. 9.3 LO: using python modules (3)
       
     2 .. ---------------------------------
       
     3 .. * executing python scripts from command line
       
     4 .. * import
       
     5 .. * scipy
       
     6 .. * pylab
       
     7 .. * sys
       
     8 .. * STDLIB modules show off
       
     9 
       
    10 ====================
       
    11 Using Python modules
       
    12 ====================
       
    13 {{{ show the welcome slide }}}
       
    14 
       
    15 Welcome to the spoken tutorial on using python modules.
       
    16 
       
    17 {{{ switch to next slide, outline slide }}}
       
    18 
       
    19 In this tutorial, we will see how to run python scripts from command
       
    20 line, importing modules, importing scipy and pylab modules.
       
    21 
       
    22 {{{ switch to next slide on executing python scripts from command line }}}
       
    23 
       
    24 Let us create a simple python script to print hello world. Open your
       
    25 text editor and type the following,
       
    26 
       
    27 {{{ open the text editor and type the following }}}
       
    28 ::
       
    29 
       
    30     print "Hello world!"
       
    31     print
       
    32 
       
    33 and save the script as hello.py,
       
    34 
       
    35 {{{ save the script as hello.py }}}
       
    36 
       
    37 Till now we saw how to run a script using the IPython interpreter
       
    38 using the
       
    39 ::
       
    40 
       
    41     %run -i hello.py
       
    42 
       
    43 option, but that is not the correct way of running a python
       
    44 script. 
       
    45 
       
    46 The correct method is to run it using the Python interpreter. Open the
       
    47 terminal and navigate to the directory where hello.py is,
       
    48 
       
    49 {{{ open terminal and navigate to directory where hello.py was saved }}}
       
    50 
       
    51 now run the Python script as,
       
    52 ::
       
    53 
       
    54     python hello.py
       
    55 
       
    56 It executed the script and we got the output ``Hello World!``.
       
    57 
       
    58 {{{ highlight ``python filename`` syntax on slide while narrating }}}
       
    59 
       
    60 The syntax is python space filename.
       
    61 
       
    62 Now recall the four plot problem where we plotted four plots in a single
       
    63 figure. Let us run that script from command line.
       
    64 
       
    65 If you don't have the script, 
       
    66 
       
    67 {{{ open the four_plot.py file in text editor }}}
       
    68 
       
    69 just pause here and create a python script with the following lines
       
    70 and save it as four_plot.py.
       
    71 
       
    72 Now let us run four_plot.py as a python script.
       
    73 ::
       
    74 
       
    75     python four_plot.py
       
    76 
       
    77 Oops! even though it was supposed to work, it didn't. It gave an error
       
    78 ``linspace()`` is not defined, which means that the function
       
    79 ``linspace()`` is not available in the current name-space.
       
    80 
       
    81 But if you try to run the same script using ``%run -i four_plot.py``
       
    82 in your IPython interpreter started with the option ``-pylab`` it will
       
    83 work, because the ``-pylab`` option does some work for us by importing
       
    84 the required modules to our name-space when ipython interpreter
       
    85 starts. And thus we don't have to explicitly import modules.
       
    86 
       
    87 So now let us try to fix the problem and run the script in command
       
    88 line,
       
    89 
       
    90 add the following line as the first line in the script,
       
    91 {{{ add the line as first line in four_plot.py and save }}}
       
    92 ::
       
    93 
       
    94     from scipy import *
       
    95 
       
    96 Now let us run the script again,
       
    97 ::
       
    98 
       
    99     python four_plot.py
       
   100 
       
   101 Now it gave another error plot not defined, let us edit the file again
       
   102 and add the line below the line we just added,
       
   103 {{{ add the line as second line in four_plot.py and save }}}
       
   104 ::
       
   105 
       
   106     from pylab import *
       
   107 
       
   108 And run the script,
       
   109 ::
       
   110 
       
   111     python four_plot.py
       
   112 
       
   113 Yes! it worked. So what did we do?
       
   114 
       
   115 We actually imported the required modules using the keyword ``import``.
       
   116 It could have also be done as,
       
   117 
       
   118 {{{ highlight the following in slide and say it loud }}}
       
   119 ::
       
   120 
       
   121     from scipy import linspace
       
   122 
       
   123 instead of,
       
   124 ::
       
   125 
       
   126     from scipy import *
       
   127 
       
   128 So in practice it is always good to use function names instead of
       
   129 asterisk or star. As if we use asterisk to import from a particular
       
   130 module then it will replace any existing functions with the same name
       
   131 in our name-space.
       
   132 
       
   133 So let us modify four_plot.py as,
       
   134 {{{ delete the first two lines and add the following }}}
       
   135 ::
       
   136 
       
   137     from scipy import linspace, pi, sin
       
   138     from pylab import plot, legend, annotate, title, show
       
   139     from pylab import xlim, ylim
       
   140 
       
   141 {{{ switch to next slide }}}
       
   142 it could also be done as,
       
   143 
       
   144 ..     import scipy
       
   145 ..     import pylab
       
   146 ..     x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500)
       
   147 ..     pylab.plot(x, x, 'b')
       
   148 ..     pylab.plot(x, -x, 'b')
       
   149 ..     pylab.plot(x, scipy.sin(x), 'g', linewidth=2)
       
   150 ..     pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3)
       
   151 ..     pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)'])
       
   152 ..     pylab.annotate('origin', xy = (0, 0))
       
   153 ..     pylab.xlim(-5*scipy.pi, 5*scipy.pi)
       
   154 ..     pylab.ylim(-5*scipy.pi, 5*scipy.pi)
       
   155 
       
   156 
       
   157 Notice that we use ``scipy.pi`` instead of just ``pi`` as in the
       
   158 previous method, and the functions are called as ``pylab.plot()`` and
       
   159 ``pylab.annotate()`` and not as ``plot()`` and ``annotate()``.
       
   160 
       
   161 {{{ switch to next slide, problem statement }}}
       
   162 
       
   163 Write a script to plot a sine wave from minus two pi to two pi.
       
   164 
       
   165 Pause here and try to solve the problem yourself before looking at the
       
   166 solution.
       
   167 
       
   168 It can solved as,
       
   169 
       
   170 {{{ open sine.py and show it }}}
       
   171 
       
   172 the first line we import the required functions ``linspace()`` and
       
   173 ``sin()`` and constant ``pi`` from the module scipy. the second and
       
   174 third line we import the functions ``plot()``, ``legend()``,
       
   175 ``show()``, ``title()``, ``xlabel()`` and ``ylabel()``. And the rest
       
   176 the code to generate the plot.
       
   177 
       
   178 We can run it as,
       
   179 {{{ now switch focus to terminal and run the script }}}
       
   180 ::
       
   181 
       
   182     python sine.py
       
   183 
       
   184 {{{ switch to next slide, What is a module? }}}
       
   185 
       
   186 So till now we have been learning about importing modules, now what is
       
   187 a module?
       
   188 
       
   189 A module is simply a file containing Python definitions and
       
   190 statements. Definitions from a module can be imported into other
       
   191 modules or into the main module.
       
   192 
       
   193 {{{ switch to next slide, Python standard library }}}
       
   194 
       
   195 Python has a very rich standard library of modules
       
   196 
       
   197 Python's standard library is very extensive, offering a wide range of
       
   198 facilities. Some of the standard modules are,
       
   199 
       
   200 for Math: math, random
       
   201 for Internet access: urllib2, smtplib
       
   202 for System, Command line arguments: sys
       
   203 for Operating system interface: os
       
   204 for regular expressions: re
       
   205 for compression: gzip, zipfile, tarfile
       
   206 And there are lot more.
       
   207 
       
   208 Find more information at Python Library reference,
       
   209 ``http://docs.python.org/library/``
       
   210 
       
   211 The modules pylab, scipy, Mayavi are not part of the standard python
       
   212 library.
       
   213 
       
   214 {{{ switch to next slide, recap }}}
       
   215 
       
   216 This brings us to the end of this tutorial, in this tutorial we
       
   217 learned running scripts from command line, learned about modules, saw
       
   218 the python standard library.
       
   219 
       
   220 {{{ switch to next slide, thank you slide }}}
       
   221 
       
   222 Thank you!
       
   223 
       
   224 ..  Author: Anoop Jacob Thomas <anoop@fossee.in>
       
   225     Reviewer 1:
       
   226     Reviewer 2:
       
   227     External reviewer: