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