writing_python_scripts/script.rst
author bhanu
Mon, 15 Nov 2010 15:20:54 +0530
changeset 509 0775f177947a
parent 464 0bb297415dfb
child 510 06561ee0f6a4
permissions -rw-r--r--
Language check done for `writing python scripts`
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
509
0775f177947a Language check done for `writing python scripts`
bhanu
parents: 464
diff changeset
    15
   Internal Reviewer   : Punch
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    16
   External Reviewer   :
509
0775f177947a Language check done for `writing python scripts`
bhanu
parents: 464
diff changeset
    17
   Language Reviewer   : Bhanukiran
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    18
   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
    19
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    20
Script
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    21
------
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    22
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    23
{{{ Show the slide containing title }}}
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    24
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    25
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
    26
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    27
{{{ Show the slide containing the outline slide }}}
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    28
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    29
In this tutorial, we shall learn
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    30
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    31
 * How to write Python scripts 
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    32
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    33
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
    34
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
    35
when required in other scripts.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    36
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    37
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
    38
in a script.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    39
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    40
{{{ 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
    41
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    42
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    43
    def gcd(a, b):
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    44
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    45
        while b:
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    46
            a, b = b, a%b
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    47
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    48
        return a
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    49
509
0775f177947a Language check done for `writing python scripts`
bhanu
parents: 464
diff changeset
    50
We shall write a test function in the script that tests the gcd function every
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    51
time the script is run.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    52
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    53
{{{ Add to the script }}}
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
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    57
    if gcd(40, 12) == 4:
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    58
        print "Everything OK"
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    59
    else:
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    60
        print "The GCD function is wrong"
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    61
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    62
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
    63
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    64
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
    65
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    66
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    67
    $ python /home/fossee/gcd_script.py
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    68
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    69
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
    70
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    71
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
    72
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
    73
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    74
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
    75
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    76
Open IPython and type
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
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    79
    import sys
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    80
    sys.path
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    81
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    82
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
    83
encounters an import statement.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    84
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    85
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
    86
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
    87
until it finds one.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    88
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    89
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
    90
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
    91
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
    92
working directory is also searched.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    93
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    94
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
    95
directory where the script exists.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    96
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    97
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
    98
::
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
    99
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   100
    import gcd_script
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   101
    
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   102
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
   103
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
   104
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   105
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
   106
script and not when it is imported.
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   107
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   108
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
   109
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   110
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
   111
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   112
Go to the file and add
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
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   115
    if __name__ == "__main__":
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   116
        
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   117
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
   118
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   119
Let us first run the code.
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
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   122
    $ python gcd_script.py
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   123
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   124
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
   125
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   126
Now we shall import the file
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
    
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   129
    import gcd_script
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   130
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   131
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
   132
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   133
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
   134
``__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
   135
464
0bb297415dfb Reviewed writing python scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents: 460
diff changeset
   136
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
   137
"__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
   138
script.
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   139
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   140
{{{ Show summary slide }}}
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   141
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   142
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
   143
we have learnt
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   144
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   145
 * What happens when we import a module
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   146
 * 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
   147
 * 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
   148
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   149
{{{ Show the "sponsored by FOSSEE" slide }}}
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   150
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   151
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
   152
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   153
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
   154
Thank you!
296
641a6ee868c0 made the script writing_python_scripts into new form
Nishanth <nishanth@fossee.in>
parents:
diff changeset
   155