writing_python_scripts/script.rst
author bhanu
Mon, 15 Nov 2010 15:07:22 +0530
changeset 505 394c3642cf9c
parent 464 0bb297415dfb
child 509 0775f177947a
permissions -rw-r--r--
Language check done for `getting started with tuples`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     1
.. Objectives
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     2
.. ----------
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
     3
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     4
.. By the end of this tutorial, you will be able to 
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     5
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     6
..  * Understand what is importing
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     7
..  * Write your own Python modules
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     8
..  * Understand the ``__name__=="__main__"`` idiom
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
     9
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    10
.. Prerequisites
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    11
.. -------------
335
d5248a15274c Finished writing_python_scripts
Nishanth <nishanth@fossee.in>
parents: 296
diff changeset
    12
.. 1. Using Python modules
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    13
     
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    14
.. Author              : Nishanth Amuluru
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    15
   Internal Reviewer   : 
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    16
   External Reviewer   :
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    17
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    18
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    19
Script
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    20
------
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    21
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    22
{{{ Show the slide containing title }}}
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    23
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    24
Hello friends and welcome to the tutorial on "Writing Python scripts"
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    25
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    26
{{{ Show the slide containing the outline slide }}}
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    27
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    28
In this tutorial, we shall learn
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    29
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    30
 * How to write Python scripts 
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    31
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    32
Often we will have to reuse the code that we haave written. We do that by
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    33
writing functions. Functions are bundled into packages and are imported as and
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    34
when required in other scripts.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    35
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    36
Let us first write a function that computes the gcd of two numbers and save it
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    37
in a script.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    38
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    39
{{{ Open an editor and start typing out the following code }}}
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    40
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    41
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    42
    def gcd(a, b):
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    43
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    44
        while b:
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    45
            a, b = b, a%b
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    46
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    47
        return a
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    48
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    49
We shall write an test function in the script that tests the gcd function every
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    50
time the script is run.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    51
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    52
{{{ Add to the script }}}
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    53
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    54
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    55
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    56
    if gcd(40, 12) == 4:
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    57
        print "Everything OK"
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    58
    else:
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    59
        print "The GCD function is wrong"
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    60
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    61
Let us save the file as script.py in ``/home/fossee/gcd_script.py``
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    62
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    63
We shall run the script by typing
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    64
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    65
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    66
    $ python /home/fossee/gcd_script.py
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    67
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    68
We can see that the script is executed and everything is fine.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    69
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    70
What if we want to use the gcd function in some of our other scripts. This
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    71
is also possible since every python file can be used as a module.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    72
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    73
But first, we shall understand what happens when you import a module.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    74
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    75
Open IPython and type
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    76
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    77
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    78
    import sys
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    79
    sys.path
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    80
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    81
This is a list of locations where python searches for a module when it
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    82
encounters an import statement.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    83
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    84
Hence, when we just did ``import sys``, python searches for a file
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    85
named sys.py or a folder named sys in all these locations one by one,
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    86
until it finds one.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    87
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    88
We can place our script in any one of these locations and can import it.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    89
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    90
The first item in the list is an empty string which means the current
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    91
working directory is also searched.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    92
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    93
Alternatively, we can also import the module if we are working in same 
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    94
directory where the script exists.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    95
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    96
Since we are in /home/fossee, we can simply do
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    97
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    98
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    99
    import gcd_script
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   100
    
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   101
We can see that the gcd_script is imported. But the test code that we added at
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   102
the end of the file is also executed.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   103
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   104
But we want the test code to be executed only when the file is run as a python 
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   105
script and not when it is imported.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   106
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   107
This is possible by using ``__name__`` variable.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   108
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   109
First, we shall look at how to use the idiom and then understand how it works.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   110
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   111
Go to the file and add
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   112
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   113
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   114
    if __name__ == "__main__":
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   115
        
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   116
before the test code and indent the test code.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   117
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   118
Let us first run the code.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   119
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   120
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   121
    $ python gcd_script.py
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   122
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   123
We can see that the test runs successfully.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   124
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   125
Now we shall import the file
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   126
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   127
    
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   128
    import gcd_script
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   129
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   130
We see that now the test code is not executed.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   131
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   132
The ``__name__`` variable is local to every module and it is equal to
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   133
``__main__`` only when the file is run as a script.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   134
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   135
Hence, all the code that goes in to the if block, ``if __name__ ==
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   136
"__main__":`` is executed only when the file is run as a python
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   137
script.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   138
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   139
{{{ Show summary slide }}}
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   140
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   141
This brings us to the end of the tutorial.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   142
we have learnt
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   143
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   144
 * What happens when we import a module
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   145
 * How to use a script as a module
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   146
 * How to write test functions using the __name__ idiom 
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   147
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   148
{{{ Show the "sponsored by FOSSEE" slide }}}
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   149
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   150
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   151
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   152
Hope you have enjoyed and found it useful.
460
5d032e253580 Changes to getting started with Sage notebook.
Puneeth Chaganti <punchagan@fossee.in>
parents: 335
diff changeset
   153
Thank you!
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   154