diff options
author | Jovina | 2011-07-13 16:29:34 +0530 |
---|---|---|
committer | Jovina | 2011-07-13 16:29:34 +0530 |
commit | 648ec6935bda1d7835ea8fcc6e0e7cca000148bd (patch) | |
tree | c8db71cbba330efe4543429883591cbf3042ad98 /using_python_modules/script.rst | |
parent | bc1bdc9be493bada965904f7c6235aa09a082b18 (diff) | |
download | st-scripts-648ec6935bda1d7835ea8fcc6e0e7cca000148bd.tar.gz st-scripts-648ec6935bda1d7835ea8fcc6e0e7cca000148bd.tar.bz2 st-scripts-648ec6935bda1d7835ea8fcc6e0e7cca000148bd.zip |
Major changes to script of 'using python modules'.
Diffstat (limited to 'using_python_modules/script.rst')
-rw-r--r-- | using_python_modules/script.rst | 322 |
1 files changed, 283 insertions, 39 deletions
diff --git a/using_python_modules/script.rst b/using_python_modules/script.rst index 0b0858b..9f95682 100644 --- a/using_python_modules/script.rst +++ b/using_python_modules/script.rst @@ -27,75 +27,161 @@ ==================== Using Python modules ==================== -{{{ show the welcome slide }}} -Welcome to the spoken tutorial on Using Python Modules. +.. L1 -{{{ switch to next slide, outline slide }}} +{{{ Show the first slide containing title, name of the production +team along with the logo of MHRD }}} -In this tutorial, we will see how to run python scripts from command -line. We'll see how to import modules, importing scipy and pylab -modules and have a look at the Python standard library. +.. R1 -{{{ switch to next slide on executing python scripts from command line }}} +Hello Friends and Welcome to the spoken tutorial on +'Using Python Modules'. + +.. L2 + +{{{ switch to objectives slide }}} + +.. R2 + +At the end of this tutorial, you will be able to , + + 1. Execute python scripts from command line. + #. Use import in scripts. + #. Import scipy and pylab modules. + #. Use python standard modules and 3rd party modules. + +.. L3 + +{{{ Switch to the pre-requisite slide }}} + +.. R3 + +Before beginning this tutorial,we would suggest you to complete the +tutorial on "Using plot interactively", "Embellishing a plot" and +"Saving plots". + +.. L4 + +{{{ switch to next slide on running python scripts from +command line }}} + +.. R5 Let us create a simple python script to print hello world. Open your text editor and type the following, +.. L6 + {{{ open the text editor and type the following }}} :: print "Hello world!" print -and save the script as ``hello.py``, +.. R6 + +.. R7 + +Now save this script as ``hello.py``, + +.. L7 {{{ save the script as hello.py }}} -Till now we saw how to run a script using the IPython interpreter -using the +.. R8 + +Start the ipython interpreter + +.. L8 + +{{{ Open the terminal }}} +:: + + ipython + +.. R9 + +In the previous tutorials,we have seen how to run a script using +the IPython interpreter using ``%run`` + +.. L9 :: %run -i hello.py -option, but that is not the correct way of running a python +.. R10 + +but this is not the correct way of running a python script. +.. L10 + +{{{ Close the terminal }}} + +.. R11 + The correct method is to run it using the Python interpreter. Open the terminal and navigate to the directory where hello.py is, +.. L11 + {{{ open terminal and navigate to directory where hello.py was saved }}} -{{{ switch to next slide }}} +.. R12 now run the Python script as, + +.. L12 :: python hello.py +.. R13 + It executed the script and we got the output ``Hello World!``. +.. L13 + +.. L14 + +{{{ switch to next slide }}} {{{ highlight ``python filename`` syntax on slide while narrating }}} -The syntax is python space filename. +.. R14 + +The syntax is ``python space filename``. + +.. L15 {{{ switch to next slide, four plot problem }}} -Now recall the four plot problem where we plotted four plots in a single -figure. Let us run that script from command line. +.. R15 + +Now, we have a four plot problem where we have plotted four plots in a +single figure. Let us run that script from command line. + +.. R16 + +If you don't have the script, then make one with the following set of +commands -If you don't have the script, +.. L16 -{{{ open the four_plot.py file in text editor }}} +{{{ open the four_plot.py file in text editor and show }}} +{{{ Pause for some time and then continue }}} -just pause here and create a python script with the following lines -and save it as four_plot.py. +.. R17 Now let us run four_plot.py as a python script. + +.. L17 :: python four_plot.py +.. R18 + Oops! even though it was supposed to work, it didn't. It gave an error ``linspace()`` is not defined, which means that the function ``linspace()`` is not available in the current name-space. @@ -109,48 +195,88 @@ starts. And thus we don't have to explicitly import modules. So now let us try to fix the problem and run the script in command line, +.. L18 + +.. L19 + {{{ switch to next slide, fix ``linspace`` problem }}} -add the following line as the first line in the script, +.. R19 + +add this line as the first line in the script, + +.. L20 + {{{ add the line as first line in four_plot.py and save }}} :: from scipy import * +.. R20 + +.. R21 + Now let us run the script again, + +.. L21 :: python four_plot.py -Now it gave another error -- plot not defined, let us edit the file -again and add the line below the line we just added, +.. R22 + +Now it gave another error -- plot not defined, + +.. L22 + +.. L23 {{{ switch to next slide, fix ``plot`` problem }}} +.. R23 + +let us edit the file again and add this line as the +second line in our script and save it, + +.. L24 + {{{ add the line as second line in four_plot.py and save }}} :: from pylab import * -And run the script, +.. R24 + +.. R25 + +And now, run the script, + +.. L25 + +{{{ Switch to the terminal }}} :: python four_plot.py +.. R26 + Yes! it worked. So what did we do? We actually imported the required modules using the keyword ``import``. -It could have also be done as, +It could also be done as by using, + +.. L26 + +.. L27 {{{ switch to next slide, better way of fixing }}} +{{{ highlight the required line while narrating }}} -{{{ highlight the following in slide and say it loud }}} -:: +.. R27 from scipy import linspace instead of, -:: from scipy import * @@ -159,9 +285,19 @@ asterisk or star. If we use asterisk to import from a particular module then it will replace any existing functions with the same name in our name-space. +.. L28 + {{{ switch to next slide, Instead of ``*`` }}} +.. R28 + So let us modify four_plot.py as, +Hence we delete the first two lines of our code which we had added +and add these lines + +.. L29 + +{{{ Switch to script 'four_plot.py' }}} {{{ delete the first two lines and add the following }}} :: @@ -169,46 +305,89 @@ So let us modify four_plot.py as, from pylab import plot, legend, annotate from pylab import xlim, ylim, title, show +.. R29 + +.. R30 + Now let us try running the code again as, + +.. L30 :: python four_plot.py +.. R31 + It works! In this method we actually imported the functions to the -current name-space, and there is another method of doing it. And that +current name-space.There is one more way of doing it. And that is, -{{{ switch to next slide }}} +.. L31 + +.. L32 + +{{{ switch to slide 'another fix' }}} +{{{ highlight the required line while narrating }}} + +.. R32 Notice that we use ``scipy.pi`` instead of just ``pi`` as in the previous method, and the functions are called as ``pylab.plot()`` and ``pylab.annotate()`` and not as ``plot()`` and ``annotate()``. -{{{ switch to next slide, problem statement }}} +Pause the video here, try out the following exercise and resume the video. + +.. L33 -%% %% Write a script to plot a sine wave from minus two pi to two pi. +{{{ Show slide with exercise 1 }}} -Pause here and try to solve the problem yourself before looking at the -solution. +.. R33 + Write a script to plot a sine wave from minus two pi to two pi. +<Pause> It can solved as, +.. L34 + {{{ open sine.py and show it }}} -the first line we import the required functions ``linspace()`` and -``sin()`` and constant ``pi`` from the module scipy. the second and +.. R34 + +The first line we import the required functions ``linspace()`` , +``sin()`` and constant ``pi`` from the module scipy. The second and third line we import the functions ``plot()``, ``legend()``, ``show()``, ``title()``, ``xlabel()`` and ``ylabel()``. And the rest the code to generate the plot. +.. L35 + +{{{ Pause for sometime and then continue }}} + +.. R35 + +.. R36 + We can run it as, -{{{ now switch focus to terminal and run the script }}} + +.. L36 + +{{{ Switch to the terminal }}} :: python sine.py +.. R37 + +As we can see, we our sine plot.Let us move further in our topic. + +.. L37 + +.. L38 + {{{ switch to next slide, What is a module? }}} +.. R38 + Until now we have been learning about importing modules, now what is a module? @@ -216,8 +395,12 @@ A module is simply a file containing Python definitions and statements. Definitions from a module can be imported into other modules or into the main module. +.. L39 + {{{ switch to next slide, Python standard library }}} +.. R39 + Python has a very rich standard library of modules. It is very extensive, offering a wide range of facilities. Some of the standard modules are, @@ -236,12 +419,73 @@ Find more information at Python Library reference, There are a lot of other modules like pylab, scipy, Mayavi, etc which are not part of the standard python library. +.. L40 + {{{ switch to next slide, summary }}} -This brings us to the end of this tutorial, in this tutorial we -learned running scripts from command line, learned about modules, saw -the python standard library. +.. R40 + +This brings us to the end of this tutorial. In this tutorial, we have +learnt to, + + 1. Run scripts from command line, + #. Import modules by specifying the module name followed by + an asterisk. + #. Import only the required functions from modules by specifying + the function name. + #. Use python standard library. + +.. L41 + +{{{Show self assessment questions slide}}} + +.. R41 + +Here are some self assessment questions for you to solve + +1. Which among this is correct ? + + - from scipy import plot + - from numpy import plot + - from matplotlib import plot + - from pylab import plot + +2. Which among these libraries is part of python standard library ? + + - Mayavi + - scipy + - matplotlib + - urllib2 + +3. Functions ``xlim()`` and ``ylim()`` can be imported to the current + name-space as, + + - from pylab import xlim, ylim + - import pylab + - from scipy import xlim, ylim + - import scipy + +.. L42 + +{{{solution of self assessment questions on slide}}} + +.. R42 + +And the answers, + +1. The option ``from pylab import plot`` is the correct one, since plot + is a function of module module. + +2. ``urllib2`` is a part of the python standard library. + +3. Functions ``xlim()`` and ``ylim()`` can be imported to the current + name-space as, ``from pylab import xlim, ylim``. + +.. L23 {{{ switch to next slide, thank you slide }}} +.. R43 + +Hope you have enjoyed this tutorial and found it useful. Thank you! |