author | Puneeth Chaganti <punchagan@fossee.in> |
Wed, 06 Oct 2010 19:32:18 +0530 | |
changeset 240 | f4c36aa38a84 |
parent 174 | b50fa22ab6b8 |
permissions | -rw-r--r-- |
164 | 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 |
||
174
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
40 |
Let us save the file as script.py in /home/fossee/gcd_script.py |
164 | 41 |
|
42 |
We shall run the script by doing |
|
43 |
:: |
|
44 |
||
174
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
45 |
$ python /home/fossee/gcd_script.py |
164 | 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 |
||
174
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
52 |
But first, we shall understand what happens when you import a module. |
164 | 53 |
|
54 |
Open IPython and type |
|
55 |
:: |
|
56 |
||
57 |
import sys |
|
174
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
58 |
sys.path |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
59 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
60 |
This is a list of locations where python searches for a module when it |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
61 |
encounters an import statement. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
62 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
63 |
hence when we just did =import sys=, python searches for a file named sys.py or |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
64 |
a folder named sys in all these locations one by one, until it finds one. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
65 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
66 |
We can place our script in any one of these locations and can import it. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
67 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
68 |
The first item in the list is an empty string which means the current working |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
69 |
directory is also searched. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
70 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
71 |
Alternatively, we can also import the module if we are working in same |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
72 |
directory where the script exists. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
73 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
74 |
Since we are in /home/fossee, we can simply do |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
75 |
:: |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
76 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
77 |
import gcd_script |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
78 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
79 |
We can see that the gcd_script is imported. But the test code that we added at |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
80 |
the end of the file is also executed. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
81 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
82 |
But we want the test code to be executed only when the file is run as a python |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
83 |
script and not when it is imported. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
84 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
85 |
This is possible by using =__name__= variable. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
86 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
87 |
First we shall look at how to use the idiom and then understand how it works. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
88 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
89 |
Go to the file and add |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
90 |
:: |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
91 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
92 |
if __name__ == "__main__": |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
93 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
94 |
before the test code and indent the test code. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
95 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
96 |
Let us first run the code. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
97 |
:: |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
98 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
99 |
$ python gcd_script.py |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
100 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
101 |
We can see that the test runs successfully. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
102 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
103 |
Now we shall import the file |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
104 |
:: |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
105 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
106 |
import gcd_script |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
107 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
108 |
We see that now the test code is not executed. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
109 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
110 |
The __name__ variable is local to every module and it is equal to __main__ only |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
111 |
when the file is run as a script. |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
112 |
|
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
113 |
hence all the code that goes after __name__ == "__main__" is executed only when |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
114 |
the file is run as a python script. |
164 | 115 |
|
116 |
{{{ Show summary slide }}} |
|
117 |
||
118 |
This brings us to the end of the tutorial. |
|
119 |
we have learnt |
|
120 |
||
174
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
121 |
* What happens when we import a module |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
122 |
* How to use a script as a module |
b50fa22ab6b8
writing_python_scripts is complete
Nishanth <nishanth@fossee.in>
parents:
164
diff
changeset
|
123 |
* How to write test functions using the __name__ idiom |
164 | 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 : |