writing_python_scripts/script.rst
changeset 464 0bb297415dfb
parent 460 5d032e253580
child 509 0775f177947a
equal deleted inserted replaced
463:192559461dcb 464:0bb297415dfb
    17    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    17    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    18 
    18 
    19 Script
    19 Script
    20 ------
    20 ------
    21 
    21 
       
    22 {{{ Show the slide containing title }}}
       
    23 
    22 Hello friends and welcome to the tutorial on "Writing Python scripts"
    24 Hello friends and welcome to the tutorial on "Writing Python scripts"
    23 
       
    24 {{{ Show the slide containing title }}}
       
    25 
    25 
    26 {{{ Show the slide containing the outline slide }}}
    26 {{{ Show the slide containing the outline slide }}}
    27 
    27 
    28 In this tutorial, we shall learn
    28 In this tutorial, we shall learn
    29 
    29 
    30  * How write Python scripts 
    30  * How to write Python scripts 
    31 
    31 
    32 Often we will have to reuse the code that we haave written. We do that by
    32 Often we will have to reuse the code that we haave written. We do that by
    33 writing functions. Functions are bundled into packages and are imported as and
    33 writing functions. Functions are bundled into packages and are imported as and
    34 required in the script.
    34 when required in other scripts.
    35 
    35 
    36 Let us first write a function that computes the gcd of two numbers and save it
    36 Let us first write a function that computes the gcd of two numbers and save it
    37 in a script.
    37 in a script.
    38 
    38 
    39 {{{ Open an editor and start typing out the following code }}}
    39 {{{ Open an editor and start typing out the following code }}}
    56     if gcd(40, 12) == 4:
    56     if gcd(40, 12) == 4:
    57         print "Everything OK"
    57         print "Everything OK"
    58     else:
    58     else:
    59         print "The GCD function is wrong"
    59         print "The GCD function is wrong"
    60 
    60 
    61 Let us save the file as script.py in /home/fossee/gcd_script.py
    61 Let us save the file as script.py in ``/home/fossee/gcd_script.py``
    62 
    62 
    63 We shall run the script by doing
    63 We shall run the script by typing
    64 ::
    64 ::
    65 
    65 
    66     $ python /home/fossee/gcd_script.py
    66     $ python /home/fossee/gcd_script.py
    67 
    67 
    68 We can see that the script is executed and everything is fine.
    68 We can see that the script is executed and everything is fine.
    69 
    69 
    70 What if we want to use the gcd function in some of our later scripts. This
    70 What if we want to use the gcd function in some of our other scripts. This
    71 is also possible since every python file can be used as a module.
    71 is also possible since every python file can be used as a module.
    72 
    72 
    73 But first, we shall understand what happens when you import a module.
    73 But first, we shall understand what happens when you import a module.
    74 
    74 
    75 Open IPython and type
    75 Open IPython and type
    79     sys.path
    79     sys.path
    80 
    80 
    81 This is a list of locations where python searches for a module when it
    81 This is a list of locations where python searches for a module when it
    82 encounters an import statement.
    82 encounters an import statement.
    83 
    83 
    84 hence when we just did =import sys=, python searches for a file named sys.py or
    84 Hence, when we just did ``import sys``, python searches for a file
    85 a folder named sys in all these locations one by one, until it finds one.
    85 named sys.py or a folder named sys in all these locations one by one,
       
    86 until it finds one.
    86 
    87 
    87 We can place our script in any one of these locations and can import it.
    88 We can place our script in any one of these locations and can import it.
    88 
    89 
    89 The first item in the list is an empty string which means the current working
    90 The first item in the list is an empty string which means the current
    90 directory is also searched. 
    91 working directory is also searched.
    91 
    92 
    92 Alternatively, we can also import the module if we are working in same 
    93 Alternatively, we can also import the module if we are working in same 
    93 directory where the script exists.
    94 directory where the script exists.
    94 
    95 
    95 Since we are in /home/fossee, we can simply do
    96 Since we are in /home/fossee, we can simply do
   101 the end of the file is also executed.
   102 the end of the file is also executed.
   102 
   103 
   103 But we want the test code to be executed only when the file is run as a python 
   104 But we want the test code to be executed only when the file is run as a python 
   104 script and not when it is imported.
   105 script and not when it is imported.
   105 
   106 
   106 This is possible by using =__name__= variable.
   107 This is possible by using ``__name__`` variable.
   107 
   108 
   108 First we shall look at how to use the idiom and then understand how it works.
   109 First, we shall look at how to use the idiom and then understand how it works.
   109 
   110 
   110 Go to the file and add
   111 Go to the file and add
   111 ::
   112 ::
   112 
   113 
   113     if __name__ == "__main__":
   114     if __name__ == "__main__":
   126     
   127     
   127     import gcd_script
   128     import gcd_script
   128 
   129 
   129 We see that now the test code is not executed.
   130 We see that now the test code is not executed.
   130 
   131 
   131 The __name__ variable is local to every module and it is equal to __main__ only
   132 The ``__name__`` variable is local to every module and it is equal to
   132 when the file is run as a script.
   133 ``__main__`` only when the file is run as a script.
   133 
   134 
   134 hence all the code that goes after __name__ == "__main__" is executed only when
   135 Hence, all the code that goes in to the if block, ``if __name__ ==
   135 the file is run as a python script.
   136 "__main__":`` is executed only when the file is run as a python
       
   137 script.
   136 
   138 
   137 {{{ Show summary slide }}}
   139 {{{ Show summary slide }}}
   138 
   140 
   139 This brings us to the end of the tutorial.
   141 This brings us to the end of the tutorial.
   140 we have learnt
   142 we have learnt
   143  * How to use a script as a module
   145  * How to use a script as a module
   144  * How to write test functions using the __name__ idiom 
   146  * How to write test functions using the __name__ idiom 
   145 
   147 
   146 {{{ Show the "sponsored by FOSSEE" slide }}}
   148 {{{ Show the "sponsored by FOSSEE" slide }}}
   147 
   149 
   148 #[Nishanth]: Will add this line after all of us fix on one.
       
   149 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   150 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   150 
   151 
   151 Hope you have enjoyed and found it useful.
   152 Hope you have enjoyed and found it useful.
   152 Thank you!
   153 Thank you!
   153 
   154