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