Python for Lectures V05 – Jan 2011 by
[email protected] 2010/11
Content 1 2
Installati Installation on ................................ .................................................. ................................... .................................. .................................. .................................. ......................3 .....3 First Trials................ Trials................................. ................................... ................................... .................................. .................................. .................................. ......................3 .....3 2.1 Pylab Pylab and IPython IPython ................................ ................................................. ................................... ................................... .................................. ................... ..3 3 2.2 IDLE....................................... IDLE........................................................ .................................. .................................. ................................... ..................................4 ................4 2.3 SciTe Editor Editor ................................. .................................................. .................................. .................................. .................................. ............................5 ...........5 3 Session Session 2............................. 2.............................................. .................................. .................................. .................................. .................................. ............................6 ...........6 3.1 Short programs programs .................................. ................................................... .................................. .................................. .................................. ......................6 .....6 3.2 Tutorials Tutorials ................................. .................................................. .................................. .................................. ................................... ..................................8 ................8 4 Modules, Modules, Plotting, Plotting, etc. .................................. ................................................... ................................... ................................... .................................. ................... ..8 8 4.1 Modules Modules ................................. .................................................. .................................. .................................. ................................... ..................................8 ................8 4.2 Plotting Plotting............... ................................ .................................. .................................. ................................... ................................... .................................. ................... ..9 9 5 Statements, control flow, indentation, advantage...............................................................13 5.1 for loop......................................... loop.......................................................... .................................. .................................. .................................. ..........................14 .........14 5.2 Indentatio Indentation n ................................ ................................................. .................................. .................................. .................................. .............................14 ............14 5.3 if – elif elif – else.................. else ................................... .................................. .................................. .................................. .................................. ....................... ......14 14 5.4 while...................... while....................................... .................................. .................................. .................................. ................................... ................................15 ..............15 5.5 try........................................ try......................................................... .................................. ................................... ................................... ..................................15 .................15 5.6 Advantages Advantages of Python: Python: .................................. ................................................... .................................. .................................. ..........................16 .........16 6 Numbers, Numbers, Strings, Strings, Boolean......................... Boolean.......................................... .................................. .................................. .................................. .................... ...16 16 6.1 Everythi Everything ng is an Object Object ................................. .................................................. .................................. .................................. ..........................16 .........16 6.2 Number Number formats formats ................................. .................................................. .................................. .................................. .................................. .................... ...16 16 6.3 Strings...................... Strings....................................... .................................. .................................. .................................. .................................. .............................18 ............18 6.4 Truth Value, Value, Boolean Boolean Operations Operations ................................ ................................................. .................................. .............................18 ............18 6.5 Declarin Declaring g variabl variables.................... es..................................... .................................. .................................. .................................. .............................19 ............19 7 Data Datatypes types ................................. ................................................... ................................... .................................. .................................. .................................. .................... ...20 20 7.1 Lists ................................. ................................................... ................................... .................................. .................................. .................................. .................... ...20 20 7.2 Tuples Tuples ................................. .................................................. .................................. ................................... ................................... ..................................21 .................21 7.3 Dictionar Dictionaries ies .................................. ................................................... .................................. .................................. .................................. ..........................22 .........22 8 Functions............ Functions............................. .................................. .................................. .................................. .................................. .................................. ..........................23 .........23 8.1 One return return value value .................................. ................................................... ................................... ................................... ..................................23 .................23 8.2 No return return value............... value................................ .................................. .................................. .................................. .................................. ....................... ......24 24 8.3 Multiple Multiple return values values ................................. .................................................. .................................. .................................. .............................24 ............24 8.4 Default Default argument argument values values .................................. ................................................... .................................. .................................. ....................... ......25 25 8.5 Lambda Lambda expressi expressions ons .................................. ................................................... .................................. .................................. .............................26 ............26 8.6 Built-in Built-in functions..................... functions...................................... .................................. .................................. ................................... ................................26 ..............26 9 Important Important built-in built-in modules modules .................................. ................................................... .................................. .................................. .............................27 ............27 9.1 The sys module...................... module....................................... .................................. .................................. ................................... ................................28 ..............28 9.2 The os module module ................................... .................................................... .................................. .................................. .................................. .................... ...29 29 10 Pitfalls Pitfalls .................................. .................................................... ................................... .................................. .................................. .................................. .................... ...31 31 10.1 The object object pitfall pitfall ................................. .................................................. .................................. .................................. .................................. .................... ...31 31 10.2 More pitfalls................. pitfalls.................................. .................................. .................................. .................................. .................................. ..........................33 .........33 11 Introspecti Introspection on ................................. .................................................. .................................. .................................. .................................. .............................33 ............33 12 Appendix................... Appendix.................................... .................................. .................................. .................................. ................................... ................................33 ..............33
© Gerhard Brunthaler
1
Python for Lectures 12.1 Conversion of a Matlab file into a Python file.............................................................34 12.2 Figures Figures with matplotli matplotlib.................. b................................... .................................. .................................. .................................. ..........................34 .........34 12.3 Nonb Nonblocki locking ng Plots with with Matplotli Matplotlib b ................................ ................................................. .................................. .............................35 ............35 12.4 Matplotl Matplotlib's ib's OO Class Class Library Library ................................ .................................................. ................................... ..................................36 .................36 12.5 File and Directory handling/manipulation...................................................................36 12.6 Measurin Measuring g raw speed speed ................................. .................................................. .................................. .................................. .............................37 ............37 12.7 Video Video processin processing.................... g..................................... .................................. .................................. ................................... ................................37 ..............37 13 Other stuff................. stuff .................................. .................................. .................................. .................................. ................................... ................................39 ..............39 13.1 Unordered Unordered Stuff............................... Stuff................................................ .................................. .................................. .................................. ....................... ......39 39 14 Version Version changes changes ................................. .................................................. .................................. .................................. .................................. ....................... ......41 41
Foreword I have heard from several students that it is difficult to find a programming language which which can be used to solve exercises in physics, physics, for plotting scientific diagrams diagrams and for simulations. It seems to me that Python is very suitable to perform this tasks. An important feature is also that it is a GPL -compatible language (GPL = General Public License). So it can be used free of charge by students and any other persons in contrast to packages like Matlab, which I use a lot for my scientific work at the university institute. Python is a general purpose programming language. It is interpreted and dynamically typed and is very suited for f or interactive work and quick prototyping, while being powerful enough to write large applications in.
Note: On my computer, I use the English version of the Windows Windows operating system as well as all software in English versions if they are available. I am also used to describe and comment my scientific work and software stuff in simple English language and thus I go on in that t hat way here.
-2-
Python for Lectures
1
Installation
I suggest to use Enthought Python for scientific scientific programming work. Enthought is a company which offers a programming programming package around python. python. It is free for personal and academic use, use, but companies have to pay for it. Before that, I installed pure Python Python and then added packages packages to it. It took me days days to get a useful system up. The Enthought Enthought package works works at once and and you can indeed start to work on Python itself. Download from end of page: http://www.enthought.com/products/getepd.php , free use for academic and students! Installation on Windows Windows XP. I create usually a system restore point before before larger installations installations or changes, in order to be able to set my computer back to a state before that procedure. Click installer, choose installation folder …, choose to put icons onto desktop and install!
2
First Trials
After installation, installa tion, there are two new Icons on the tesktop: PyLab and Mayavi. PyLab opens the an interactive shell “IPython”, whereas Mayavi is an 3D scientific data visualization and plotting package. First we have a look on PyLab.
2.1
Pylab and IPython
Clicking on “Pylab” opens up a black command window and after 30 seconds or a minute (be patient) some text appears: ********************************************************* ***************************************** ***************************** ************* Welcome to IPython. I will try to create a personal configuration directory where you can customize many aspects of IPython's functionality in: C:\Documents and Settings\bru\_ipython Settings\bru\_ipython Initializing from configuration: C:\Prog\Python26EPD\lib\site-packages\IPython\ C:\Prog\Python26EPD\lib\site-packages\IPython\ serConfig Successful installation! Please read the sections 'Initial Configuration' and 'Quick Tips' in the IPython manual (there are both HTML and PDF versions supplied with the distribution) to make sure that your system environment is properly configured to take advantage of IPython's features. Important note: the configuration system has changed! The old system is still in place, but its setting may be partly overridden by the settings in "~/.ipython/ipy_user_conf.py" "~/.ipython/ipy_user_c onf.py" config file. Please take a look at the file if some of the new settings bother you. Please press
to start IPython.
-3-
Python for Lectures IPython is an interactive shell for the Python programming language. After pressing return in the command window, window, one gets g ets further: ********************************************************* ***************************************** ***************************** ************* Enthought Python Distribution -- http://www.enthought.com Python 2.6.6 |EPD 6.3-2 (32-bit)| (r266:84292, Sep 20 2010, 11:26:16) [MSC v.150 0 32 bit (Intel)] Type "copyright", "credits" or "license" for more information. IPython 0.10.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. Welcome to pylab, a matplotlib-based Python environment. environment. For more information, type 'help(pylab)'. In [1]:
type: “exit()” in order to finish f inish this IPython shell. The interactive shell IPython is a component of the SciPy package. For more info info see e.g. http://en.wikipedia.org/wiki/IPytho http://en.wikipedia.org/wiki/IPython n or see a short short video on the use of it http://showmedo.com/videotutorials http://showmedo.com/v ideotutorials/series?name=P /series?name=PythonIPythonSeri ythonIPythonSeries es . IPython can be used to perform simple one-line statements and calculations, but also to t o edit and run large Python programs.
2.2
IDLE
IDLE is the simplest integrated development development environment (IDE) for Python, which is bundled in each release of the programming programming language since version 2.3. In the Windows W indows “Programs” folder in the start menue, the new folder “Enthought” has appeared. Inside there are icons for “Documentation”, “Documentation”, “Python Manuals”, Manuals”, “IDLE” and others. Start IDLE by double-clicking double-clicking the corresponding icon. icon. You should get a window (with (with white background color?) with the following text: Python 2.6.6 |EPD 6.3-2 (32-bit)| (r266:84292, Sep 20 2010, 11:26:16) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. ************************************* ***************************************************** *************************** *********** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection connection is not visible visible on any external external interface and no data is sent to or received from the Internet. ************************************* ***************************************************** *************************** *********** IDLE 2.6.6 >>>
As a first step in the Interactive Shell type
-4-
Python for Lectures >>> 1 + 1
by pressing return at the end of the t he line, you should get 2
and further >>> x = 1 >>> y = 2 >>> x + y 3
As you see, IDLE can be used for simple calculations, but also for more advanced work. Close IDLE by entering “exit()” into the next line, or by clicking into the upper right closing button of the active Windows window. I personally find IDLE and IPython not very convenient environments to work with, so we try another one included in the Enthought distribution package.
2.3
SciTe Editor
SciTE or SCIntilla based Text Editor is a cross-platform text editor http://en.wikipedia.org/wiki/SciTE . In the “Documentatio “ Documentation” n” folder in the t he Windows “Programs” “Enthought” folder you find at position 88 the link http://www.scintilla.org/SciTEDoc.html to the SciTe documentation for detailed information! The icon for the SciTe editor is also inside the “Enthought” folder in the Windows “Programs” “Programs” folder in the start menue. Start SciTe and and type:
print “Hi” into the first line of the editor, save the file as “printhi.py” to a convenient folder; the file extension is important in order that the editor recognizes the text as Python code and performs a corresponding text highlighting highlighting after saving. Note: When working on a Linux system, it is usually necessary to include: #!/usr/bin/python into the first line, so that the Python interpreter is addressed. addressed. It is not clear to me, whether this is also necessary when working with the SciTe editor. Press F5-key (or go to Menue “Tools” – “Go”) to execute the python code! An attached window opens up and prints the output as:
>pythonw -u "printhi.py" Hi >Exit code: 0 Congratulations, Congratulations, you have run your first Python script! “Exit code: 0” means that there was no error.
-5-
Python for Lectures
3
Session 2
3.1
Short programs
Here is the next small script file f ile (program) to demonstrate a little more on the “print” statement and on commenting with the hash sign “#”. # # # #
prog_01.py calculation and window closing everything after the hash character '#' is considered as comment [email protected] Gerhard.Brunthaler@j ku.at
# printing into output window: print "Hi" print 1 + 2 print " " # create empty line # do not type any space in front of line - this would cause an error! # if one works within an Editor, IDE or so, the output window stays open # and you can see the result. result. But if you run the python python script by double# clicking the file in the folder, under Windows the python interpreter # (if correctly installed) may be called directly to perform the script. # In that case it closes the output window immediately after finishing # and you cannot read the result. # In such a case you can add e.g. an 'raw_input' command in order # that the python interpreter waits for an input before finishing. # For activation, remove the two leading hash-characters from next line: ##raw_input("Press return to close this window ... ")
I could also send the script files separately, but I think that this would have a smaller learning effect in that t hat the participants are probably less active. # prog_02.py # simple calculation and formated output # Gerhard.Brunthaler@j [email protected] ku.at a = 2 b = 3 c = a*b print c print " " # empty line print "a*b = ", c print " " print "formated output:" print "a = %g, %g, b = %g gives c = a*b = %g" % (a,b,c) (a,b,c) # for %-formating see "5.6.2. String Formatting Operations" in # http://docs.python.o http://docs.python.org/library/stdtypes rg/library/stdtypes.html#typesseq-s .html#typesseq-strings trings
String Formatting Operations: see 5.6.2. in: http://docs.python.org/library/stdtypes.html# http://docs.python.org/l ibrary/stdtypes.html#typesseq-strings typesseq-strings Conversion Meaning 'd' Signed integer decimal. 'i' Signed integer decimal.
Notes
-6-
Python for Lectures 'o' 'u' 'x' 'X' 'e' 'E' 'f' 'F' 'g' 'G' 'c' 'r' 's' '%' For
Signed octal value. (1) Obsolete type – it is identical to 'd'. (7) Signed hexadecimal hexadecimal (lowercase). (lowercase). (2) Signed hexadecimal hexadecimal (uppercase). (2) Floating point exponential exponential format (lowercase). (lowercase). (3) Floating point exponential exponential format (uppercase). (uppercase). (3) Floating point decimal format. (3) Floating point decimal format. (3) Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. otherwise. (4) Floating point format. Uses uppercase exponential exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. otherwise. (4) Single character (accepts integer or single character string). String (converts any Python object using repr()). (5) String (converts any Python object using str()). (6) No argument is converted, results in a '%' character in the result. Notes see explanation in above given link.
next program example: # prog_03.py # more simple calculations # Gerhard.Brunthaler@j [email protected] ku.at # printing into output window: print "Simple calculations:" print " 2/3 = ", 2/3, "- here an integer division has been performed!" print " 2.0/3 = ", 2.0/3, "- here a floating point operation has been performed!" print " (1+2j)*(2+3j) =", (1+2j)*(2+3j), "- complex numbers" # but functions like sin(x), sqrt(x) are not defined in the base module # you have to import modules! # the next statement leads to an error message: sin(0.5) # also the constant 'pi' is not yet defined
In order to work with mathematical function function one needs to add a module to the script. This is shown in the next example. # prog_04.py # more calculations, module import # Gerhard.Brunthaler@j [email protected] ku.at # importing a mathematic module: from pylab import * # include function e.g. as sin(0) print print print print print
"Functions from mudule:" " sqrt(2) = ", sqrt(2) " pi = ", pi " sin(pi/2) = ", sin(pi/2) " exp(1j*pi/2) = ", exp(1j*pi/2), " - equal to 0+1j within rounding errors"
With such modules Python programming gets a little more messy, but only with those one can do powerful work.
-7-
Python for Lectures
3.2
Tutorials
For those who can’t wait for any longer, just go g o by yourself for one of the many tutorials and introductions on Python like http://docs.python.org/tutorial/introduction.html or http://diveintopython.org/ . A good introduction into Python can be found in the online tutorial http://www.networktheory.co.uk/docs/pytut/index.html by Guido van Rossum, the creator of the Python language himself. This tutorial can also also be ordered as a book.
4 4.1
Modules, Plotting, etc. Modules
Working with modules – see next example. #!/usr/bin/python # prog_05_module_1.py # works with Python 2.6.2 # the first line above is necessary if working on a Linux system! import pylab # include function e.g. as pylab.sin(0) # module pylab is a part of module 'matplotlib' print "sin(pi/6) =", pylab.sin(pylab.pi/6) pylab.sin(pylab.pi/6) # work with complex numbers - be careful: print "square-root of -2 =", pylab.sqrt(-2) # gives "not a number"-output! print "square-root of -2+0j =", pylab.sqrt(-2+0j) # gives complex number
With import pylab
the module named ‘pylab’ ‘pylab’ gets linked to the program. It includes many mathematical mathematical functions. If a module is included in that way, all functions (“subroutines”) (“subroutines”) of that module have to be called by giving the module name together together with the function name, e.g. ‘pylab.sin’ ‘pylab.sin’ or ‘pylab.pi’. ‘pylab.pi’. This is cumbersome, but ensures that even if two functions inside two different modules have the same name, they will not be mixed up. Nevertheless, in most cases one likes likes to have a compact naming for functions. functions. In that sense it is possible to use from pylab import *
which means that all ffunction unction (because of ‘*’) from the module are imported directly with their names without any prename, prename, e.g. directly ‘sin’ or ‘pi’. See the following example. # prog_06_module_2.py from pylab import *
# include function e.g. as sin(0)
print "sin(pi/6) =", sin(pi/6) # work with complex numbers - be careful: print "square-root of -2+0j =", sqrt(-2+0j) # gives complex number print "exp(1) =", exp(1)
-8-
Python for Lectures Instead of importing all functions from the module, one can import a certain set by listing them up, as in: # prog_06b_module_2b.p prog_06b_module_2b.py y from pylab import pi,sin,exp
# include function e.g. as sin(0)
print "sin(pi/6) =", sin(pi/6) print "exp(1) =", exp(1)
A compromise between the options above is to use the import statement as in: # prog_07_module_3.py import pylab as p
# include function e.g. as p.sin(0)
pi = p.pi # here a new name is assigned to p.pi print "sin(pi/6) =", p.sin(pi/6) print "exp(1) =", p.exp(1)
Here, to all pylab functions the prename ‘p’ is assigned to have a short form, but still to reflect from which module the functions functions are coming. With that form of import, “one and the same” function can be important from different modules, like in: # prog_07b_module_3b.p prog_07b_module_3b.py y import pylab as p import numpy.lib.scimath numpy.lib.scimath as s
# include function e.g. as p.sqrt (0) # include function e.g. as s.sqrt (0)
# work with complex numbers - be careful: print "pylab square-root of -2 =", p.sqrt(-2) print "scimath square-root of -2 =", s.sqrt(-2)
# nan (not a number) # complex number
The last example shows that the square root function for negative numbers behaves behaves differently in different modules, as these modules were written by different persons or groups and for different purposes. This has disadvantages and and makes a careful handling handling of functions from different modules very important.
4.2
Plotting
There are different possibilities possibilities for plotting under Python. Python. The most promising one is presently presently “Matplotlib”. It allows calculations calculations and plotting in a very similar similar way as as with Matlab. Matlab. If you work with the Enthought Python package, matplotlib should should be ready for use. If not, you have to download matplotlib from http://matplotlib.sourceforge.net/ http://matplotlib.sourceforge.net/ and and install it according to the documentation there (I did this some time ago, but for now I work with the Enthought package). Let’s start with a simple program to demonstrate demonstrate the simplicity of plotting: # prog_09_plotting_1.p prog_09_plotting_1.py y # Python version and matplotlib version have to match in order to work properly! import matplotlib matplotlib.use('TkAgg') matplotlib.use('TkAgg' ) # define backend before pylab import! from pylab import *
# include function e.g. as sin(0)
x = linspace(0,2*pi,50) # defines a number array from 0 to 2*pi with 50 points y = sin(x) # calculate the sin for all individual elements of the array
-9-
Python for Lectures figure(1) # num=1 plot(x,y) xlabel('x') ylabel('sin(x)') title('Example Plot 1') show()
A separate window with the plot of a sin-curve should open up if everything is installed correctly. If that is the case, please stare for a while onto the image in order order to appreciate your success! It should look the image, which is partly shown below.
Fig.: Cut-out of screenshot of image produced by “prog_09_plotting_1.py”. In order to see the full image, run the program by yourself!
If the separate window has not opened up, don’t worry and solve the problems by googleing for the matplotlib installation problems and start a discussion in our work group! In order that you are able to continue your work with Python, you have first to close the separate plot window (at least on my Win-XP installation with Enthought Python and SciTe). Now let’s consider some details of the modules used above. Here some sentences from http://en.wikipedia.org/wiki/Matplotlib http://en.wikipedia.org/wiki/Matplotlib:: Matplotlib is a plotting library for f or the Python programming language and its NumPy numerical mathematics extension. It provides an object-oriented API which allows plots to be embedded into applications using generic GUI (i.e. graphical user interface) toolkits, like wxPython, Qt, or GTK. There is also a procedural "pylab" "pylab" interface based on a state machine (like OpenGL), designed to closely resemble that of MATLAB. Matplotlib is written and maintained primarily by John Hunter, and is distributed under a BSDstyle license. The package pylab combines pyplot with numpy into a single namespace. Comparison with MATLAB The pylab interface makes Matplotlib easy to learn for experienced MATLAB users, resulting in a viable alternative for many MATLAB users as a teaching tool for numerical mathematics and signal processing. Some of the advantages of Python+NumPy+Matplotlib over MATLAB include: * Based on Python, a full-featured modern object-oriented programming language suitable for large-scale software development * Suitable for fast scripting, including CGI scripts * Free, open source, no license servers * Native SVG support (i.e. http://en.wikipedia.org/wiki/SVG http://en.wikipedia.org/wiki/SVG:: SVG means “Scalable Vector Graphics”.) Gr aphics”.) http://en.wikipedia.org/wiki/NumPy:: http://en.wikipedia.org/wiki/NumPy NumPy is an extension to the Python programming language, adding support for large, multidimensional arrays and matrices, along with a large library of high-level mathematical mathematical functions to operate on these arrays. The ancestor of NumPy, Numeric, was originally created by Jim Hugunin. NumPy is open source and has many contributors.
- 10 -
Python for Lectures Since the standard Python implementation is an interpreter, mathematical algorithms often run much slower than compiled equivalents, such as those written in C. NumPy addresses this problem for many numerical algorithms by providing multidimensional arrays and lots of functions and operators that operate on arrays. Thus any algorithm that can be expressed primarily as operations on arrays and matrices can run almost as fast as the equivalent C code. This means that “pyplot” is a module which includes the plotting capabilities, “numpy” adds the capability to work with number number arrays and matrices. Normally numpy is is a separate module. Pylab is a part of matplotlib and combines pyplot and numpy into one module with a common namespace. Normally each module has its separate namespace, i.e. it has its own memory for function functi on names and variables, which can not be seen directly by the calling program. But with modulename.function modulename.function (like pylab.sqrt) one has access from the calling program to the module namespace. In that Pylab combines pyplot and numpy, the two modules work hand in hand in a common namespace without any barrier in between. Using matplotlib in a python shell: http://matplotlib.sourceforge.net/users/shell.html: By default, matplotlib defers drawing until the end of the script because drawing can be an expensive operation, and you may not want to update the plot every time a single property is changed, only once after all the properties have changed. But when working from the python shell, you usually do want to update the plot with every command, eg, after changing the xlabel(), or the marker style of a line. While this is simple in concept, in practice it can be tricky, because matplotlib is a graphical user interface application under the hood, and there are some tricks to make the t he applications work right in a python shell. … What is a backend: http://matplotlib http://matplotlib.sourceforge.net/fa .sourceforge.net/faq/installing_faq q/installing_faq.html#what-is-a-b .html#what-is-a-backend ackend:: A lot of documentation documentation on the website and in the mailing lists refers to the “backend “backend”” and many new users are confused by this term. matplotlib targets many different use cases and output formats. Some people use matplotlib interactively from the python shell and have plotting windows pop up when they type commands. Some people embed matplotlib into graphical user interfaces like wxpython or pygtk to t o build rich applications. Others use matplotlib in batch scripts to generate postscript images from some numerical simulations, simulations, and still others in web application servers to dynamically serve up graphs. To support all of these use cases, matplotlib can target different outputs, and each of these t hese capabililities capabililities is called a backend; the “frontend” is the user facing code, ie the plotting code, whereas the “backend” does all the dirty work behind the scenes to make the figure. There are two types of backends: user interface backends (for use in pygtk, wxpython, tkinter, qt, macosx, or fltk) and hardcopy backends to make image files (PNG, SVG, PDF, PS). There are a two primary ways to configure your backend. backend. One is to set the backend parameter in you matplotlibrc file (see Customizing Customizing matplotlib): backend : WXAgg
# use wxpython with antigrain (agg) rendering
The other is to use the t he matplotlib use() directive: import matplotlib matplotlib.use('PS')
# generate postscript output by default
- 11 -
Python for Lectures
If you use the use directive, this must be done before importing matplotlib.pyplot matplotlib.pyplot or matplotlib.pylab. If you are unsure what to do, and just want to get cranking, just set your backend to TkAgg. This will do the right thing for 95% of the users. It gives you the option of running your scripts in batch or working interactively from the python shell, with the least amount of hassles, and is smart enough to do the right thing when you ask for postscript, or pdf, or other image formats. … Here is a somewhat more advanced version of the plotting example from above: # prog_10_plotting_2.p prog_10_plotting_2.py y # Python version and matplotlib version have to match in order to work properly! import matplotlib matplotlib.use('TkAgg') matplotlib.use('TkAgg' ) # define backend before pylab import! # works also if the two lines above are absent!! - some default settings then?? from pylab import *
# include function e.g. as sin(0)
help(linspace) # gives help on 'linspace' x = linspace(0,2*pi,50) # defines a number array from 0 to 2*pi with 50 points y = sin(x) # calculate the sin for all individual elements of the array figure(1,(10,7.5),90) # num=1, figsize=(8,6), dpi=90 clf(), hold(True) plt1 = plot(x,y,'*-', linewidth=3) # linewidth = 2 setp(plt1, 'color', 'r', 'linewidth', 2.0) # set plot attributes like matlab setp(plt1, 'markersize', 10, 'markerfacecolor', 'g', 'markeredgecolor', 'r') xlabel('x') ylabel('sin(x)') title('Example Plot 2') show()
Fig.: Cut-out of image produced by “prog_10_plotting_2.py”. In order to see the full image, run … !
Example program distributed separately The program “plotting_with_matplotlib_2.py” “plotting_with_matplotlib_2.py” has been distributed within our online discussion group and shows many more plotting possibilities!
Fig.: Small cut-out of image produced by “plotting_with_matplotlib_2.py” and saved into pdf-file. In order to see … !
See also the Webpage “NumPy for MATLAB users” in order to learn more on NumPy possiblities: http://mathesaurus. http://mathesaurus.sourceforge.net/ sourceforge.net/matlab-numpy.html matlab-numpy.html Pyplot tutorial: http://matplotli http://matplotlib.sourceforge.net/u b.sourceforge.net/users/pyplot_tutori sers/pyplot_tutorial.html al.html - 12 -
Python for Lectures Figure Customizing: Customizing: http://matplotlib.sou http://matplotlib.sourceforge.net/users/ rceforge.net/users/customizing.ht customizing.html ml ### FIGURE # See http://matplotlib.sourceforge.net/a http://matplotlib.sourceforge.net/api/figure_api.ht pi/figure_api.html#matplotlib.figure.F ml#matplotlib.figure.Figure igure #figure.figsize : 8, 6 # figure size in inches #figure.dpi : 80 # figure dots per inch #figure.facecolor : 0.75 # figure facecolor; 0.75 is scalar gray #figure.edgecolor : white # figure edgecolor
Plot figure with white background and fill area with plot (no space for axes, etc.), switch axis off (in order not to see any ticks showing in from the borders) and write figtext instead of title: fig1 = figure(1, figure(1, figsize=(10.0, 10.0)) # figure with with size defined defined in inch fig1.set_facecolor('w') fig1.subplots_adjust(left=0.0, fig1.subplots_adjust(l eft=0.0, bottom=0.0, right=1.0, top=1.0) ... axis('off'); grid('off') figtext(0.5, 0.04, t_str, horizontalalignment='center', horizontalalignment='center', \ verticalalignment='center', verticalalignment='cen ter', fontsize=16)
Set Figure's position: http://osdir.com/ml http://osdir.com/ml/python.matplotlib /python.matplotlib.general/2004 .general/2004-12/msg00114 -12/msg00114.html .html http://osdir.com/ml/python.matplotlib.gen http://osdir.com/ml/pytho n.matplotlib.general/2004-12/ms eral/2004-12/msg00115.html g00115.html http://osdir.com/ml/python.matplotlib.gen http://osdir.com/ml/pytho n.matplotlib.general/2004-12/ms eral/2004-12/msg00116.html g00116.html For WX backend, in order to set figure position on screen 500 pixel from left and 200 pixel from top, use: manager = get_current_fig_mana get_current_fig_manager() ger() manager.window.SetPosition((500,200))
In TkAgg it works like this: get_current_fig_manager().window.wm_geome get_current_fig_manage r().window.wm_geometry("+200+300") try("+200+300")
To set the window position to X=200, Y=300. Create figures for use in article publishing: http://seehuhn.de/blog/59 (see App. 12.2) How to generate publishable quality images: http://eccentric.mae.cornell.edu/~vh63 http://eccentric.mae.corn ell.edu/~vh63/matplotlib.html /matplotlib.html
5
Statements, control flow, indentation, advantage
See e.g.: Python_(prog Python_(programming_lan ramming_language)#Statemen guage)#Statements_and_control ts_and_control_flow _flow A Python script consists consists of a series of statements statements and comments. Each statement is a command that is recognized recognized by the Python interpreter and executed. executed. Statements are usually usually one line long, though there are a few ways to make them extend over several lines (http://www.dal http://www.dalkescientific.com/wri kescientific.com/writings/NBN/pytho tings/NBN/python_intro/statements.h n_intro/statements.html tml). ).
Python's statements include (among others): •
•
•
The if statement, which conditionally executes a block of code, along with else and elif (a contraction of else-if). The for statement, which iterates over an iterable object, capturing each element to a local variable for use by the t he attached block. The while statement, which executes a block of code as long as its condition is true.
- 13 -
Python for Lectures
Such statements are marked by certain keywords. keywords. It turns that Python itself is a remarkably simple programming language – it has a very limited number of keywords. Python has twenty-eight keywords:
and
continue
else
for
import
not
raise
assert
def
except
from
in
or
return
break
del
exec
global
is
pass
try
class
elif
finally
if
lambda
print
while
5.1
for loop
The ‘for’ statement controls the program program flow in the following following example. The statement block inside the ‘for’ loop is executed until all given values values in the list have been taken. The important thing is that the block inside the ‘for’ loop is not marked by braces or an ‘end’ statement, but it is rather distinguished by indentation. indentation. # prog_11_control_flow prog_11_control_flow_1.py _1.py # Gerhard.Brunthaler@j [email protected] ku.at n_sum = 0 for n in [1, 2, 3, 4, 5]: n_sum = n_sum + n
# numbers given in list
print " last number: ", n # last value of n after leaving loop print " sum from from 1 to %g: %g \n" % (n, n_sum) n_sum) # sum of all
Please run the programs by yourself!
5.2
Indentation
Python uses whitespace indentation, indentation, rather than curly braces or keywords, to delimit blocks. An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block. (see: Myths about Indentation I ndentation:: http://www.secn http://www.secnetix.de/olli/Python etix.de/olli/Python/block_indentation /block_indentation.hawk .hawk )
5.3
if – elif – else
An example with the ‘if-elif-else’ statement: # prog_11b_control_flo prog_11b_control_flow_2.py w_2.py # the 'if-elif-else statement' n = int(raw_input("Please int(raw_input("Please enter a positive or negative integer: ")) if n<0: print " %s is smaller than zero" % n elif n==0: print " %s is equal to zero" % n else: print " %s is larger than zero" % n
- 14 -
Python for Lectures
5.4
while
The while statement: # prog_11c_control_flo prog_11c_control_flow_3.py w_3.py # create table of square numbers with 'while'-loop x = 1.0 while x < 6.0: print x, '\t', x**2 # the string '\t' represents a tab character x = x + 1.0
5.5
try
Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally unconditionally fatal. They can be handled by by the “try”-command. “try”-command. See the following example. example. def is_number(s): # function defintion try: float(s) return True except ValueError: return False
Although there is a better way to test whether a string can be converted into a number or not. Use the isdigit() function for string objects. a = "03523" a.isdigit()
True When to catch exceptions? exceptions? (http://www.python.or http://www.python.org/doc/essays/ppt/hp g/doc/essays/ppt/hp-training/sld02 -training/sld028.htm 8.htm ) a) When there's an alternative option try: f = open(".startup") except IOError: f = None # No startup file; use defaults
b) To exit with nice error message try: f = open("data") except IOError, msg: print "I/O Error:", msg; sys.exit(1)
Hint Sometimes it is necessary to put an already written program block into a loop (or generally speaking into a control-flow statement) and thus to indent the whole block at once. once. This can be done by hand, line by line. But it is desirable desirable that that a text editor editor supports that that duty directly (this is indeed a matter of the t he text editor or the IDE and is not an ability of the Python interpreter itself, although it requires it).
- 15 -
Python for Lectures In SciTe the task can be handled by highlighting highlighting the block of lines to be indented and just pressing the Tab-key on the keyboard (at least in windows version SciTe 1.74).
5.6
Advantages of Python:
Here is some motivation, why to go on and learn Python! Why python? http://www http://www.linuxjournal.com/a .linuxjournal.com/article/3882 rticle/3882 Python as a First Language: http://mcsp.wartbur http://mcsp.wartburg.edu/zelle/py g.edu/zelle/python/python-first.html thon/python-first.html
6
Numbers, Strings, Boolean
So far we have worked with Python without knowing a lot about the available available datatypes.
6.1
Everything is an Object
In Python everything is an object, even a simple letter of number, but you will not notice it until you have quite some experience experience on it. Probably one isn’t isn’t so surprised to hear that a function is an object. Nearly all of that objects have built-in attributes and methods – more on that later.
6.2
Number formats
Python supports 4 types of Numbers, Numbers, the int, the long, the float and the complex. You don’t have to specify what type of variable you want; Python does that automatically. •
•
•
•
Int: This is the basic integer type in python, it is equivalent to the hardware 'c long' for the platform you are using. Long: This is a integer number that's length is non-limited. In python 2.2 and later, Ints are automatically turned into long ints when they overflow. Float: This is a binary floating point number. Longs and Ints are automatically converted to floats when a float is used in an expression, and with the true-division true-division // operator. Complex: This is a complex number consisting of two floats. Complex literals are written as a + bj where a and b are floating-point numbers denoting the real and imaginary parts respectively.
In general, the number types are automatically 'up cast' in this order: Int
→
Long
→
Float
→
Complex. The farther to the right you go, the higher the precedence. precedence.
See some examples in the IDLE environment (for IDLE see chap. 2.2). >>> x = 5 >>> type(x) >>> x = 187687654564658970978909869576453 187687654564658970978909869576453 >>> type(x) >>> x = 1.34763 >>> type(x) >>> x = 5 + 2j
- 16 -
Python for Lectures >>> type(x)
However, some expressions may be confusing since in the t he current version of python, using the / operator on two integers will return another integer, using floor division. For example, 5/2 will give you 2. You have to specify one of the operands as a float to get true division, e.g. 5/2. or 5./2 (the dot specifies you want to work with float) to have 2.5. This behavior is declined and will disappear in a future python release. >>> 2 >>> 2.5 >>> 2.5 >>> 0.5
5/2 5/2. 5./2 float(1)/2
# can also be used to give an explicit explicit hint to the pitfall
See also: http://en.wikiboo http://en.wikibooks.org/wiki/Python ks.org/wiki/Python_Programming _Programming/Numbers /Numbers Numbers can also be converted into a certain format by use of built-in constructors int(), long(), float(), and complex(), see e.g.: >>> a = 1 >>> float(a) 1.0 >>> type(a)
float also accepts the strings “nan” and “inf” with an optional prefix “+” or “-” for Not a Number (NaN) and positive or negative infinity. >>> a, b = float('nan'), float('-inf') float('-inf') >>> print a, b nan -inf
Some more basic mathematical functions along with the number types. >>> 8 >>> 8 >>> >>> >>> 1.0 >>> 2.0
pow(2,3)
# 2^3
2**3
# 2^3
x = 1.234 import math math.floor(x)
# math module is needed
math.ceil(x)
Unfortunately it is a little bit complicated to get the fractional fr actional part of a floating point number. >>> math.modf(x,1) (0.23399999999999999, 1.0) >>> fx, fi = math.modf(x) >>> fx 0.23399999999999999
# # # #
modulo function with rest results in a tuple get both parts separately fractional part alone
http://docs.python.org/library/stdtypes.html# http://docs.python.org/l ibrary/stdtypes.html#typesnumeric typesnumeric
- 17 -
Python for Lectures
6.3
Strings
In order to execute some examples again start IDLE (see chap. 2.2). IDLE 2.6.6 >>> 'text' 'text' >>> "this is a string" 'this is a string' >>> >>>
Single quotes (‘) and double quotes (“) define strings the same way. The one sort can contain the other without being interpreted or changed. >>> "Python's" "Python's" >>> 'This is in "double quotes"' 'This is in "double quotes"'
A raw string str ing (r’text’) is a special string, where backslash commands are not interpreted and executed. It is e.g. useful to contain DOS-like path definitons definitons (like r’c:\dir\file’). r’c:\dir\file’). >>> print r'c:\dir\file' c:\dir\file >>> print 'text with \n command sequence' text with command sequence
In contrast, in a string defined like 'text with \n command sequence' contains the backslash character and is interpreted as new line command. For more information see e.g.: http://docs.python.org/tutorial/introductio http://docs.python.org/tu torial/introduction.html#strings n.html#strings
6.4
Truth Value, Boolean Operations
The type object for Boolean type is named bool; the constructor for it takes any Python value and converts it to True or False. >>> bool(1) True >>> bool(0) False >>> bool([]) False >>> bool( (1,) ) True
Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false: •
None
•
False
•
zero of any numeric type, for example, 0, 0L, 0.0, 0j.
•
any empty sequence, for example, '', (), [].
•
any empty mapping, for example, {}.
- 18 -
Python for Lectures •
instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool value False. [1]
All other values are considered true — so objects of many types are always true. Operations and built-in functions that have a Boolean result always always return 0 or False for f or false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.) Use any Boolean expression in an if or while statement: if bool(1): print "yes,it is!" b = bool(0) print b type(b)
These are the Boolean operations, operations, ordered by ascending priority: Operation
Result
x or y
if x is false, then y, else x
x and y
if x is false, then x, else y
not x
if x is false, then True, else False
Comparison operations are supported by all objects. They all have the same priority (which is higher than that of the Boolean operations). Comparisons can be chained arbitrarily; for example, x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false). This table summarizes the comparison operations: Operation
Meaning
<
strictly less than
<=
less than or equal
>
strictly greater than
>=
greater than or equal
==
equal
!=
not equal
is
object identity
is not
negated object identity
For more details see: http://docs.python.org/library/stdtypes.html# http://docs.python.org/l ibrary/stdtypes.html#typesnumeric typesnumeric http://docs.python.org/release/2.5.2/lib/bool http://docs.python.org/rel ease/2.5.2/lib/boolean.html ean.html
6.5
Declaring variables
Python has local and global variables like most other languages, but it has no explicit variable declarations. Variables spring into existence by being assigned a value, and they are automatically destroyed when they go out of scope. >>> x
# gives an error as it was not defined yet
- 19 -
Python for Lectures Traceback (most recent call last): File "", line 1, in x NameError: name 'x' is not defined >>> >>> x = 1 # the variable x is defined on on the fly, by it’s first first use >>> x 1 >>> price_of_fine_china_tea_no_1 = 2.50 # this variable name is allowed
You will thank Python for this simplicity one day. Programmers generally generally choose names for their variables that are meaningful – they document what the variable is used for. Variable names can be arbitrarily long. They can contain both letters and numbers, but they have to begin with a letter. Although it is legal to use uppercase letters, by convention we don’t. If you do, remember that case matters. Bruce and bruce are different variables. The underscore character ( _) can appear in a name. It is often used in names with multiple words, such as my_name or price_of_fine_china_tea. If you give a variable an illegal name, you get a syntax error: >>> 76trombones = "big parade" SyntaxError: invalid syntax >>> more$ = 1000000 SyntaxError: invalid syntax >>> class = "Computer Science 101" SyntaxError: invalid syntax
76trombones is illegal because it does not begin with a letter. more$ is illegal because it contains an illegal character, the dollar sign. But what’s wrong with class? It turns out that class is one of the Python keywords. Keywords define the language’s rules and structure, and they cannot be used as variable names. For more see: http://diveintopython.org/native_d http://diveintopy thon.org/native_data_types/declar ata_types/declaring_variabl ing_variables.html es.html chap 2.3 in http://openbookproject.net//thinkCSpy/ch02.html
7 7.1
Datatypes Lists
A list is something like an array or a set of data stored with one variable name. In order to execute examples we work again with IDLE (see chap. 2.2). IDLE 2.6.6 >>> a = [1, 2, 3, 4] >>> a [1, 2, 3, 4] >>> a[0] 1 >>> a[1] 2 >>>
- 20 -
Python for Lectures “a” is defined here as a list with the four elements elements “1, 2, 3, 4”. The first element is retrieved by “a[0]”. Note that Python starts always counting with number “0” for the first element! This might be somewhat unusual and inconvenient inconvenient for physicists, which are more used to start counting of natural objects objects with “1”. On the other hand, computer scientists scientists work with bytes filled with all combinations combinations from “000” to “111” in the binary system. system. Translated into the decimal system this corresponds to the numbers numbers from 0 to 7. Thus for binary oriented work work it is more obvious to start counting at “0”. With the function “type” you can test the t he type of any variable. >>> type(a)
But a list can handle any object (and that’s everything everything in Python), not just numbers. numbers. Here we create a list of strings: >>> b = ["a", "word", "b", "example"] >>> b[3] 'example' >>> b[1:3] ['word', 'b'] >>> b[1:4] ['word', 'b', 'example']
With “1:3” all elements from the first index, here number 1 (i.e. the second), up to, but not including the last index (up to number 2, which is the third element, because the counting starts at zero). >>> b[-1] 'example'
If the index of a list is negative (i.e. “b[-1]”) counting starts from the end with “-1” corresponding to the last one. Here somewhat more on Python Python counting. The “range” function creates a list list with very much the same notation as the “0:3” retrieving of elements from a list. >>> range(3) [0, 1, 2]
It also starts counting at “0” and ends one before the index index “3” given as argument. Equivalently, Equivalently, the function can be simply seen as producing a list with 3 elements starting at 0. For many more details see: http://diveintopython.org/native_data_types/lists.html http://docs.python.org/tutorial/introduction.html#lists There are three built-in functions that are very useful when used with lists: filter(), map(), and reduce(), see: http://docs.python http://docs.python.org/tutorial/datas .org/tutorial/datastructures.html#functio tructures.html#functional-programmi nal-programming-tools ng-tools
7.2
Tuples
A tuple is an immutable list. It cannot be changed in any way once it is created. Retrieving elements from a tuple is much the same way as it is with lists. >>> b = ("a", "word", "b", "example") >>> b ('a', 'word', 'b', 'example')
- 21 -
Python for Lectures >>> b[1] 'word' >>> >>> type(b)
Tuples are faster than lists. Tuples can be converted converted into lists and vice-versa. For more details see: http://diveintop http://diveintopython.org/native ython.org/native_data_types/tuples _data_types/tuples.html .html
7.3
Dictionaries
Unlike lists, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. >>> tel = {'Alex': 9600, 'Eugen':9605} >>> tel {'Eugen': 9605, 'Alex': 9600} >>> >>> tel['Alex'] 9600 >>> >>> tel['Tom'] = 9604 >>> tel {'Eugen': 9605, 'Alex': 9600, 'Tom': 9604} >>>
It is best to t o think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within (within one dictionary). A pair of braces creates an empty dictionary: dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output. >>> tel.keys() ['Eugen', 'Alex', 'Tom'] >>> tel.values() [9605, 9600, 9604] >>> 'Tom' in tel True >>> del tel['Tom'] >>> 'Tom' in tel False >>>
The keys() method of a dictionary object (i.e. “tel.keys()” in the example above) returns a list of all the keys used in the dictionary, dictionary, in arbitrary order (if you want it sorted, just apply the sorted() function to it). To check whether a single key is in the dictionary, dictionary, use the in keyword. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. For many more details see: http://docs.python.org/tutorial/datastructures http://docs.python.org/tu torial/datastructures.html#diction .html#dictionaries aries http://diveintopython.org/native_d http://diveintopy thon.org/native_data_types/index ata_types/index.html#odbchelpe .html#odbchelper.dict r.dict
- 22 -
Python for Lectures
8
Functions
Functions are very useful in order to execute execute program parts from different positions. positions. The keyword def introduces a function definition.
8.1
One return value
In the next example, a circle is plotted in form of a parametric curve with parameter t being interpreted as time of a particle moving moving along. The velocity vector components components are calculated by the derivatives dx/dt and dy/dt and is plotted along along with the curve. In order to calculate the curve and the derivatives, derivatives, a function containing the definition of the curve is called several times. # prog_12_Curve_05.py # V05 parametric curve x(t), y(t); # t interpreted as time of particle along curve # v(t) derivatives dx/dx, dy/dt # save image in svg-file (scalable vector graphic) import matplotlib from pylab import * nmax = 4*8+1 # number of points along curve curve dt = 0.001 # interval size for derivative calculation om = 2*pi # angular velocity $\omega$ v_scale = 1/(2*pi) # scaling factor for velocity in x,y-space def fx(t,om): # define a function return cos(om*t) def fy(t,om): # define a function return sin(om*t) t = linspace(0,1,nmax); linspace(0,1,nmax); # time array (type is numpy.ndarray) x = fx(t,om) # function calls y = fy(t,om) x1 = fx(t-dt/2,om) x2 = fx(t+dt/2,om) dxodt = (x2-x1)/dt
# function calls
y1 = fy(t-dt/2,om) y2 = fy(t+dt/2,om) dyodt = (y2-y1)/dt
# function calls
# calculate derivative
# calculate derivative
figure(1, figsize=(8.0, figsize=(8.0, 8.0)) # figure with size defined in inch plot(x, y, 'bo-', markersize=6, linewidth=1.5) # plot curve with markers for n in range(0,len(t),4): # plot V(t) for every 4th point plot([x[n],x[n]+v_scale*dxodt[n]], plot([x[n],x[n]+v_scale*dxodt[n]], \ [y[n],y[n]+v_scale*dyodt[n]],'r-', [y[n],y[n]+v_scale*dy odt[n]],'r-', linewidth=1.5) # plot statement is continued on 2nd line above axis('on'); grid('on') title('Parametric curve: circle with velocity vectors') xlabel('x'); ylabel('y')
- 23 -
Python for Lectures savefig('prog_14_Curve_05.png', dpi=100) savefig('prog_14_Curve_05.png', savefig('prog_14_Curve_05.svg', savefig('prog_14_Curve _05.svg', dpi=100) # scalable vector graphic show() # show plot
Execute the program from the SciTe editor or call the saved file from within the IPython shell or from IDLE.
Fig.8.1: Part of resulting figure of “prog_12_Curve_05.py”. The red lines represent velocity velocity vectors.
8.2
No return value
In the function definition of the example above, a value (i.e. the function value) was returned to the calling position. Another possibility possibility is that the function performs some task like printing, printing, without returning anything. anything. See the following example example for printing the Fibonacci series. series. # prog_13_function_Fib prog_13_function_Fibonacci.py onacci.py def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.""" a, b = 0, 1 while a < n: print a, a, b = b, a+b # # Now call the function we just defined: fib(200)
It will print the result 0 1 1 2 3 5 8 13 21 34 55 89 144
For more info see : http://docs.python.or http://docs.python.org/tutorial/control g/tutorial/controlflow.html#definin flow.html#defining-functions g-functions
8.3
Multiple return values
Most often when you have to return multiple arguments you will probably just use something simple like this example. Here we just take advantage of the fact that Python includes built built in Tuples, Lists, and Dictionaries, and return one of these objects to encapsulate the multiple values. ( http://www.pen http://www.penzilla.net/tutorials zilla.net/tutorials/python/functions/ /python/functions/ )) # multiple-returns.py a, b, c = 0, 0, 0 # multiple assignment for shortness shortness def getabc(): a = "Hello" b = "World" c = "!" return a,b,c a,b,c # defines defines a tuple on the fly def gettuple(): a,b,c = 1,2,3 # Notice the similarities between this and getabc? return (a,b,c) # defines a tuple by will
- 24 -
Python for Lectures def getlist(): a,b,c = (3,4),(4,5),(5,6) return [a,b,c] # defines a list
These all work, as amazing as it seems. So multiple assignment is actually quite easy. a,b,c = getabc() d,e,f = gettuple() g,h,i = getlist()
8.4
Default argument values
The most useful form is to specify a default value for one or more arguments. This creates a function that can be called with fewer arguments than it is defined to allow. For example: def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): while True: ok = raw_input(prompt) if ok in ('y', 'ye', 'yes'): return True if ok in ('n', 'no', 'nop', 'nope'): return False retries = retries - 1 if retries < 0: raise IOError('refusenik user') print complaint
This function can be called in several ways: * giving only the mandatory argument: ask_ok('Do you really want to quit?') * giving one of the optional arguments: ask_ok('OK to overwrite the file?', 2) * or even giving all arguments: ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!') This example also introduces the in keyword. This tests whether or not a sequence contains a certain value. The default values are evaluated at the point of function definition in the defining scope, so that i = 5 def f(arg=i): print arg i = 6 f()
will print 5
Important warning: The default value is evaluated only once at the function f unction definition. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes. For example, the following function accumulates the arguments passed to it on subsequent calls: def f(a, L=[]): L.append(a) return L
- 25 -
Python for Lectures print f(1) print f(2) print f(3)
This will print [1] [1, 2] [1, 2, 3]
If you don’t want the default to be shared between subsequent subsequent calls, you can write tthe he function like this instead: def f(a, L=None): if L is None: L = [] L.append(a) return L
More on defining functions: http://docs.python.org/tutorial/control http://docs.python.org/tu torial/controlflow.html#more-on-de flow.html#more-on-defining-function fining-functions s
8.5
Lambda expressions
Lambda expressions are also called anonymous functions. Python supports an interesting interest ing syntax that lets you define one-line one-line mini-functions on the fly. Borrowed from Lisp, these socalled lambda functions can be used anywhere anywhere a function is required. # prog_14_lambda_expre prog_14_lambda_expression.py ssion.py def f(x): return x*2 print f(3) y = lambda x: x*2 print y(3)
The lambda function accomplishes accomplishes the same thing as the normal function above it. Note the abbreviated syntax in the lambda expression: there are no parentheses around the argument list, and the return keyword is missing (it is implied, since the entire function f unction can only be one expression). Also, the function has no name, but it can be called through the variable it is assigned to. (http://diveintopy http://diveintopython.org/power_ thon.org/power_of_introspectio of_introspection/lambda_fun n/lambda_functions.html ctions.html )
8.6
Built-in functions
There are many built-in-functions available in Python (http://docs.python.org/library/functions.html ) There of them are very useful when used with lists: f ilter(), map(), and reduce(). filter(function, sequence) returns a sequence consisting of those items from the sequence for which function(item) is true. If sequence is a string or tuple, the t he result will be of the same type; otherwise, it is always a list. For example, to compute primes up to 25: >>> def f(x): return x % 2 != 0 and x % 3 != 0 ... >>> filter(f, range(2, 25)) [5, 7, 11, 13, 17, 19, 23]
- 26 -
Python for Lectures
map(function, sequence) calls function(item) function(item) for each of the sequence’s items and returns a list of the return values. For example, to compute some cubes: >>> def cube(x): return x*x*x ... >>> map(cube, range(1, 11)) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
More than one sequence may be passed; the function must then have as many arguments as there are sequences and is called with the corresponding item from each sequence (or None if some sequence is shorter than another). For example: >>> >>> ... >>> [0,
seq = range(8) def add(x, y): return x+y map(add, seq, seq) 2, 4, 6, 8, 10, 12, 14]
reduce(function, reduce(function, sequence) returns a single value constructed by calling the binary function function on the first two items of the sequence, then on the result and the next item, and so on. For example, to compute the sum of the numbers 1 through 10: >>> def add(x,y): return x+y ... >>> reduce(add, range(1, 11)) 55
If there’s only one item in the t he sequence, its value is returned; if the sequence is empty, an exception is raised. See also: http://docs.python.or http://docs.python.org/tutorial/datastruc g/tutorial/datastructures.html#functio tures.html#functional-programmi nal-programming-tools ng-tools
9
Important built-in modules
Python owns many built-in built-in modules. In order to use them inside a user program, program, they have to be imported first. The sys module allows access to interpreter specific and PC platform specific informations. The os module is an interface to the computer operating system and enables the call of shell commandos and more. The string module contains a number of functions f unctions to process standard Python strings. The time module provides functions to work with dates and times. The re module provides support to recognize character patterns inside strings and to extract substrings. The math module implements a number of mathematical operations for floating point numbers. The cmath module provides similar functionality for complex numbers. The fileinput module makes it easy to write different kinds of text filters. f ilters. The random module provides a number of different random number generators. Multimedia Modules: Python comes with a small set of modules for dealing with image files and audio files. - 27 -
Python for Lectures For more extensive support of multimedia, multimedia, see Python Imaging Library (PIL) and the Snack Sound Toolkit, among others. See http://docs.python.org/tutorial/stdlib.html and http://effbot.org/librarybook/ Because Python comes with a large amount of modules included, it is often stated as “Batteries included”.
9.1
The sys module
One module that provides insightful insightful information abo about ut Python itself is the sys module. You make use of a module by importing the module and referencing its contents (such as variables, functions, and classes) using dot (.) notation. The sys module contains a variety of variables and functions that reveal interesting details about the current Python interpreter. interpreter. Let's take a look at some of them. The first thing we'll do is import the sys module and ask for sys.platform. Again, we're going to run Python interactively and enter commands at the Python command prompt. The first thing we'll do is import the sys module. Then we'll enter the sys.executable variable, which contains the path to the Python interpreter: If one runs r uns Python interactively and enters commands at the Python command prompt, this looks like >>> import sys >>> sys.executable '/usr/local/bin/python'
We can do the same if we write a small program in the editor and then execute the file. import sys print sys.platform
This prints win32
(or any other computer platform, we are working on) into the output window. The following program gives additional information on the sys module and shows access to different data types. # prog_15_sys_module.p prog_15_sys_module.py y # Gerhard.Brunthaler@j [email protected] ku.at # see also http://www.ibm.com/developerworks/library/l-pyint.ht http://www.ibm.com/developerworks/library/l-pyint.html ml import sys print "computer platform: ", sys.platform # computer platform: win32, linux, ... print "python interpreter: ", sys.executable # python interpreter used print "phython version:", sys.version_info # python version # print sys.argv # print sys.path print "" # empty line print "In order to activate the output window,\ click with mouse mouse into this window here" # this statement statement extends over two lines lines raw_input("and then press return to continue!\n") print sys.modules # this gives a long line containing actually a python dictionary print "the type of sys.modules is: ", type(sys.modules)
- 28 -
Python for Lectures print "" raw_input("Press return to continue!") print "'dictionary.items()' converts the content into a list" L = sys.modules.items() sys.modules.items() print "the list L contains %g items" % len(L) print "printing the first 3 items (each being a tuple itself):" for nn in range(3): print L[nn] ## print whole list #print L ## print all entries of dictionary sys.modules #for entry1, entry2 in sys.modules.items(): #print " %s: %s " % (entry1, entry2)
In order to get the t he name of the currently executed (interpreted) python file: import os,sys py_file = sys.argv[0] # list of system arguments for python interpreter
9.2
The os module
For a systematic overview see: http://docs.python.org/library/os.html A very good introduction in form of examples is given in http://effbot.org/librarybook/os.htm http://effbot.org/librarybook/os.htm.. We look at some of these (somewhat modiefied) examples here. Using the os module to list the files f iles in a directory with os.listdir(path): # prog_16_os_1.py import os for file in os.listdir(""): # list current working directory if path string is empty print file
results in prog_01.py prog_02.py ...
On Windows one can e.g. give the path string like "C:\\temp" to get g et the directory listing of “temp” on “C:”. Note that you have to use “\\” in a string in order to produce a single single “\” in the command output. A single backslash in the string string would here be interpreted as as the tabulator command “\t”. for file in os.listdir("C:\\temp"): os.listdir("C:\\temp"): # note the “\\” under Windows print file
Using the os module to create and remove a directory and to change the working directory: import os # where are we? cwd = os.getcwd() print "1", cwd os.mkdir("test") # create subdir "test"
- 29 -
Python for Lectures # go down os.chdir("test") print "2", os.getcwd() # go back up os.chdir(os.pardir) print "3", os.getcwd() os.rmdir("test") # removes only empty directories # will fail, as subdir has just been removed os.chdir("test") print "2", os.getcwd()
Using the os module to create and remove multiple directory levels and create a text file meanwhile: # # # #
prog_16_os_2b.py http://effbot.org/librarybook/os.htm http://effbot.org/li brarybook/os.htm Using the os module to create and remove multiple directory levels File: os-example-6.py
import os # create several directory levels at once os.makedirs("test/multiple/levels") fp = open("test/multiple/levels/file.txt" open("test/multiple/levels/file.txt", , "w") # create text file fp.write("this text is in file") # write into text file fp.close() # close file access # list files in directory for file in os.listdir("test/multiple/levels"): os.listdir("test/multiple/levels"): print file # remove the file os.remove("test/multiple/levels/file.txt") # and all empty directories above it os.removedirs("test/multiple/levels")
Note that removedirs removes all empty directories along the given path, starting with the t he last directory in the given path name. In contrast, the mkdir and rmdir functions can only handle a single directory level. Using the os module to get information about a file: # prog_16_os_3.py # File: os-example-1.py import os import time file = "prog_01.py" def dump(st): mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st print "- size:", size, "bytes" print "- owner:", uid, gid print "- created:", time.ctime(ctime) print "- last accessed:", time.ctime(atime) time.ctime(atime)
- 30 -
Python for Lectures print "- last modified:", time.ctime(mtime) time.ctime(mtime) print "- mode:", oct(mode) print "- inode/dev:", ino, dev # get stats for a filename stat_info = os.stat(file) print "\nstat_info", file #print stat_info dump(stat_info) print
Using the os module to t o run an operating system command: # prog_16_os_4.py # File: os-example-8.py import os if os.name == "nt": command = "dir" else: command = "ls -l" os.system(command)
Further examples on http://effbot.org/librarybook/os.htm : Using the os module to change a file’s privileges and timestamps Using the os module to run an operating system command Using the os module to start a new process Using the os module to t o run another program (Unix) Using the os module to t o run another program (Windows) Using the os module to run another program in the background (Windows) Using the os module to exit the current process
10 Pitfalls 10.1 The object pitfall Remember? Everything in Python is an object! This may cause troubles in some cases. What is the problem with: >>> >>> >>> >>> [1,
a = [1, 2, 3] b = a a.append(4) # the number 4 is appended to the list print b # b has changed to [1, 2, 3, 4] as well! 2, 3, 4]
At first something on the objects and their identity: >>> a = 3 >>> id(3) 10017632 >>> id(a)
# identity number of object
- 31 -
Python for Lectures 10017632
Here the number “3” is assigned to variable variable “a”. So the number “3” is generated as an object object with a certain (more or less arbitrary?) arbitrary?) integer identity number. number. The variable “a” gets the same identity number as it is equal to number “3”. >>> b = a >>> id(b) 10017632 >>> print b 3
With the assignment “b = a”, variable “b” gets the same id-number id-number as “3” and “a”. And if you print b, you get “3” as an output. >>> a = 2 >>> id(2) 10017644 >>> id(a) 10017644 >>> id(b) 10017632
Now we changed the assignment of “a” to the object “2”, and indeed both have an new, identical id-number. The id-number of “b” from above has not been changed, changed, as it points to the object “3”, which is an immutable object and still exists in the workspace. Everything works as expected. But this is not always the case. Lists are objects that can be changed changed at place (mutable objects) and therefore unwanted effects may occur. See the example from above: >>> a = [1, 2] >>> b = a >>> id(a) 18496008 >>> a.append(3) >>> id(a) 18496008 >>> print b [1, 2, 3]
# the number 3 is appended to the list # the object id is not changed! # b “has changed” to [1, 2, 3] as well!
Here “a” “b” and the list list “[1, 2]” all have the same same object id after the first two lines. Then by a.append, the list is changed under the same object identity. So everything which refers to that object now refers to the changed list – so does the t he variable “b”. For more explanation on that see “Python Objects” at http://effbot.org/zone/python-objects.htm and “Sharing mutable objects” at http://www.python http://www.python.org/doc/essays/ppt/h .org/doc/essays/ppt/hp-training/sld p-training/sld034.htm 034.htm.. To overcome the problem above, instead of “b = a” it is necessary to create a separate object for “b” with the same content. content. This can be achieved achieved in different ways: ways: >>> a = [1, 2] >>> id(a) 18540104 >>> b = a[:] # slicing: alle elements of “a” are taken and assigned to “b” >>> id(b) # “b” is now a new, independent object 13231288 >>> c = list(a) # creates a new list out of “a” >>> id(c) # “c” is a new object 18522440 >>> print c [1, 2] >>>
- 32 -
Python for Lectures >>> import copy # the copy module is built to copy lists and objects, objects, etc. >>> # it possesses different copy methods >>> d = copy.copy(a) >>> d [1, 2] >>> id(d) # “d” has a new id-number, i.e. is assigned to a new list object 18522160 >>> id(a) # id of “a” is still the same as at beginning! 18540104
For more details on object copies see: http://stackoverflow.com/questions/26128 http://stackoverflow.co m/questions/2612802/how-to-clo 02/how-to-clone-a-list-in-py ne-a-list-in-python thon http://www.velocityreviews.com/forums/t68 http://www.veloci tyreviews.com/forums/t684012-python 4012-python-copy-method-al -copy-method-alters-type.html ters-type.html http://docs.python.org/library/copy.html http://openbook.galileocomputin http://openbook.gal ileocomputing.de/python/pytho g.de/python/python_kapitel_17_0 n_kapitel_17_006.htm#mjc96b 06.htm#mjc96b1149114c8 1149114c87b8 7b8 cb9ce33af7a7f2d0 (german language) A tuble is immutable and thus copy does not create a new object: # prog_17_copy_1.py from copy import * f = (1, 2) g = copy(f) print id(f) print id(g) print f is g
# # # # #
a tuple is immutable does not create a new object! identity number of object shows same id-number as f results in ‘True’
10.2 More pitfalls For „10 Python pitfalls” see: http://zephyrfalco http://zephyrfalcon.org/labs/python n.org/labs/python_pitfalls.html _pitfalls.html
11 Introspection Guide to Python introspection: introspection: http://www http://www.ibm.com/developerw .ibm.com/developerworks/library/l-pyi orks/library/l-pyint.html nt.html (Introspections means in German language: Selbstbeobachtung, Selbstbeobachtung, Innenschau, … ) to be continued ...
12 Appendix Details, examples and applications are collected here.
- 33 -
Python for Lectures
12.1 Conversion of a Matlab Matlab file file into a Python Python file Here are some important conversion rules which I recognized (and worked): replace %
by #
comment commenting
.* .^ ; length()
* **
element by element power operator no line endings length of array
figure(1)
figure(1,(8,6),90)
hold on ...
hold(True) show()
in
ind
x(in) a = b for in = 1:length(x2)
x[ind] a = b[:] for ind in range(len(x2)):
len()
comment
num=1, more figsize=(8,6), possibilities dpi=90 for figures to display figures „in“ is a keyword in use []-brackets new independ. obj.
pitfall!
See also „Python Lists and Arrays“ at http://www.brpreiss. http://www.brpreiss.com/books/opus com/books/opus7/html/page82.ht 7/html/page82.html ml See also: http://www.scipy http://www.scipy.org/Cookbook/Matpl .org/Cookbook/Matplotlib/Animatio otlib/Animations ns http://www.scipy.org/Cookbook/Matplotlib,, http://www.scipy.org/Cookbook , http://www.scipy.org/Cookbook/Matplotlib and: http://bytes.com/gro http://bytes.com/groups/python/44332 ups/python/44332-there-better-in -there-better-interactive-plotter-then teractive-plotter-then-pylab -pylab
12.2 Figures with matplotlib http://seehuhn.de/blog/59 2009-02-03 I like to use the matplotlib Python library to create figures for use in my articles. Unfortunately, the matplotlib default settings seem to be optimised for screen-viewing, so creating pictures for inclusion in a scientific paper needs some fiddling. Here is an example script which sets up things for such a figure. #! /usr/bin/env python import matplotlib matplotlib.use("PS") from pylab import * rc("font", family="serif") rc("font", size=10) width = 4.5 height = 1.4
- 34 -
Python for Lectures rc("figure.subplot", left=(22/72.27)/width) left=(22/72.27)/width) rc("figure.subplot", right=(width-10/72.27)/width) right=(width-10/72.27)/width) rc("figure.subplot", bottom=(14/72.27)/height) bottom=(14/72.27)/height) rc("figure.subplot", top=(height-7/72.27)/height) top=(height-7/72.27)/height) figure(figsize=(width, height)) x = load("x.dat") plot(x[:,0], x[:,1], "k-") savefig("figure.eps")
The script performs the following following steps: * The matplotlib.use statement tells matplotlib to use one of the non-interactive non-interactive backends. Without this statement, the script would need a connections to an X-windows server to run. * The two rc statement set the font family and size for the labels on the axes to match the font used in the article's text (matplotlib uses a 12pt sans-serif font by default). * The rc("figure.subplot", ...) settings and the figsize argument to the figure call set the image geometry. Since the text on journal pages typically is about 5in wide, a total figure width of 4.5in is often a good value. The margins set by the t he rc statements have to be given as fractions f ractions of the image width/height and must be big enough to contain the axis labels. A bit of experimentation experimentation helps to find good values; here we use a 14pt margin at the t he bottom (10pt font height plus some space), 22pt left margin (depends on the number of digits in the y-axis labels), 7pt for the top margin (space for half of the top-most y-axis label, and then some more) and 10pt to the right (enough space for half the right-most x-axis label). * The color code "k" in the plot command sets the plot color to black. Matplotlib default is blue, but some journals charge you for use of colour in your articles (and also it is nice if you can print things on a b/w printer). * The savefig command creates an "encapsulated "encapsulated PostScript file". This format is good for inclusion in TeX documents. The file can be converted to PDF using the epstopdf command as needed. This is an excerpt from Jochen's blog.
12.3 Nonblocking Plots with Matplotlib Is there a way to detach matplotlib plots so that the computation can continue? http://stackoverflow.com/questions/458209/is-there-a-w http://stackoverflow.com/questions/45820 9/is-there-a-way-to-detach-ma ay-to-detach-matplotlib-plots-so tplotlib-plots-so-that-the-that-thecomputation-can-continue After these instructions in the Python interpreter one gets a window with a plot: from matplotlib.pyplot import * plot([1,2,3]) show() # other code
Unfortunately, I don't know how to continue to interactively explore the figure created by show() while the program does further calculations. Is it possible at all? Sometimes calculations are long and it would help if they would proceed during examination of intermediate results.
Use matplotlib's calls that won't block: Using draw(): - 35 -
Python for Lectures from matplotlib import plot, draw, show plot([1,2,3]) draw() print 'continue 'continue computation' show()
# it’s it’s not blocked – do anything anything here instead!
# at the end call show to ensure window won't close.
Using interactive mode: from matplotlib import plot, ion, show ion() # enables interactive mode plot([1,2,3]) # result shows immediatelly (implicit draw()) print 'continue 'continue computation' show()
# it’s it’s not blocked – do anything anything here instead!
# at the end call show to ensure window won't close.
12.4 Matplotlib's OO Class Library For those people coming to Matplotlib without any prior experience of MatLab and who are familiar with the basic concepts of programming API's and classes, learning to use Matplotlib via the class library is an excellent choice. The library is well put together and works very intuitively once a few fundamental f undamentals s are grasped. The advice from John Hunter, the original developer of the library is 'don't be afraid to open up matplotlib/pylab.py matplotlib/pylab.py to see how the pylab interface forwards its calls to the t he OO layer.' That in combination with the user's guide, the examples/embedding examples/embedding demos, and the mailing lists, which are regularly read by many developers are an excellent way to learn the class library. Following is a brief introduction to using the class library, developed as I came to grips with how to produce my first graphs. For getting started with Matplotlib's Matplotlib's OO Class Library see: http://matplotlib.sourceforge.net/leftwich_tut.txt
12.5 File and Directory handling/manipulatio handling/manipulation n Check if an entry is a file or directory in Python: http://lookherefirst.wordpress.com/2007 http://lookherefirst.w ordpress.com/2007/12/03/check-i /12/03/check-if-an-entry-is-a-fil f-an-entry-is-a-file-or-directory-i e-or-directory-in-python/ n-python/ To check whether an entry is a file f ile or a directory in python, use the os.path.isdir() method. To read some file attributes, use the os.stat() method. The following code will read all entries in the current directory and then print out whether the entry is a file f ile or a directory together with the time the file/dir was created. import os, time dirList = os.listdir("./") for d in dirList: if os.path.isdir(d) == True: stat = os.stat(d) created = os.stat(d).st_mtime os.stat(d).st_mtime asciiTime = time.asctime( time.gmtime( created ) ) print d, d, "is a dir (created", asciiTime, ")"
- 36 -
Python for Lectures else: stat = os.stat(d) created = os.stat(d).st_mtime os.stat(d).st_mtime asciiTime = time.asctime( time.gmtime( created ) ) print d, "is a file (created", asciiTime, ")"
A simple non-recursive non-recursive directory walker: http://code.activestate.com/recipes/435875/ This is also usefull for incremental directory walking (where you want to scan remaining directories in subsequent calls). import os, os.path startDir = "/" directories = [startDir] while len(directories)>0: len(directories)>0: directory = directories.pop() for name in os.listdir(director os.listdir(directory): y): fullpath = os.path.join(direct os.path.join(directory,name) ory,name) if os.path.isfile(full os.path.isfile(fullpath): path): print fullpath # That's a file. Do something with it. elif os.path.isdir(fullpath): os.path.isdir(fullpath): directories.append(fullpath) directories.append(fullpath) # It's a directory, directory, store store it.
12.6 Measuring raw speed Here's one way (http://www.python.or (http://www.python.org/doc/essays/pp g/doc/essays/ppt/hp-training/sld04 t/hp-training/sld041.htm 1.htm ): import time def timing(func, arg, ncalls=100): r = range(ncalls) t0 = time.clock() for i in r: func(arg) t1 = time.clock() dt = t1-t0 print "%s: %.3f ms/call (%.3f seconds / %d calls)" \ % ( func.__name__, 1000*dt/ncalls, dt, ncalls)
Using the profile module: http://www.py http://www.python.org/doc/essays/ppt thon.org/doc/essays/ppt/hp-training/sld /hp-training/sld040.htm 040.htm
12.7 Video processing See: http://www.goo http://www.google.at/#sclient=psy gle.at/#sclient=psy&hl=de&q=py &hl=de&q=python+video+mod thon+video+module ule http://pymedia.org/tut/ : PyMedia is a Python library for accessing and manipulating media files. It makes audio and video playback/creation a snap for even a newcomer to programming. This tutorial aims to walk you through installing and using the PyMedia library. For the sake of simplicity most of the examples have been kept concise and straightforward. straightforward. In a 'real world' application, you would need to take care of error handling, input validation and so on.
- 37 -
Python for Lectures http://pymedia.org/tut/src/dump_video.py.html: #! /bin/env python import sys, os import pymedia.muxer as muxer import pymedia.video.vcodec as vcodec import pygame def dumpVideo( inFile, outFilePattern, fmt ): dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] ) i= 1 f= open( inFile, 'rb' ) s= f.read( 400000 ) r= dm.parse( s ) v= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_VIDEO, dm.streams ) if len( v )== 0: raise 'There is no video stream in a file %s' % inFile v_id= v[ 0 ][ 'index' ] print 'Assume video stream at %d index: ' % v_id c= vcodec.Decoder( dm.streams[ v_id ] ) while len( s )> 0: for fr in r: if fr[ 0 ]== v_id: d= c.decode( fr[ 1 ] ) # Save file as RGB BMP if d: dd= d.convert( fmt ) img= pygame.image.fromstring( dd.data, dd.size, "RGB" ) pygame.image.save( img, outFilePattern outFilePattern % i ) i+= 1 s= f.read( 400000 ) r= dm.parse( s ) print 'Saved %d frames' % i # --------------------------------------------------------------------------------------------------------------------------------------------------------------# Dump the whole video file into the regular BMP images in the directory and file name specified # http://pymedia.org/ if __name__ == "__main__": if len( sys.argv )!= 4: print 'Usage: dump_video \n \n