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