writing_python_scripts.rst
changeset 174 b50fa22ab6b8
parent 164 1709bdcea9d1
equal deleted inserted replaced
173:24289d3a4fdb 174:b50fa22ab6b8
    35     if gcd(40, 12) == 4:
    35     if gcd(40, 12) == 4:
    36         print "Everything OK"
    36         print "Everything OK"
    37     else:
    37     else:
    38         print "The GCD function is wrong"
    38         print "The GCD function is wrong"
    39 
    39 
    40 Let us save the file as script.py in /home/fossee
    40 Let us save the file as script.py in /home/fossee/gcd_script.py
    41 
    41 
    42 We shall run the script by doing
    42 We shall run the script by doing
    43 ::
    43 ::
    44 
    44 
    45     $ python /home/fossee/script.py
    45     $ python /home/fossee/gcd_script.py
    46 
    46 
    47 We can see that the script is executed and everything is fine.
    47 We can see that the script is executed and everything is fine.
    48 
    48 
    49 What if we want to use the gcd function in some of our later scripts. This
    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.
    50 is also possible since every python file can be used as a module.
    51 
    51 
    52 Let us understand what happens when you import a module.
    52 But first, we shall understand what happens when you import a module.
    53 
    53 
    54 Open IPython and type
    54 Open IPython and type
    55 ::
    55 ::
    56 
    56 
    57     import sys
    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.
    58 
   115 
    59 {{{ Show summary slide }}}
   116 {{{ Show summary slide }}}
    60 
   117 
    61 This brings us to the end of the tutorial.
   118 This brings us to the end of the tutorial.
    62 we have learnt
   119 we have learnt
    63 
   120 
    64  * how to use loadtxt to read files
   121  * What happens when we import a module
    65  * how to generate a least square fit
   122  * How to use a script as a module
       
   123  * How to write test functions using the __name__ idiom 
    66 
   124 
    67 {{{ Show the "sponsored by FOSSEE" slide }}}
   125 {{{ Show the "sponsored by FOSSEE" slide }}}
    68 
   126 
    69 #[Nishanth]: Will add this line after all of us fix on one.
   127 #[Nishanth]: Will add this line after all of us fix on one.
    70 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   128 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India