writing_python_scripts.rst
author Puneeth Chaganti <punchagan@fossee.in>
Wed, 06 Oct 2010 19:32:18 +0530
changeset 240 f4c36aa38a84
parent 174 b50fa22ab6b8
permissions -rw-r--r--
Cleaned up the script of advanced features of functions LO.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
164
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on "Writing Python scripts"
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
     2
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
     3
{{{ Show the slide containing title }}}
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
     4
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
     5
{{{ Show the slide containing the outline slide }}}
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
     6
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
     7
In this tutorial, we shall learn
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
     8
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
     9
 * How write Python scripts 
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    10
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    11
Often we will have to reuse the code that we haave written. We do that by
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    12
writing functions. Functions are bundled into packages and are imported as and
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    13
required in the script.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    14
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    15
Let us first write a function that computes the gcd of two numbers and save it
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    16
in a script.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    17
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    18
{{{ Open an editor and start typing out the following code }}}
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    19
::
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    20
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    21
    def gcd(a, b):
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    22
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    23
        while b:
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    24
            a, b = b, a%b
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    25
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    26
        return a
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    27
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    28
We shall write an test function in the script that tests the gcd function every
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    29
time the script is run.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    30
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    31
{{{ Add to the script }}}
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    32
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    33
::
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    34
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    35
    if gcd(40, 12) == 4:
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    36
        print "Everything OK"
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    37
    else:
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    38
        print "The GCD function is wrong"
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    39
174
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    40
Let us save the file as script.py in /home/fossee/gcd_script.py
164
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    41
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    42
We shall run the script by doing
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    43
::
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    44
174
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    45
    $ python /home/fossee/gcd_script.py
164
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    46
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    47
We can see that the script is executed and everything is fine.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    48
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    49
What if we want to use the gcd function in some of our later scripts. This
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    50
is also possible since every python file can be used as a module.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    51
174
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    52
But first, we shall understand what happens when you import a module.
164
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    53
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    54
Open IPython and type
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    55
::
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    56
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    57
    import sys
174
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    58
    sys.path
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    59
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    60
This is a list of locations where python searches for a module when it
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    61
encounters an import statement.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    62
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    63
hence when we just did =import sys=, python searches for a file named sys.py or
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    64
a folder named sys in all these locations one by one, until it finds one.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    65
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    66
We can place our script in any one of these locations and can import it.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    67
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    68
The first item in the list is an empty string which means the current working
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    69
directory is also searched. 
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    70
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    71
Alternatively, we can also import the module if we are working in same 
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    72
directory where the script exists.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    73
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    74
Since we are in /home/fossee, we can simply do
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    75
::
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    76
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    77
    import gcd_script
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    78
    
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    79
We can see that the gcd_script is imported. But the test code that we added at
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    80
the end of the file is also executed.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    81
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    82
But we want the test code to be executed only when the file is run as a python 
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    83
script and not when it is imported.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    84
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    85
This is possible by using =__name__= variable.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    86
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    87
First we shall look at how to use the idiom and then understand how it works.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    88
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    89
Go to the file and add
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    90
::
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    91
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    92
    if __name__ == "__main__":
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    93
        
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    94
before the test code and indent the test code.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    95
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    96
Let us first run the code.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    97
::
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    98
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
    99
    $ python gcd_script.py
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   100
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   101
We can see that the test runs successfully.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   102
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   103
Now we shall import the file
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   104
::
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   105
    
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   106
    import gcd_script
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   107
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   108
We see that now the test code is not executed.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   109
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   110
The __name__ variable is local to every module and it is equal to __main__ only
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   111
when the file is run as a script.
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   112
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   113
hence all the code that goes after __name__ == "__main__" is executed only when
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   114
the file is run as a python script.
164
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   115
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   116
{{{ Show summary slide }}}
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   117
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   118
This brings us to the end of the tutorial.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   119
we have learnt
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   120
174
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   121
 * What happens when we import a module
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   122
 * How to use a script as a module
b50fa22ab6b8 writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents: 164
diff changeset
   123
 * How to write test functions using the __name__ idiom 
164
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   124
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   125
{{{ Show the "sponsored by FOSSEE" slide }}}
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   126
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   127
#[Nishanth]: Will add this line after all of us fix on one.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   128
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   129
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   130
Hope you have enjoyed and found it useful.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   131
Thankyou
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   132
 
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   133
.. Author              : Nishanth
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   134
   Internal Reviewer 1 : 
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   135
   Internal Reviewer 2 : 
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
   136
   External Reviewer   :