Python/C4/Writing-python-scripts/English-timed

From Script | Spoken-Tutorial
Revision as of 17:46, 2 January 2013 by Minal (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
0:01 Hello friends and welcome to the tutorial on "Writing Python scripts".
0:05 At the end of this tutorial, you will be able to,
  1. Understand what is importing.
  2. Write your own Python modules.
  3. Understand the __name__ == in double quotes __main__ underscore idiom
0:19 Before beginning this tutorial,we would suggest you to complete the tutorial on "Using Python modules".
0:25 Often we will have to reuse the code that we have written.
0:29 We do that by writing functions.
0:31 Functions are bundled into packages and are imported as and when required in other scripts.
0:37 Let us first write a function that computes the gcd of two numbers and save it in a script.
0:44 Open an editor and type the code.
0:47 Please take care of the indentation.
0:51 So type in terminal gcd underscore script.py then type def gcd within bracket a comma b colon
while b colon
a comma  b = b comma  a percentage b
   return a
1:49 We shall write a test function in the script that tests the gcd function every time the script is run.
1:56 So type

if gcd within bracket 40 comma 12 == 4 colon

   print  within double quotes Everything OK

else colon

   print  within double quotes The GCD function is wrong


2:53 Let us save the file as script.py in slash home slash fossee slash gcd script.py
3:05 We shall run the script by typing in the terminal
3:11 python slash home slash fossee slash gcd underscore script.py
3:25 We can see that the script is executed and everything is fine.
3:29 What if we want to use the gcd function in some of our other scripts.
3:35 This is also possible since every python file can be used as a module.
3:39 But first, we shall understand what happens when you import a module.
3:43 So type ipython in terminal.
3:52 Open IPython and type import sys (hit enter)
                       sys.path
4:15 This is a list of locations where python searches for a module when it encounters an import statement.
4:20 Hence, when we just did import sys, python searches for a file named sys.py or a folder named sys in all these locations one by one, until it finds one.
4:34 We can place our script in any one of these locations and can import it.
4:38 The first item in the list is an empty string which means the current working directory is also searched.
4:45 Alternatively, we can also import the module if we are working in same directory where the script exists.
4:53 Since we are in slash home slash fossee, we can simply do
4:59 By typing on the terminal import gcd underscore script.py and hit enter.
5:12 We can see that the gcd underscore script is imported.
5:18 But the test code that we added at the end of the file is also executed.
5:23 But we want the test code to be executed only when the file is run as a python script or not when it is imported.
5:30 This is possible by using underscore underscore name underscore underscore variable.
5:34 First, we shall look at how to use the idiom and then understand how it works.
5:41 Go to the file and add this line as the beginning of the code and indent the code accordingly.
5:47 So type if underscore underscore name underscore underscore == within double quotes underscore underscore main underscore underscore colon
6:30 Let us first run the code.
6:33 So type in terminal python gcd underscore script.py
6:45 We can see that
6:48 There is an error coming up, its showing indentation error.
6:57 So we have to edit the file and make indentation correct.
7:20 We can see that the test runs successfully.
7:24 Now we shall import the file gcd underscore script
7:29 So type import gcd underscore script
7:39 We see that now the test code is not executed.

The underscore underscore name underscore underscore variable is local to every module and it is equal to underscore underscore main underscore underscore only when the file is run as a script.

7:54 Hence, all the code that goes in to the if block, if name == within double quotes main colon is executed only when the file is run as a python script.
8:08 This brings us to the end of the tutorial.
8:11 In this tutorial, we have learnt to, 1. Know what happens when we import a module.
8:16 2.Use a script as a module.
8:18 3. Write test functions using the name idiom.
8:22 Here are some self assessment questions for you to solve
8:26 1. Which of the following variables contains the locations to search for
python modules
    • sys.pythonpath
    • sys.path
    • os.pythonpath
    • os.path
8:38 2. A module should contain only functions. - True - False
8:43 3. The script utils.py is in one of locations of PYTHONPATH and contains the following code and the code is given.
9:00 And now look at the answers,
9:04 1. sys.path contains the locations to search for python modules.
9:10 2. False is the answer.
9:13 A module which contain a range of functions.
9:19 3. After doing import utils, we can use the function show() as,
utils.show within bracket double quotes hey
9:32 So we hope you have enjoyed this tutorial and found it useful.
9:35 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha