using python modules/script.rst
author anand
Thu, 11 Nov 2010 03:09:49 +0530
changeset 483 a773e2d075eb
parent 482 b554ce5a7b26
permissions -rw-r--r--
checklist OK for `using python modules`
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   :
482
b554ce5a7b26 language check done for `using python modules`
bhanu
parents: 440
diff changeset
    23
   Language Reviewer   : Bhanukiran
483
a773e2d075eb checklist OK for `using python modules`
anand
parents: 482
diff changeset
    24
   Checklist OK?       : <11-11-2010, Anand, OK> [2010-10-05]
319
e8c02b3c51ac changed headers of scripts(objectives and prerequisites).
Anoop Jacob Thomas<anoop@fossee.in>
parents: 309
diff changeset
    25
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
    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
====================
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
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
    29
====================
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
{{{ 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
    31
440
b2e8522839f0 Reviewed using python modules LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 371
diff changeset
    32
Welcome to the spoken tutorial on Using Python Modules.
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
    33
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
{{{ 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
    35
c7f0069d698a added base scripts and questions except for matrices and other-type-of-plots. previous commit only removed unwanted files.
anoop
parents:
diff changeset
    36
In this tutorial, we will see how to run python scripts from command
440
b2e8522839f0 Reviewed using python modules LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 371
diff changeset
    37
line. We'll see how to import modules, importing scipy and pylab
b2e8522839f0 Reviewed using python modules LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 371
diff changeset
    38
modules and have a look at the Python standard library.
356
8f6b3acb39ec Reviewed using python modules LO.
Puneeth Chaganti <punchagan@gmail.com>
parents: 319
diff changeset
    39
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
    40
{{{ 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
    41
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
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
    43
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
    44
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
{{{ 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
    46
::
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
    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
    49
    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
    50
440
b2e8522839f0 Reviewed using python modules LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 371
diff changeset
    51
and save the script as ``hello.py``,
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
    52
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
{{{ 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
    54
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
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
    56
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
    57
::
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
    %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
    60
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
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
    62
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
    63
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
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
    65
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
    66
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
{{{ 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
    68
309
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
    69
{{{ switch to next slide }}}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
    70
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
    71
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
    72
::
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
    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
    75
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
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
    77
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
{{{ 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
    79
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
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
    81
309
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
    82
{{{ switch to next slide, four plot problem }}}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
    83
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
    84
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
    85
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
    86
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
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
    88
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
{{{ 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
    90
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
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
    92
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
    93
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
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
    95
::
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
    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
    98
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
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
   100
``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
   101
``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
   102
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
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
   104
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
   105
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
   106
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
   107
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
   108
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
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
   110
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
   111
309
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   112
{{{ switch to next slide, fix ``linspace`` problem }}}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   113
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
   114
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
   115
{{{ 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
   116
::
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
    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
   119
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
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
   121
::
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
    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
   124
356
8f6b3acb39ec Reviewed using python modules LO.
Puneeth Chaganti <punchagan@gmail.com>
parents: 319
diff changeset
   125
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
   126
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
   127
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   128
{{{ switch to next slide, fix ``plot`` problem }}}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   129
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
   130
{{{ 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
   131
::
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
    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
   134
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
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
   136
::
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
    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
   139
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
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
   141
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
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
   143
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
   144
309
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   145
{{{ 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
   146
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
   147
{{{ 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
   148
::
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
    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
   151
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
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
   153
::
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
    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
   156
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
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
   158
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
   159
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
   160
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
   161
309
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   162
{{{ switch to next slide, Instead of ``*`` }}}
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   163
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
   164
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
   165
{{{ 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
   166
::
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
    from scipy import linspace, pi, sin
309
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   169
    from pylab import plot, legend, annotate
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   170
    from pylab import xlim, ylim, title, show
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   171
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   172
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
   173
::
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
    python four_plot.py
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   176
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   177
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
   178
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
   179
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
   180
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
{{{ 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
   182
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
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
   184
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
   185
``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
   186
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
{{{ 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
   188
440
b2e8522839f0 Reviewed using python modules LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 371
diff changeset
   189
%% %% Write a script to plot a sine wave from minus two pi to two pi.
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
   190
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
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
   192
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
   193
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
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
   195
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
{{{ 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
   197
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
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
   199
``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
   200
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
   201
``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
   202
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
   203
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
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
   205
{{{ 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
   206
::
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
    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
   209
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
{{{ 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
   211
356
8f6b3acb39ec Reviewed using python modules LO.
Puneeth Chaganti <punchagan@gmail.com>
parents: 319
diff changeset
   212
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
   213
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
   214
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
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
   216
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
   217
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
   218
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
{{{ 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
   220
356
8f6b3acb39ec Reviewed using python modules LO.
Puneeth Chaganti <punchagan@gmail.com>
parents: 319
diff changeset
   221
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
   222
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
   223
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
   224
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
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
   226
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
   227
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
   228
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
   229
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
   230
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
   231
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
   232
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
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
   234
``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
   235
440
b2e8522839f0 Reviewed using python modules LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 371
diff changeset
   236
There are a lot of other modules like pylab, scipy, Mayavi, etc which
b2e8522839f0 Reviewed using python modules LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 371
diff changeset
   237
are not part of the standard python 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
   238
309
9d8fd5ea64b2 added slides for using python modules.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 261
diff changeset
   239
{{{ 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
   240
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
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
   242
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
   243
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
   244
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
{{{ 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
   246
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
Thank you!