writing_python_scripts.rst
author Madhusudan.C.S <madhusudancs@gmail.com>
Fri, 17 Sep 2010 18:17:05 +0530
changeset 165 53df732199a1
parent 164 1709bdcea9d1
child 174 b50fa22ab6b8
permissions -rw-r--r--
Completed multiple plots script and for review with reviewers names.
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
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    40
Let us save the file as script.py in /home/fossee
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
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    45
    $ python /home/fossee/script.py
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
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    52
Let us understand what happens when you import a module.
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
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    58
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    59
{{{ Show summary slide }}}
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    60
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    61
This brings us to the end of the tutorial.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    62
we have learnt
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    63
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    64
 * how to use loadtxt to read files
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    65
 * how to generate a least square fit
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    66
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    67
{{{ Show the "sponsored by FOSSEE" slide }}}
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    68
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    69
#[Nishanth]: Will add this line after all of us fix on one.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    70
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    71
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    72
Hope you have enjoyed and found it useful.
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    73
Thankyou
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    74
 
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    75
.. Author              : Nishanth
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    76
   Internal Reviewer 1 : 
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    77
   Internal Reviewer 2 : 
1709bdcea9d1 initial commit writing_python_scripts
nishanth
parents:
diff changeset
    78
   External Reviewer   :