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