using python modules/script.rst
changeset 309 9d8fd5ea64b2
parent 261 c7f0069d698a
child 319 e8c02b3c51ac
equal deleted inserted replaced
308:0a0a91fb3a0d 309:9d8fd5ea64b2
    15 Welcome to the spoken tutorial on using python modules.
    15 Welcome to the spoken tutorial on using python modules.
    16 
    16 
    17 {{{ switch to next slide, outline slide }}}
    17 {{{ switch to next slide, outline slide }}}
    18 
    18 
    19 In this tutorial, we will see how to run python scripts from command
    19 In this tutorial, we will see how to run python scripts from command
    20 line, importing modules, importing scipy and pylab modules.
    20 line, importing modules, importing scipy and pylab modules. And also
       
    21 see the Python standard library.
    21 
    22 
    22 {{{ switch to next slide on executing python scripts from command line }}}
    23 {{{ switch to next slide on executing python scripts from command line }}}
    23 
    24 
    24 Let us create a simple python script to print hello world. Open your
    25 Let us create a simple python script to print hello world. Open your
    25 text editor and type the following,
    26 text editor and type the following,
    46 The correct method is to run it using the Python interpreter. Open the
    47 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 terminal and navigate to the directory where hello.py is,
    48 
    49 
    49 {{{ open terminal and navigate to directory where hello.py was saved }}}
    50 {{{ open terminal and navigate to directory where hello.py was saved }}}
    50 
    51 
       
    52 {{{ switch to next slide }}}
       
    53 
    51 now run the Python script as,
    54 now run the Python script as,
    52 ::
    55 ::
    53 
    56 
    54     python hello.py
    57     python hello.py
    55 
    58 
    56 It executed the script and we got the output ``Hello World!``.
    59 It executed the script and we got the output ``Hello World!``.
    57 
    60 
    58 {{{ highlight ``python filename`` syntax on slide while narrating }}}
    61 {{{ highlight ``python filename`` syntax on slide while narrating }}}
    59 
    62 
    60 The syntax is python space filename.
    63 The syntax is python space filename.
       
    64 
       
    65 {{{ switch to next slide, four plot problem }}}
    61 
    66 
    62 Now recall the four plot problem where we plotted four plots in a single
    67 Now recall the four plot problem where we plotted four plots in a single
    63 figure. Let us run that script from command line.
    68 figure. Let us run that script from command line.
    64 
    69 
    65 If you don't have the script, 
    70 If you don't have the script, 
    85 starts. And thus we don't have to explicitly import modules.
    90 starts. And thus we don't have to explicitly import modules.
    86 
    91 
    87 So now let us try to fix the problem and run the script in command
    92 So now let us try to fix the problem and run the script in command
    88 line,
    93 line,
    89 
    94 
       
    95 {{{ switch to next slide, fix ``linspace`` problem }}}
       
    96 
    90 add the following line as the first line in the script,
    97 add the following line as the first line in the script,
    91 {{{ add the line as first line in four_plot.py and save }}}
    98 {{{ add the line as first line in four_plot.py and save }}}
    92 ::
    99 ::
    93 
   100 
    94     from scipy import *
   101     from scipy import *
    98 
   105 
    99     python four_plot.py
   106     python four_plot.py
   100 
   107 
   101 Now it gave another error plot not defined, let us edit the file again
   108 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,
   109 and add the line below the line we just added,
       
   110 
       
   111 {{{ switch to next slide, fix ``plot`` problem }}}
       
   112 
   103 {{{ add the line as second line in four_plot.py and save }}}
   113 {{{ add the line as second line in four_plot.py and save }}}
   104 ::
   114 ::
   105 
   115 
   106     from pylab import *
   116     from pylab import *
   107 
   117 
   112 
   122 
   113 Yes! it worked. So what did we do?
   123 Yes! it worked. So what did we do?
   114 
   124 
   115 We actually imported the required modules using the keyword ``import``.
   125 We actually imported the required modules using the keyword ``import``.
   116 It could have also be done as,
   126 It could have also be done as,
       
   127 
       
   128 {{{ switch to next slide, better way of fixing }}}
   117 
   129 
   118 {{{ highlight the following in slide and say it loud }}}
   130 {{{ highlight the following in slide and say it loud }}}
   119 ::
   131 ::
   120 
   132 
   121     from scipy import linspace
   133     from scipy import linspace
   128 So in practice it is always good to use function names instead of
   140 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
   141 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
   142 module then it will replace any existing functions with the same name
   131 in our name-space.
   143 in our name-space.
   132 
   144 
       
   145 {{{ switch to next slide, Instead of ``*`` }}}
       
   146 
   133 So let us modify four_plot.py as,
   147 So let us modify four_plot.py as,
   134 {{{ delete the first two lines and add the following }}}
   148 {{{ delete the first two lines and add the following }}}
   135 ::
   149 ::
   136 
   150 
   137     from scipy import linspace, pi, sin
   151     from scipy import linspace, pi, sin
   138     from pylab import plot, legend, annotate, title, show
   152     from pylab import plot, legend, annotate
   139     from pylab import xlim, ylim
   153     from pylab import xlim, ylim, title, show
       
   154 
       
   155 Now let us try running the code again as,
       
   156 ::
       
   157 
       
   158     python four_plot.py
       
   159 
       
   160 It works! In this method we actually imported the functions to the
       
   161 current name-space, and there is another method of doing it. And that
       
   162 is,
   140 
   163 
   141 {{{ switch to next slide }}}
   164 {{{ 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 
   165 
   157 Notice that we use ``scipy.pi`` instead of just ``pi`` as in the
   166 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
   167 previous method, and the functions are called as ``pylab.plot()`` and
   159 ``pylab.annotate()`` and not as ``plot()`` and ``annotate()``.
   168 ``pylab.annotate()`` and not as ``plot()`` and ``annotate()``.
   160 
   169 
   209 ``http://docs.python.org/library/``
   218 ``http://docs.python.org/library/``
   210 
   219 
   211 The modules pylab, scipy, Mayavi are not part of the standard python
   220 The modules pylab, scipy, Mayavi are not part of the standard python
   212 library.
   221 library.
   213 
   222 
   214 {{{ switch to next slide, recap }}}
   223 {{{ switch to next slide, summary }}}
   215 
   224 
   216 This brings us to the end of this tutorial, in this tutorial we
   225 This brings us to the end of this tutorial, in this tutorial we
   217 learned running scripts from command line, learned about modules, saw
   226 learned running scripts from command line, learned about modules, saw
   218 the python standard library.
   227 the python standard library.
   219 
   228