Você está na página 1de 23

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Scientic Computing in Computer Science, Munchen Technische Universitat

Scripting with Python ... and beyond


Compact Course @ GRS
Tobias Neckel
June 03 - 07, 2013

Part I Python Overview

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 1

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 2

What Python is all about. . .

What Python is all about. . .


Python is. . .
High-level (really high-level) Truly object-oriented Interpreted Scalable Extensible Portable, available on Windows, Linux, Mac OS X, Amiga, HPC

Python
Invented in 1990 as teaching language Designed for clearness and readability Named after Monty Python (not the snake)

machines and clusters, web servers, Palm and other Handhelds, Nokia Mobiles, .NET virtual machines, . . .

Versatile Easy to learn, understand, use, and maintain (really!) Free, open-source ...
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 3 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 4

What Python is all about. . .


Suitable for
OS scripting (Python beats Bash ;-)) Internet Programming Scientic Computing (up to whole Tsunami Simulations) Parallelisation GUI (Tkinter) Visualisation Rapid prototype development . . . and much, much more!

What Python is all about. . .

Some more features


Dynamically typed Automatic garbage collection Different programming paradigmes possible (procedural, OO,

functional, aspect oriented, . . . )

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 5

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 6

What Python is all about. . .

Requirements

Some more features


Dynamically typed Automatic garbage collection Different programming paradigmes possible (procedural, OO,

To be able to use all examples you require. . .


IPython (call ipython) NumPy and SciPy Pylab (as it ships with Matplotlib) Tkinter swig (Simplied Wrapper and Interface Generator)

functional, aspect oriented, . . . )

Note
Were assuming Python 2.5.X, 2.6.X, or 2.7.X The current version Python 3.X requires few changes

independent of Python

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 6

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 7

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Lets get started. . .


Start the Python interpreter At prompt >>> type command, press <Enter> to confirm

Part II Hands On. . .

$ python Python 2.7.3 ( default , Aug 1 2012 , 05:14:39) [ GCC 4.6.3] on linux2 Type " help " , " copyright " , " credits " or " license " for more information . >>> help Type help () for interactive help , or help ( object ) for help about object . >>> help ()
Welcome to Python 2.7! utility . [...] This is the online help

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 8

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 9

Getting help

A rst Hello World!


Python simplify your Code Just type

Vast online documentation at http://docs.python.org/ Interactive using


help() for interactive help, help(modules) for a list of all available modules help(object|command) for help on an object or command

>>> print " Hello World ! " Hello World ! Compare with Java, e.g.: public class Hello { public static void main ( String argv []){ System . out . println ( " Hello World ! " ); } }

Literature

David M. Beasley: Python - Essential Reference, Addison-Wesley Professional, 4th edition, 2009 Hans Petter Langtangen: A Primer on Scientic Programming with Python, Springer, 2009

$ javac Hello . java $ java Hello Hello World !


Tobias Neckel: Scripting with Python... and beyond 10 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 11

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Statements and assignments


>>> >>> >>> >>> >>> >>> >>> >>> >>> 3+4**2/8+1 (6*3.5)*2 -5 10 e -4 a =17 a a -1.5 b = " Hello World ! " print b print a , b , (4+5**0.5)

3.5 Ways to Python

1. Start the Python interpreter with python 2. Use IPython: ipython 3. Run Python on a le 3.5. Execute as a script

assignment: variableName = expression (different to math. =) Interactive session shows return values print prints variables and expressions as string

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 12

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 13

2. IPython
$ ipython Python 2.7.3 ( default , Aug 1 2012 , 05:14:39) Type " copyright " , " credits " or " license " for more information .
IPython 0.13.2. rc2 -- 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 . In [1]: print " Hello World !" Hello World !

2. IPython
Highlights
Enhanced interactive shell Additional shell syntax

magic functions, starting with % such as %psearch to search for function in namespace Access shell commands %cd Run scripts with run filename.py Log session, dene macros, . . . Pretty printing, toggle with %Pprint

Syntax highlighting Tab completion, string completion History

Access previous command blocks

Automatic indentation
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 14 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 15

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

3. Run Python on a File

3.5. Execute as a Script


Shell script as before

File square_me.py: print 6**2 Then run

Just tell shell to execute python: # !/ usr / bin / python print 6**2 Then set executable ag and run

$ python square_me . py 36

$ chmod u + x square_me . py $ ./ square_me . py 36

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 16

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 17

Built-in Python Types


Numeric Types bool boolean type

Part III Data Types and Control Structures

print True , False True == True True != True int integer 1+5 -12 Attention integer division: digits after the comma get lost! >>> 5/3 1 long arbitrary-precision integer 12345**100

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 18

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 19

Numeric Types (cont.)


float oating point (double precision) 3.14159265*5.6 1.5 e -10 complex complex numbers 1+4 j c = complex (2 ,3) print c d = 2 * ( c **3 - 3 j ) print d d . real d . imag

General Remarks
Everything is an object (int, oat, tuple, classes, . . . ) Thus everything can have attributes and methods c = complex (2 ,3) print c . real , c . imag Dynamic typing: Type of a variable determined during Automatic conversion where necessary and possible a = 1234 type ( a ) b = 100 type ( b ) a = 1234**100 type ( a ) b = b *1.0 # equals b = float ( b )*1.0 type ( b )
Tobias Neckel: Scripting with Python... and beyond 20 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 21

assignment

None type None special type (undened)


Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Built-in Sequences
Strings
string type Sequence of characters Single, double or triple double (multiline) quotes " ... there lived a hobbit . " " a hobbit " a " hobbit " a \ hobbit \ """ In a hole in the ground there " lived " a hobbit """ Concatenation s1 = " In a hole in the ground " s2 = " there lived a hobbit " s = s1 + + s2 print s
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 22

Built-in Sequences Strings (2)


Replication " Hi ! " *3 Indexing and Slicing (immutable sequence type) s = " Hello Hobbit " s [3] s [ -2] s [2:4] s [6:] s [:6] s [0: -1:2] # stride 2 s [0::2] s [2] = 4 # error len ( s )

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 23

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Built-in Sequences Strings (3)


String formatting using % " Bilbo s % d . birthday " % (111) " Frodo , %s , and Bilbo ate % f apples " %( " Sam " ,1.5) Some format modiers %05 d % (13) % -5 d % (13) %+5 d % (13) % d % (13) % e % (13) % g %g , % f % % s % ( hi ) %% # fill 0 # left justified # sign # signed integer # floating point exponential (13 , 1e -6 , 1e -6) # string # escape %

Built-in Sequences
Tuples Sequences of arbitrary objects (,) Immutable Indexing and slicing as before
l = (1 ,) l = (1 , " two " , (3 ,4 ,5)) l [0] l [ -1] l [1] = 2 # error len ( l ) min ( l ) # here : numbers smaller than strings l = ( " Hello " , " Hobbit " ) min ( l ) max ( l ) l2 = l + (6 ,7 ,8)

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 24

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 25

Built-in Sequences
Lists Sequences of arbitrary objects [,] Mutable
l = [1 , " two " , (3 ,4 ,5) , 5] l [0] l [ -1] l [1] = 2 # works ! len ( l ) Operations on lists l = [0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9] l [2:4] = [10 , 11] l [1:7:2] = [ -1 , -2 , -3] del l [::2]

Built-in Sequences
Lists (2) List methods
l = [0 ,1 ,2 ,3] l . append ( " four " ) l . extend ([5 ,6 ,7 ,8 ,9]) l . insert (8 , " four " ) l . count ( " four " ) l . sort () l . reverse () l . remove ( " four " ) l . pop () Integer ranges: range function range (10) range (5 , 10) range ( -10 , 20 , 3)
Tobias Neckel: Scripting with Python... and beyond 26 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 27

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Control Flow
General
Indentation used to group blocks! Code within same block has to be same level Needs getting used to, but encourages readable code

Comparison and Equality


Check for equality " Bilbo " == " Frodo " " Bilbo " != " Frodo " Comparison " Bilbo " " Bilbo " " Bilbo " " Bilbo " is and in " Bilbo " is " Frodo " # object identity " Bilbo " in [ " Frodo " , " Sam " , " Bilbo " ] # membership < " Frodo " > " Frodo " <= " Frodo " >= " Frodo "

if-statement
if x < y : print print elif x == print else : print print " x is smaller " x y: " both are equal " " y is smaller " y

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 28

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 29

Comparison and Equality (2)

Control Flow - for

Pythons for iterates over the items of a sequence type


Combining and grouping (and, or, not) if ( " Bilbo " != " Frodo " and not (1 > 2 or 2 > 3)): print " Sam was here " for i in range (5 ,10): print " loop index : " , i a = ( " a " , " tuple " , " with " , 5 , " words " ) for i in a : print i

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 30

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 31

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Control Flow - break and while


break breakes out of the enclosing loop (for or while) continue continues with the lext iteration of the loop a = ( " let " , " us " , " find " , " Bilbo " , " again " ) for word in a : if word == " Bilbo " : print " found Bilbo " break print word while loops until condition is False a = 2048; exp = 0 while a > 1: a /= 2 # a = a / 2 exp +=1 # exp = exp + 1 print a , " = " , 2 , " ^ " , exp
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 32

Functions (and Procedures)


Functions are dened using def No return statement means returning None Call by reference (Note: Assignment creates a new local object) def hi (): print " Hello World ! " def factorial ( n ): fac = 1 for i in range (2 , n +1): fac = fac * i return fac Functions are objects and can be assigned, too f = factorial f (3)
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 33

Functions (2)
Default arguments def printme ( s = " Frodo " ): print s printme () printme ( " Bilbo " ) Keyword arguments def exp ( basis , exponent ): return basis ** exponent exp ( basis =2 , exponent =10) exp ( exponent =10 , basis =2)

Up to now. . .

Variables Numeric types Sequence types (str, tuple, list; more tomorrow) if comparison for, while, break, continue Functions and parameters

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 34

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 35

More on Strings
Special string methods (excerpt)
s = " Frodo and Sam and Bilbo " s . islower () s . isupper () s . startswith ( " Frodo " ) # try s . startswith (" Frodo " , 2) s . endswith ( " Bilbo " ) s = s . strip () s . upper () s = s . lower () s . capitalize () # capitalize first character s = s . center ( len ( s )+4) # center ( padding default : space ) s . lstrip () s . rstrip ( " " ) s = s . strip () s . find ( " sam " ) s . rfind ( " and " )
Tobias Neckel: Scripting with Python... and beyond 36 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 37

Part IV More on Python

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

More on Strings (2)


Searching, splitting and joining (excerpt)
s . find ( " sam " ) s . rfind ( " and " ) s . replace ( " and " , " or " ) s . split () # or s . split ( None ); arbitrary numb . whitesp . s . split ( " " ) s . split ( " and " ) s . split ( " and " , 1) s = """ Line by Line """ s . splitlines () " , " . join ([ " sequence " , " of " , " strings " ])
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 38

More on Built-in Sequences


Dictionaries
Map keys to values, arbitrary types Only immutable objects as keys Can serve as database d = {} d [ " Bilbo " ] = " Hobbit " d [ " Elrond " ] = " Elf " d [42] = 1234 d . has_key ( " Bilbo " ) d . keys () # unordered d . items () d . values () del d [42] d . clear ()

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 39

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

More on Functions
Functions have to be dened before actually called for the rst

Call by Reference vs. Call by value


Call by reference: address (id) of parameter is handed over Call by value: value of parameter is handed over In Python: always call by reference But: assignment changes reference within function >>> >>> >>> >>> >>> >>> 3 def add_one ( x ): x = x + 1 x = 3 add_one ( x ) x

time

Note: Python is an interpreted language

Variables
Variables dened or assigned to in functions have local scope Global variables (surrounding context) can be accessed To modify global variables use global count = 0 ... def count_it ( n ): global count count += 1 In general: avoid global variables (pass as params instead)
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 40

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 41

Call by Reference vs. Call by value (2)


After assignment: local variable references to a new object. Using methods, the original object can be modied. >>> def append1 ( l ): >>> l = l + [4] >>> >>> def append2 ( l ): >>> l . append (4) >>> >>> l = [1 ,2 ,3] >>> append1 ( l ); l [1 ,2 ,3] >>> append2 ( l ); l [1 ,2 ,3 ,4]

More on Functions (2)


Return values
Functions can have multiple return values (returned as tuple) def get_hobbits (): return " Bilbo " , " Frodo " h = get_hobbits () (a , b ) = get_hobbits () Assignment rules correspond to slice assignment l = [4 , 2] (a , b ) = l # implicit type conversion a, b = l l [0:3] = (3 , 2) # implicit type conversion

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 42

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 43

More on Functions (3)

Arithmetic Operations
Classical operations
x x x x x x x x + y # Addition - y # Subtraction * y # Multiplication / y # Division // y # Truncated division ** y # Exponentiation % y # Modulo -= 2; x *= 4; ... Division differs for int and float (Python < 3.0) 7/4 7.0/4

Docstrings
Functions (and classes, modules, ...) can provide help text String after function header (can be multiline) def answer (): " Returns an answer to a question " print " answer " help ( answer )

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 44

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 45

More Operations
General functions
abs ( x ) divmod (x , y ) pow (x , y [ , modulo ]) round (x , [ n ]) # ( x // y , x % y ) # x ** y % modulo # round to 10**( - n )

File I/O
Opening les
Create le object (text les) fd fd fd fd = = = = open ( " testfile . txt " ) open ( " testfile . txt " , r ) open ( " testfile . txt " , w ) open ( " testfile . txt " , a ) # # # # read read write append

Operations on integers
x << y x >> y x & y x | y x ^ y ~x # # # # # # Left shift Right shift Bitwise and Bitwise or Bitwise xor Bitwise negation

Create le object (binary les) fd = open ( " testfile . txt " , rb ) fd = open ( " testfile . txt " , wb ) fd = open ( " testfile . txt " , ab ) # read # write # append

open has more options (encoding, how to handle newlines, . . . )


Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 46 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 47

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

File I/O (2)


Methods of le objects Reading
fd . read () fd . read ( n ) fd . readline () fd . readlines () Writing fd . write ( " New Text \ n " ) fd . writelines ([ " first line \ n " , " Second \ n " ]) Dont forget to write newlines Closing fd . close ()
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 48

File I/O (3)


Iterating over textle for loop over le line by line
fd = open ( " fulltext . txt " ) for line in fd : # rather than : in fd . readlines () print line # or : while True : line = fd . readline () if not line : break print line

# all # n Bytes # one line #

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 49

File I/O (3)


Iterating over textle for loop over le line by line
fd = open ( " fulltext . txt " ) for line in fd : # rather than : in fd . readlines () print line # or : while True : line = fd . readline () if not line : break print line

Getting more. . .

Modules Outsource functionality in separate .py les Import them as library module Example (tools.py):
""" This module pro v ide s some helper tools . Try it . """ counter = 42 def readfile ( fname ): " Read text file . Returns list of lines " fd = open ( fname , r ) data = fd . readlines () fd . close () return data def do_nothing (): " Do really nothing " pass

Some more functions on le objects fd.tell() get current le position fd.seek(offset) set le to position fd.flush() ush output buffer. Note: buffered for efciency
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 49

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 50

Modules (2)
Import module import tools tools . do_nothing () print tools . counter Import module and change name import tools as t t . do_nothing () Import selected symbols to current namespace from tools import do_nothing , readfile from tools import counter as cntr do_nothing () print cntr
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 51

Modules (3)
Import all symbols to current namespace from tools import * do_nothing () print counter Modules can control which symbols are imported by from module import *: # module tools . py __all__ = [ readfile , counter ]

Then do_nothing() is unknown after import *

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 52

Modules (3)
Import all symbols to current namespace from tools import * do_nothing () print counter Modules can control which symbols are imported by from module import *: # module tools . py __all__ = [ readfile , counter ]

Modules (4)
Getting help Access docstrings
import tools help ( tools ) help ( tools . do_nothing )

Then do_nothing() is unknown after import * Inspect namespace Inspect namespace of module with
dir ( tools )
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 52 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 53

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Modules (4)
Getting help Access docstrings
import tools help ( tools ) help ( tools . do_nothing )

Modules (5)
Reload module
When debugging a module reload with reload (Python<3.0) reload ( tools )

Module search path Execute module as main program tools.py should serve as program and module
# tools . py ... if __name__ == __main__ : print " tools . py executed " else : print " tools . py imported as module " Modules have to be in current directory or in directory in search

path

import sys sys . path . append ( " folder / to / module " ) import ...

Automatically extend sys.path by setting environment variable


PYTHONPATH
Tobias Neckel: Scripting with Python... and beyond 53 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 54

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Packages
Group modules Modules can be grouped together Folder structure determines modules, e.g.:
tools / __init__ . py files . py graphics . py stringtools / __init__ . py ... further

Command-Line Options
Using sys
sys.argv is list of command-line options First one (sys.argv[0]) is program name # contents for " import tools " # for " import tools . files " # for " import tools . graphics " # for " import tools . stringtools " nesting import sys print " Executing : % s " % ( sys . argv [0]) if len ( sys . argv ) < 2: print " Not enough parameters " sys . exit (1) print " Parameters : " print " , " . join ( sys . argv [1:]) Parse parameters...

If from tools import * should import submodules, tools/__init__.py has to contain


__all__ = [ " files " , " graphics " ]

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 55

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 56

Command-Line Options (2)


Module optparse Class OptionParser in module optparse simplies option parsing
# !/ usr / bin / python import optparse p = optparse . OptionParser () # specify options p . add_option ( " -o " , action = " store " , dest = " opt " ) p . add_option ( " -v " ," -- verbose " , action = " store_true " , dest = " verbose " , help = " Produce more output " ) # parse options ( options , args ) = p . parse_args () if options . verbose : print " Starting program ... " print options , args
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 57

Module optparse (2)

Program now supports


Default help Two custom parameters

Try (parameters.py): ./ parameters . py ./ parameters . py -h ./ parameters . py -o " Enjoy it " -- verbose ./ parameters . py -v file1 . txt file2 . txt

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 58

Module optparse (3)


Some parameters of add_option One character "-x" and/or multiple character switches "--xyz" action can be store, store_true, store_false, append,
count dest name of option default default value help help text type one of string (default), int, long, float, complex, choice choices = [first, second, third] for type=choice

Module optparse (3)


Some parameters of add_option One character "-x" and/or multiple character switches "--xyz" action can be store, store_true, store_false, append,
count dest name of option default default value help help text type one of string (default), int, long, float, complex, choice choices = [first, second, third] for type=choice

Help text with set_usage Reference to program name with %prog


p . set_usage ( % prog [ options ] file ( s ) Do nice things with file ( s ) )
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 59 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 59

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Standard Input, Output, and Error


Module sys provides three standard le objects
sys.stdin read only sys.stdout, sys.stderr

More on Lists
List comprehension
Short notation to create lists even_squares = [] for i in range (10): if i %2 == 0: even_squares . append ( i * i ) even_squares = [ i **2 for i in range (10) if i %2==0]

write only

import sys sys . stdout . write ( " Enter line :\ n " ) # = print "..." line = sys . stdin . readline () # or : line = raw_input (" Enter line :\ n ") if error : sys . stderr . write ( " Error !\ n " ) write does not add newlines raw_input([text]) strips endling newline Input and output buffered
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 60

Compare: i 2 | i {0, . . . , 9}, i even

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 61

More on Lists
List comprehension
Short notation to create lists even_squares = [] for i in range (10): if i %2 == 0: even_squares . append ( i * i ) even_squares = [ i **2 for i in range (10) if i %2==0]

More on Lists (2)


Advanced slice assignement Note: assigning slices of mutable sequences can change size
l = range (5) l [1:3] = [7 ,8 ,9] l [4:6] = [6]

Can contain more than one for ... in ... [if ...] [( x , y . upper ()) for x in range (4) if x %2 == 0 for y in [ " a " , " b " , " c " ]]

Compare: i 2 | i {0, . . . , 9}, i even

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 61

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 62

More on Lists (2)


Advanced slice assignement Note: assigning slices of mutable sequences can change size
l = range (5) l [1:3] = [7 ,8 ,9] l [4:6] = [6]

Even more on Built-in Sequences


set and frozenset set is mutable, frozenset is not
s = set ([3 ,1 ,2 ,3 , " foo " ,2]) len ( s ) l = [2 ,8 ,7] # any iterable sequence s . difference ( l ) s . intersection ( l ) s . union ( l ) s . sy mm et ri c_ dif ference ( l ) s . issubset ([3 , " foo " ]) s . issuperset ([3 , " foo " ]) For set s . add (42) s . remove (3) s . in ters ecti on_upd ate ( l )
Tobias Neckel: Scripting with Python... and beyond 62 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 63

range and xrange

range([i,] j [, stride]) creates list Memory allocated for whole list, even when only iterating over xrange object calculates values when accessed (generator) for i in range (650000): do_something () for i in xrange (650000): do_something ()

them, especially in loops

# and more methods ...

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Even more on Functions


Anonymous functions
Can be dened using keyword lambda Lambda functions allow functional programming def inc ( i ): return i +1 inc = lambda ( i ): i +1 ( lambda i : i +1)(4) Common use: map function on list items / lter list l = range (10) map ( lambda x : x * x +1 , l ) filter ( lambda x : x %2==0 , l )

Part V Object Oriented Programming

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 64

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 65

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

What is an object?

What is an object?
a car a cat a chair ...

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 66

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 66

What is an object?
a car a cat a chair ... a molecule a star a galaxy ...

What is an object?
a car a cat a chair ... a molecule a star a galaxy ... anything that represents a real thing anything that represents an abstract thing anything which has some properties

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 66

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 66

How to Program Object Oriented


Change your way of thinking Change your point of view It is not just a technical question!

Theory: Classes and Objects


Classes A class is similiar to the denition of a type Species properties of the objects to be created Data to be stored once for all objects (class/static variable) Data to be stored in each object (member variables) Operations to be done with objects (methods) Represents a class of things/objects

In Python? You do it all the time


You just dont know it In Python, everything is an object (event int) a =4 a . __str__ ()

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 67

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 68

Theory: Classes and Objects


Classes A class is similiar to the denition of a type Species properties of the objects to be created Data to be stored once for all objects (class/static variable) Data to be stored in each object (member variables) Operations to be done with objects (methods) Represents a class of things/objects Instances / Objects An instance/object is a concrete thing of the type of a class A class species properties, an object has specic values for the specied properties For each class, there can be an arbitrary number of objects Each object belongs exactly to one class (exception: polymorphism)
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 68

Theory: Constructor, Destructor


When creating an instance ... Memory is allocated A variable for storing the object is created Perhaps some things are initialized The initialisation is done with a constructor (__init__(self,...)) When a variable is not used any more ... Memory has to be freed Perhaps some things have to be cleaned up The destructor (__del__(self,...)) is called BUT: No guarantee at which time it is called
class Example : def __init__ ( self , ...): do_something def __del__ ( self , ...): do_something
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 69

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

First Class Example


# !/ usr / bin / python class GRSEmployee ( object ): num_employees = 0 def __init__ ( self , name ): self . name = name GRSEmployee . num_employees += 1 def getName ( self ): return self . name @staticmethod def numPeople (): return GRSEmployee . num_employees person1 = GRSEmployee ( " John " ) person2 = GRSEmployee ( " Mary " ) print person1 . getName () print GRSEmployee . numPeople ()
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 70

Theory: self
Usage of an object
Access to methods: object.method(...) Example: list.append(x) Access to variables: object.variable Example: complex.real

Access witin methods


Concrete object is created outside the class denition

Methods of a class do not know the object name (actual parameter)

All methods get additional formal parameter self

self always is the rst formal parameter, but is skipped at a call Within methods: access to other methods and variables via self.variable and self.method(), resp.

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 71

Theory: Methods

Procedural Approach to Operate an Oven


# !/ usr / bin / python def doBaking ( temperature , mode ): do_something

. . . implement the behaviour of objects. . . . represent all types of changes of an object after initialisation. accessor methods: give info on state of an object mutator methods: change state of an object

oventemperature = 180 ovenmode = 2 doBaking ( oventemperature , ovenmode )

If ive got a whole bakery?


oventemperatures = [] ovenmodes = [] oventemperatures . append (190) ovenmodes . append (2) ... for i in range ( len ( oventemperatures )): do_baking ( oventemperatures [ i ] , ovenmodes [ i ])
Tobias Neckel: Scripting with Python... and beyond 72 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 73

change (at least) one object/member variable

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Object Oriented Approach to Operate an Oven


# !/ usr / bin / python class Oven ( object ): def __init__ ( self , temperature , mode ): self . temperature = temperature self . mode = mode def doBaking ( self ): do_something myOven = Oven (180 ,2) myOven . doBaking ()

What Are the Differences?


Procedural approach
Procedures specify how to produce something When implementing them, you have to think of actions You have to care about storing the data yourself

Object oriented approach


Objects specify things (real or abstract) When implementing them, you have to think of the thing and its

Again a whole bakery


ovens = [] ovens . append ( Oven (190 ,2)) ... for oven in ovens : oven . doBaking ()
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 74

properties

All data is stored in the object itself Whenever lots of data elements are associated with an action,

try to think object oriented

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 75

Inheritance

Inheritance in Real Life

Classes can inherit from a base class / superclass This makes them a derived class / subclass of the superclass All classes can be specialized they become superclasses All classes together form a class hierarchy (taxonomy) Properties of the superclass can be taken over or can be redened

# !/ usr / bin / python class MasterStudent ( GRSEmployee ): def __init__ ( self , name , thesis ): self . thesis = thesis GRSEmployee . __init__ ( self , name ) def printInfo ( self ): print " % s works on % s " % ( self . name , self . thesis ) person3 = MasterStudent ( " Terry " , " Terry s thesis topic " ) person3 . printInfo ()
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 76 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013

object

77

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Inheritance in Real Life

Inheritance in Real Life


car machine object plant tree flower comp. animal reptile laptop desktop vectorc. cluster

table furniture machine object plant comp. animal

chair furniture

mammal

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 77

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 77

Inheritance in Real Life


sunbed table chair furniture oak cypress beech daisy tulip rose car machine object plant tree flower comp. animal reptile Audi VW BMW laptop desktop vectorc. cluster croc. snake

Inheritance in Real Life


sunbed table chair furniture oak cypress beech daisy tulip rose
77

car machine object plant

Audi VW BMW comp. animal reptile

Golf Polo Phaeton laptop desktop vectorc. cluster croc. snake mule
77

mammal

tree flower

mammal

horse donkey cat

horse donkey cat

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Information Hiding
public
Per default: everything (all variables/methods) Comfortable (access from outside) Often dangerous / undesired

Special Variables and methods


Variables and Methods starting with __, but not ending with __ like __privateVar and __privateMet() are private __get__(value): Allows reading via [] __set__(i, value): Allows writing via [] __add__(value): Denes an addition with + __str__(): used if object is a parameter for print class Test : def __init__ ( self , val ): self . value = val def __add__ ( self , b ): return self . value + b . value +1 def __mul__ ( self , b ): return self . value * b . value *2

private
External access (i.e. from outside the class) forbidden

Variables/methods not visible externally

Variables/methods not usable externally

Syntax: starting with __, but not ending with __ (__privateVar, __privateMet())

Attention: indirect access possible (._class__privateVar)

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 78

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 79

More on Maths
Module math
Constants pi and e

Part VI Scientic Computing in Python

Functions that operate on int and float All return values float ceil ( x ) floor ( x ) exp ( x ) fabs ( x ) ldexp (x , i ) log ( x [ , base ]) log10 ( x ) modf ( x ) pow (x , y ) sqrt ( x )

# same as globally defined abs () # x * 2** i # == log (x , 10) # ( fractional , integer part ) # x ** y

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 80

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 81

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Module math (2)


Trigonometric functions assume radians cos ( x ); cosh ( x ); acos ( x ) sin ( x ); ... tan ( x ); ... degrees ( x ) radians ( x ) # rad -> deg # deg -> rad

Module math (2)


Trigonometric functions assume radians cos ( x ); cosh ( x ); acos ( x ) sin ( x ); ... tan ( x ); ... degrees ( x ) radians ( x ) inf/nan float ( " inf " ) float ( " - inf " ) float ( " nan " ) # rad -> deg # deg -> rad

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 82

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 82

Module math (2)


Trigonometric functions assume radians cos ( x ); cosh ( x ); acos ( x ) sin ( x ); ... tan ( x ); ... degrees ( x ) radians ( x ) inf/nan float ( " inf " ) float ( " - inf " ) float ( " nan " ) Use module cmath for complex numbers
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 82

Now to Real Maths. . .


Standard sequence types (list, tuple, . . . )
Can be used as arrays Can contain different types of objects

# rad -> deg # deg -> rad

Very exible, but slow Loops are not very efcient either

For efcient scientic computing, other datatypes and methods

required

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 83

Now to Real Maths. . .


Standard sequence types (list, tuple, . . . )
Can be used as arrays Can contain different types of objects

Very exible, but slow Loops are not very efcient either

NumPy

For efcient scientic computing, other datatypes and methods

required

Modules
NumPy Matplotlib SciPy

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 83

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 84

Module numpy
Homogeneous arrays
NumPy provides arbitrary-dimensional homogeneous arrays Example from numpy import * a = array ([[1 ,2 ,3] ,[4 ,5 ,6]]) print a type ( a ) a . shape print a [0 ,2] a [0 ,2] = -1 b = a *2 print b

Array creation
Create from (nested) sequence type Direct access with method [] a = array ([1 ,2 ,3 ,4 ,5 ,6 ,7 ,8]) a [1] a = array ([[1 ,2 ,3 ,4] ,[5 ,6 ,7 ,8]]) a [1 ,1] a = array ([[[1 ,2] ,[3 ,4]] ,[[5 ,6] ,[7 ,8]]]) a [1 ,1 ,1]

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 85

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 86

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Array creation
Create from (nested) sequence type Direct access with method [] a = array ([1 ,2 ,3 ,4 ,5 ,6 ,7 ,8]) a [1] a = array ([[1 ,2 ,3 ,4] ,[5 ,6 ,7 ,8]]) a [1 ,1] a = array ([[[1 ,2] ,[3 ,4]] ,[[5 ,6] ,[7 ,8]]]) a [1 ,1 ,1] Properties of arrays a . ndim a . shape a . size a . dtype a . itemsize # # # # # number of dimensions dimensions number of elements data type number of bytes

Data Types
Exact, C/C++-motivated type of array elements can be specied Otherwise, defaults are used Some types (different storage requirements): int_, int8, int16, int32, int64, float_, float8, float16, float32, float64, complex_, complex64, bool_, character, object_ Standard python type names result in default behaviour array ([[1 ,2 ,3] ,[4 ,5 ,6]] , dtype = int ) array ([[1 ,2 ,3] ,[4 ,5 ,6]] , dtype = complex ) array ([[1 ,2 ,3] ,[4 ,5 ,6]] , dtype = int8 ) array ([[1 ,2 ,3] ,[4 ,5 ,1000]] , dtype = int8 ) # wrong array ([[1 ,2 ,3] ,[4 ,5 , " hi " ]] , dtype = object ) Exception: object_ stores pointers to objects
Tobias Neckel: Scripting with Python... and beyond 86 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 87

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Create Arrays
(Some) default matrices (optional parameter: dtype) arange ([ a ,] b [ , stride ]) zeros ( (3 ,4) ) ones ( (1 ,3 ,4) ) empty ( (3 ,4) ) linspace (a , b [ , n ]) logspace (a , b [ , n ]) identity ( n ) # as range , 1 D

Manipulate Arrays
Reshaping arrays a = arange (12) b = a . reshape ((3 ,4)) a . resize ((3 ,4)) a . transpose () a . flatten ()

# # # #

uninitialized ( fast ) n equidistant in [a , b ] 10** a to 10** b 2d

# in - place !

fromfunction ( lambda i , j : i +j , (3 ,4) , dtype = int ) def f (i , j ): return i + j fromfunction (f , (3 ,4) , dtype = int )

# Example use - case : a = arange (144) a . resize ((12 ,12))

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 88

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 89

Create Arrays (2)


Create/Copy from existing data a = arange (12); a . resize ((3 ,4)) copy ( a ) diag ( a ); tril ( a ); triu ( a ) empty_like ( a ) zeros_like ( a ) ones_like ( a ) # copy shape

Array Access and Manipulation


Typical slicing operations can be used Separate dimensions by comma a = arange (20); a . resize ((4 ,5)) a [1] a [1:2 ,:] a [: ,::2] a [::2 ,::2] a [::2 ,::2] = [[0 , -2 , -4] ,[ -10 , -12 , -14]] a [1::2 ,1::2] = -1* a [1::2 ,1::2] Selective access a [ a > 3] a [ a > 3] = -1 # tofile () if binary
Tobias Neckel: Scripting with Python... and beyond 90 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 91

a = loadtxt ( " matrix . txt " ) # fromfile () if binary # plenty of options : comments , delim . , usecols , ... Matrix output a . tolist () savetxt ( " matrix . txt " , a )
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Array Access
Iterating over entries for row in a : print row b = arange (30); b . resize ((2 ,3 ,4)) for row in b : for col in row : print col for entry in a . flat : print entry

Computing with Arrays


Fast built-in methods working on arrays a = arange (12); a . resize ((3 ,4)) 3* a a **2 a + a ^2 sin ( a ) sqrt ( a ) prod ( a ) sum ( a ) it = transpose ( a ) x = array ([1 ,2 ,3]) y = array ([10 ,20 ,30]) inner (x , y ) dot ( it , x ) cross (x , y )
Tobias Neckel: Scripting with Python... and beyond 92 Compact Course @ GRS, June 03 - 07, 2013 93

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Computing with Arrays


There is much more. . . var () mean () min () svd () tensordot () ... cov () median () max () std ()

Submodules
Module numpy.random
Draw from plenty of different distributions More powerful than module random Work on and return arrays from numpy . random import * binomial (10 , 0.5) # 10 trials , success 50% binomial (10 , 0.5 , 15) randint (0 , 10 , 15) # [0 ,10) , int rand () rand (3 ,4) # [0 ,1) #

Matrices (with mat) are subclasses of ndarray, but strictly

two-dimensional, with additional attributes


m = mat ( a ) m.T # transpose m.I # inverse m.A # as 2 d array m.H # conjugate transpose

(3 x4 ) array

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 94

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 95

Submodules (2)
Module numpy.linalg
Core linear algebra tools norm ( a ); norm ( x ) inv ( a ) solve (a , b ) # LAPACK LU decomp . det ( a ) eig ( a ) cholesky ( a )

Submodules (2)
Module numpy.linalg
Core linear algebra tools norm ( a ); norm ( x ) inv ( a ) solve (a , b ) # LAPACK LU decomp . det ( a ) eig ( a ) cholesky ( a )

Module numpy.fft
Fourier transforms

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 96

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 96

Submodules (2)
Module numpy.linalg
Core linear algebra tools norm ( a ); norm ( x ) inv ( a ) solve (a , b ) # LAPACK LU decomp . det ( a ) eig ( a ) cholesky ( a )

Matplotlib

Module numpy.fft
Fourier transforms

There is more. . .

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 96

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 97

Matplotlib
What is it?
Object-oriented library for plotting 2D Designed to be similar to the matlab plotting functionality Designed to plot scientic data, built on numpy datastructures

Several Ways to do the Same


python/ipython interactive
> ipython import scipy , matplotlib . pylab x = scipy . randn (10000) matplotlib . pylab . hist (x , 100) > ipython import numpy . random , matplotlib . pylab x = numpy . random . randn (10000) matplotlib . pylab . hist (x , 100)

Version Mania
Numpy, scipy, ipython and matplotlib often used together They somehow depend on each other (e.g. matplotlib uses

arrays from numpy)

Pylab is a (unofcial) package containing all four Depending on your linux distribution, you might have to install

ipython in pylab mode


> ipython - pylab x = randn (10000) hist (x , 100)
Tobias Neckel: Scripting with Python... and beyond 98 Compact Course @ GRS, June 03 - 07, 2013 99

different packages

This changes the module names from which you have to import

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Example - First Plot


partially taken from http://matplotlib.sourceforge.net/users/screenshots.html
from pylab import * x = arange (0.0 , 2* pi , 0.01) y = sin ( x ) plot (x , y , linewidth =4) plot (x , y ) xlabel ( Label for x axis ) ylabel ( Label for y axis ) title ( Simple plot of sin ) grid ( True ) show ()

Example Using Subplots


from pylab import * def f ( t ): s1 = cos (2* pi * t ) e1 = exp ( - t ) return multiply ( s1 , e1 ) t1 = arange (0.0 , 5.0 , 0.1) t2 = arange (0.0 , 5.0 , 0.02) t3 = arange (0.0 , 2.0 , 0.01) show () # gives error but helps ; -) subplot (2 ,1 ,1) # rows , columns , which to show plot ( t1 , f ( t1 ) , go , t2 , f ( t2 ) , k - - ) subplot (2 ,1 ,2) plot ( t3 , cos (2* pi * t3 ) , r . )
Tobias Neckel: Scripting with Python... and beyond 100 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 101

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Example Using Subplots

Example - Histogram
# start ipython - pylab or use imports : # from matplotlib . mlab import * # from matplotlib . pyplot import * from numpy import * mu , sigma = 100 , 15 x = mu + sigma * random . randn (10000) n , bins , patches = hist (x , 50 , normed =1 , \ facecolor = green , alpha =0.75) # add a best fit line y = normpdf ( bins , mu , sigma ) plot ( bins , y , r - - , linewidth =1) axis ([40 , 160 , 0 , 0.03]) plt . show ()

# previous slide continued subplot (2 ,1 ,1) grid ( True ) title ( A tale of 2 subplots ) ylabel ( Damped oscillation ) subplot (2 ,1 ,2) grid ( True ) xlabel ( time ( s ) ) ylabel ( Undamped )

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 102

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 103

More than NumPy?


SciPy depends on NumPy Built to work on NumPy arrays Providing functionality for mathematics, science and engineering Still under development NumPy is mostly about (N-dimensional) arrays SciPy comprises a large number of tools using these arrays SciPy includes the NumPy functionality (only one import

SciPy

necessary)

A lot more libraries for scientic computing are available, some of

them using NumPy and SciPy

Here, just a short overview will be given www.scipy.org for more material (incl. the content of the

following slides)

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 104

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 105

SciPy Organisation - Subpackages


cluster constants fftpack integrate interpolate io linalg maxentropy ndimage odr optimize signal sparse spatial special stats weave Clustering algorithms Physical and mathematical constants Fast Fourier Transform routines Integration and ordinary differential equation solvers Interpolation and smoothing splines Input and Output Linear algebra Maximum entropy methods N-dimensional image processing Orthogonal distance regression Optimization and root-nding routines Signal processing Sparse matrices and associated routines Spatial data structures and algorithms Special functions Statistical distributions and functions C/C++ integration
106

Special Functions
Airy functions Elliptic functions Bessel functions (+ Zeros, Integrals, Derivatives, Spherical,

Ricatti-) Struve functions A large number of statistical functions Gamma functions Legendre functions Orthogonal polynomials (Legendre, Chebyshev, Jacobi,...) Hypergeometric functios parabolic cylinder functions Mathieu functions Spheroidal wave functions Kelvin functions ...

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 107

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Example: Interpolation - Linear

Example: Interpolation - Cubic Spline


import numpy as np import matplotlib . pyplot as plt from scipy import interpolate x = np . arange (0 , 2.25* np . pi , np . pi /4) y = np . sin ( x ) spline = interpolate . splrep (x ,y , s =0) xnew = np . arange (0 ,2.02* np . pi , np . pi /50) ynew = interpolate . splev ( xnew , spline ) plt . plot (x ,y , o , xnew , ynew ) plt . legend ([ Linear , Cubic Spline ]) plt . axis ([ -0.05 ,6.33 , -1.05 ,1.05]) plt . title ( Cubic - spline interpolation ) plt . show ()

import numpy as np import matplotlib . pyplot as plt from scipy import interpolate x = np . arange (0 ,10) y = np . exp ( - x /3.0) f = interpolate . interp1d (x , y ) xnew = np . arange (0 ,9 ,0.1) plt . plot (x ,y , o , xnew , f ( xnew ) , - ) plt . title ( Linear interpolation ) plt . show ()

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 108

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 109

Module random
Compute and use pseudo-random numbers

Part VII Misc Modules, Functionality, and More

from random import * print random () print choice ([1 ,4 , " Hello " ]) (Re)initialize random with seed([x]) (def: time)

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 110

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 111

Module random
Compute and use pseudo-random numbers from random import * print random () print choice ([1 ,4 , " Hello " ]) (Re)initialize random with seed([x]) (def: time) Random oats random () # [0.0 , 1.0) uniform (a , b ) # [a , b ) expovariate ( lambd ) # [0.0 , + inf ) normalvariate ( mu , sigma ) ...

Module random (2)

Random integers randint (a , b ) Random sequences choice ( seq ) sample ( seq , len ) shuffle ( l ) # a <= r <= b

# in place ( list )

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 111

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 112

Running External Programs


Module subprocess Example: call echo in subshell
# !/ usr / bin / python import subprocess as sp p = sp . Popen ( " echo Hello World " , shell = True , stdout = sp . PIPE ) print p . stdout . read ()

Running External Programs


Module subprocess Example: call echo in subshell
# !/ usr / bin / python import subprocess as sp p = sp . Popen ( " echo Hello World " , shell = True , stdout = sp . PIPE ) print p . stdout . read ()

Opens new shell: shell=True Runs command echo Hello World Creates le object (read only) for stdout of shell

Opens new shell: shell=True Runs command echo Hello World Creates le object (read only) for stdout of shell Call directly without shell

p = sp . Popen ([ " echo " , " first param " , " second " ] , stdout = sp . PIPE )

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 113

List of program name and parameters


113

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Module subprocess (2)


Example: Call Gnuplot # !/ usr / bin / python import subprocess as sp p = sp . Popen ([ " gnuplot " , " - persist " ] , stdin = sp . PIPE , stdout = sp . PIPE , stderr = sp . STDOUT ) p . stdin . write ( " plot sin ( x )\ n " ) p . stdin . flush () raw_input ( " Press Enter to continue " ) p . stdin . write ( " plot sin ( x ) , cos ( x )\ n " ) p . stdin . close ()

Operating System Services


Module os Interface to OS services Variables
import os os . environ os . linesep os . name # environment variables , dict # line separation ; \ r \ n win , \ n linux # to find out what system

Some general purpose methods os . getcwd () os . chdir ( path ) os . getgroups () os . uname () os . times () ...
Tobias Neckel: Scripting with Python... and beyond 114 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 115

File objects for stdin and stdout+stderr I/O bufferred (flush() if necessary)

# ( UNIX ) # system information ( UNIX ) # ( usr , sys , c_usr , c_sys , wct )

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Operating System Services (2)


Some methods related to les os . chmod ( path , mode ) os . chmod ( " tmp . py " , 0664) listdir ( path ) mkdir ( path ) makedirs ( path ) rename ( src , dst ) remove ( path ) rmdir ( path ) removedirs ( path ) stat ( path ) # # # # # # # # # # chmod four digits required ls mkdir mkdir -p mv rm rmdir rm - rf stats ( atime , ...)

Operating System Services (3)


Submodule os.path
To manipulate pathnames and check for le properties abspath ( path ) basename ( path ) dirname ( path ) exists ( path ) isfile ( path ); isdir ( path ); islink ( path ) getsize ( path ) getatime ( path ) join ( path1 , path2 , ...)

walk ( path [ , topdown ]) # traverse dir rec . # ( dirpath , dirnames , fnames ) for each dir

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 116

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 117

Operating System Services (4)


Example traversing a directory
# !/ usr / bin / python import os def print_tree ( path ): """ Print the contents of the directory specified in path and all its subdirectories . """ for ( dirpath , dirnames , fnames ) in os . walk ( path ): # get the directory level level = len ( dirpath . split ( os . sep )) -1 # print directory print " | " * level + " + " + os . path . basename ( dirpath ) # print all files for f in fnames : print " | " * level + " | " + f print_tree ( " . " )
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 118

Regular Expressions
Pattern Syntax Regular expressions should always be placed in a raw string E.g. r([^.]*.(.*)) . match any character but newline ^ match start of the string $ match end of the string * match previous expression zero or more times (greedy) + match previous expression one or more times (greedy) ? match previous expression zero or one time (greedy) *?, +?, ?? non-greedy versions of *, +, ? {m} match previous expression m times {m,n} match previous expression m to n times (greedy) {m,n}? non-greedy version of {m,n} [...] match any character from the enclosed set [^...] match any character not in the set A|B matches A or B (both regular expressions) (...) stores the contents of the match inside the backets
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 119

Character Escape Sequences


Characters with special meaning (., *, ...) can be used with their literal meaning by escaping them with a \, e.g. \., \* Standard escape characters (\n, \t, ...) work as expected, e.g. r\n+ matches one or more newlines

Regular Expressions (cont.)


findall(patt, string) nd all non-overlapping matches sub(patt, repl, string) replaces matches by repl from re import * regstr = r \ d *\.\ d *|\ d * s = " 12.3/5/4.4;5.7;6 " findall ( regstr , s ) regstr = r (\ d *\.\ d *|\ d *) sub ( regstr , r \1 xxx , s ) greedy vs. non-greedy reg_greedy = r <.* > s = " <H1 > title </ H1 > " m_g = search ( reg_greedy , s ) # whole <H1 > title </ H1 > matched reg_nongreedy = r <.*? > # match as few chars as possible m_ng = search ( reg_nongreedy , s ) # only <H1 > matched print m_g . group () + " , " + m_ng . group ()
Tobias Neckel: Scripting with Python... and beyond 120 Compact Course @ GRS, June 03 - 07, 2013 121

\number \d \D \s \S \w \W \A \Z

match the text matched by group number (starting from 1) same as [0-9] same as [^0-9] matches any whitespace (same as [\t\n\r\f\v] matches nonwhitespace matches any alphanumeric character matches nonalphanumeric characters matches the start of a string matches the end of a string

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Match Objects
Some re functions do not return strings, but match objects (m) match(patt, string) returns MatchObject in case of match search(patt, string) searches for rst match finditer(patt, string) like ndall, but returns iterator m.group([group1, group2]) returns subgroups of the match import re s = " dates 05/02/2010 - 05/14/2010 " patt = r (\ d +)/(\ d +)/(\ d +) for m in finditer ( patt , s ): print m . group () print ( " %s -% s " % ( m . group (2) , m . group (3)))

Exceptions: Dealing with Errors


Consider the user enters characters: x = raw_input ( " Please enter a number : " ) print float ( x )

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 122

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 123

Exceptions: Dealing with Errors


Consider the user enters characters: x = raw_input ( " Please enter a number : " ) print float ( x ) Failed conversion raises an ValueError exception Please enter a number : xyz ... Traceback ( most recent call last ): File " < stdin > " , line 1 , in < module > ValueError : invalid literal for float (): xyz To deal with erroneous behaviour catch exception and handle it

Exceptions: Dealing with Errors (2)


Syntax is try : # do something except ValueError , e : # Py >3.0: " Exception as e " # handle ValueError except Exception , e : # handle all other errors else : # what to do if no error occured finally : # code that should be executed in any case

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 123

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 124

Exceptions: Dealing with Errors (3)


Back to the previous example got_answer = False while not got_answer : x = raw_input ( " Please enter a number : " ) try : print float ( x ) got_answer = True except ValueError , e : print " That wasn t a number " # do something with e

Exceptions: Dealing with Errors (3)


Back to the previous example got_answer = False while not got_answer : x = raw_input ( " Please enter a number : " ) try : print float ( x ) got_answer = True except ValueError , e : print " That wasn t a number " # do something with e To raise your own errors (use one of many default types or create

your own):

raise RuntimeError ( " Ooops ! Something went wrong ! " ) raise IOError ( " File not existent ... " )
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 125 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 125

Built-in Exceptions
BaseException # GeneratorExit Key boardInterrupt # SystemExit # Exception # StopIteration StandardError # ArithmeticError FloatingPointE rror ZeroDevisionError AssertionError AttributeError EnvironmentError IOError # OSError EOFError ImportError #
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 126

root of all exc . Ctrl +C , e . g . Program Exit ( sys . exit ()) Base for non - exit exc . only Python 2. x

open (" file . txt ") , e . g .

modul not found , e . g .

LookupError IndexError # tupels / lists , e . g . KeyError # dictionaries , e . g . MemoryError NameError # name not found Unbou nd ed Lo calError ReferenceError RuntimeError NotIm pl em en tedError SyntaxError IndentationError TabError SystemError TypeError # type error (2 + "3") ValueError # value errorr ( float (" three ")) UnicodeError UnicodeDec odeErr or UnicodeEnc odeErr or Un i co de T ra ns late E rr or
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 127

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Throw Built-in Exceptions


def readfloat1 (): while True : try : a = raw_input ( " Number between 0.0 and 1.0: " ) a = float ( a ) if (a <0.0 or a >1.0): raise ValueError ( " Number not in [0.0; 1.0] " ) except ValueError as e : print " Error : % s " % e else : return a At a = float(a), an error is thrown automatically; the rest of the

Module pickle
Serializing Python objects Save objects as they are (current state) to le
import pickle fd = open ( myObjects , wb ) pickle . dump (x , fd ) pickle . dump (y , fd ) fd . close () Most objects can be pickled Not valid for objects such as opened les

try Block is not being executed is possibly thrown

If a convertable, the value range is checked and a new exception


Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 128 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 129

Module pickle
Serializing Python objects Save objects as they are (current state) to le
import pickle fd = open ( myObjects , wb ) pickle . dump (x , fd ) pickle . dump (y , fd ) fd . close () Most objects can be pickled Not valid for objects such as opened les Then load in same order fd = open ( myObjects , rb ) x = pickle . load ( fd ) y = pickle . load ( fd ) fd . close ()
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 129

Misc: Python 2 to 3
Which version to use? See http://wiki.python.org/moin/Python2orPython3 for hints Converting from Python 2.x to Python 3.x
Convert your code to Python 2.6 To check, there is a Py3k warnings mode python -3 scriptname . py

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 130

Misc: Python 2 to 3
Which version to use? See http://wiki.python.org/moin/Python2orPython3 for hints Converting from Python 2.x to Python 3.x
Convert your code to Python 2.6 To check, there is a Py3k warnings mode python -3 scriptname . py If no warnings remain, use 2to3 tool

Misc: Python 2 to 3
Which version to use? See http://wiki.python.org/moin/Python2orPython3 for hints Converting from Python 2.x to Python 3.x
Convert your code to Python 2.6 To check, there is a Py3k warnings mode python -3 scriptname . py If no warnings remain, use 2to3 tool

Ships with Python Converts many things automatically Cant do everything Rest manually

Ships with Python Converts many things automatically Cant do everything Rest manually Further reading: http://docs.python.org/release/3.0.1/library/2to3.html

Tobias Neckel: Scripting with Python... and beyond 130 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 130

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

Misc: Python 2 to 3 (2)


Some examples
print " Hello " , " World " 1234567890 L a <> b dictionary . has_key ( x ) try : import something except ImportError , e : pass -> -> -> -> print ( " Hello " , " World " ) 1234567890 a != b x in dictionary

Misc: Python 2 to 3 (2)


Some examples
print " Hello " , " World " 1234567890 L a <> b dictionary . has_key ( x ) try : import something except ImportError , e : pass -> -> -> -> print ( " Hello " , " World " ) 1234567890 a != b x in dictionary

-> except ImportError as e :

-> except ImportError as e :

Some modules (urllib, httplib, . . . ) renamed or restructured More views, less lists map ( lambda x : x +1 , l ) filter ( lambda x : x >3 , l ) xrange (10) range (10)
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 131 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 131

-> -> -> ->

list ( map ( lambda x : x +1 , l )) list ( filter (...)) range (10) list ( range (10))

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Misc: execle/exec
Run commands
Run/execute commands in current namespace exec ( " a = 3+7 " ) exec ( " print Hello " ) Run/execute commands from le execfile ( filename ) execfile corresponds to run in ipython

Extending Python using C/C++

Performance critical parts can be written in hardware-dependent

code

Use existing functionality written in other languages

Use as module

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 132

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 133

Extending Python using C/C++

Extending Python using C/C++ (2)


Example: /* example . cpp */ # include " example . hpp "

Performance critical parts can be written in hardware-dependent

code

Use existing functionality written in other languages

Use as module
Well built an example, using C++ and swig Provides two functions, computing mod and factorial

int mod ( int x , int y ) { return ( x % y ); } int fact ( int n ) { if ( n <= 1) return 1; else return n * fact (n -1); } /* example . hpp */ // ... int mod ( int x , int y ); int fact ( int n ); // ...

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 133

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 134

Extending Python using C/C++ (3)


Additionally, we need to care about

Extending Python using C/C++ (3)


Additionally, we need to care about

Data conversion from C++ to Python Writing both wrapper and interface les

Data conversion from C++ to Python Writing both wrapper and interface les Fortunately, swig (Simplied Wrapper and Interface Generator) does everything for us We only need a .i le:

/* tools . i */ % module tools %{ /* Put header files here ... */ # include " example . hpp " %} /* Parse the header file to generate wrappers */ % include " example . hpp "

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 135

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 135

Extending Python using C/C++ (4)


Now, weve only got to run swig swig -o tools_wrap . cpp -c ++ - python tools . i

Extending Python using C/C++ (4)


Now, weve only got to run swig swig -o tools_wrap . cpp -c ++ - python tools . i

creating tools.py and tools_wrap.cpp . . .

creating tools.py and tools_wrap.cpp . . . . . . and compile everything as a shared library


g ++ -o tools_wrap . os -c -I / usr / include / python2 .6 \ tools_wrap . cpp - fPIC g ++ -o example . os -c example . cpp - fPIC g ++ -o _tools . so - shared example . os tools_wrap . os

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 136

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 136

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Extending Python using C/C++ (4)


Now, weve only got to run swig swig -o tools_wrap . cpp -c ++ - python tools . i

Extending Python using C/C++ (5)

creating tools.py and tools_wrap.cpp . . . . . . and compile everything as a shared library


g ++ -o tools_wrap . os -c -I / usr / include / python2 .6 \ tools_wrap . cpp - fPIC g ++ -o example . os -c example . cpp - fPIC g ++ -o _tools . so - shared example . os tools_wrap . os Then use in Python: import tools print tools . mod (8 , 6) print tools . fact (6)
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 136

Coments
Using global variables is a little more tricky Functions can be conveniently renamed in the .i le % rename ( __str__ ) DataVector :: toString ;

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 137

Time
Module time Functions to access and display (the current) timestamp Without parameter: use current time
from time import * time () # ctime ([ secs ]) # localtime ([ secs ]) # gmtime ([ secs ]) # # Y, M, D, H, M, S, sleep ( secs ) # secs since 01/01/1970 string ( local time ) struct ( local time ) struct ( GMT ) Wday , Yday , DST (1= y ,0= n , -1=?) pause curr . process

Time
Module time Functions to access and display (the current) timestamp Without parameter: use current time
from time import * time () # ctime ([ secs ]) # localtime ([ secs ]) # gmtime ([ secs ]) # # Y, M, D, H, M, S, sleep ( secs ) # secs since 01/01/1970 string ( local time ) struct ( local time ) struct ( GMT ) Wday , Yday , DST (1= y ,0= n , -1=?) pause curr . process

Measure time (up to 1/100s, system clock dependent) t = time () for i in range (10000): # do something t = time () - t print t , " seconds "
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 138 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 138

Graphical User Interface


GUI
There are lots of GUI toolkits for Python Most common: TKinter

Graphical User Interface


GUI
There are lots of GUI toolkits for Python Most common: TKinter

Module Tkinter
Example: Simple window with title # !/ usr / bin / python import Tkinter as tki root = tki . Tk () root . title ( " Hello to all ! " ) tki . Label ( root , text = " Welcome here " , font = " times 32 bold " ). pack () root . mainloop ()
mainloop()
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 139 Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat

runs until some event exits


139

Tobias Neckel: Scripting with Python... and beyond

Module Tkinter (2)


A more useful example with a button, allowing to quit # !/ usr / bin / python import Tkinter as tki from tkMessageBox import askokcancel import sys def quit (): aoc = askokcancel ( " Exit " , " Really quit ? " ) if aoc : sys . exit () root = tki . Tk () root . title ( " What a nice window ... " ) b = tki . Button ( root , text = " Press me ! " , font = " times 24 normal " , command = quit ) b . pack () root . mainloop ()
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 140

Animation with Tkinter


# ! / usr / bin / python from Tkinter import * import time , math cv = Canvas ( width =800 , height =600) cv . pack () oval = cv . create_oval (300 , 50 , 400 , 150) cv . itemconfig ( oval , fill = blue ) i = 0.0 while True : cv . move ( oval , 10* math . cos ( i ) , 10* math . sin ( i )) cv . update () time . sleep (0.02) i += 0.05

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 141

Munchen Scientic Computing in Computer Science, Technische Universitat

Munchen Scientic Computing in Computer Science, Technische Universitat

Compressing les
There are modules for tar, zip, bz2, gz, . . .

Compressing les
There are modules for tar, zip, bz2, gz, . . .

Example: module gzip Simple to use: just replace standard call to open
import gzip fd = gzip . open ( " file . txt . gz " , " w " ) fd . write ( """ Funny lines in gzip file """ ) fd . close () fd = gzip . open ( " file . txt . gz " ) print fd . read () fd . close ()

. . . as easy as that File modes as usual (rwa + b for binary)


Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 142 Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 142

More Python Fun :-)


Web server in 3 lines?
import SimpleHTTPServer , SocketServer httpd = SocketServer . TCPServer (( " " , 8000) , \ SimpleHTTPServer . S i mp l e H TT P R eq u e s tH a n d le r ) httpd . serve_forever ()

More Python Fun :-)


Web server in 3 lines?
import SimpleHTTPServer , SocketServer httpd = SocketServer . TCPServer (( " " , 8000) , \ SimpleHTTPServer . S i mp l e H TT P R eq u e s tH a n d le r ) httpd . serve_forever () What could this be good for?

Want to share quickly some les with colleagues in the same network? Goto directory, start python, run three lines, tell them your IP and the port (here: 8000) Thats it!

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 Munchen Scientic Computing in Computer Science, Technische Universitat 143

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 143

import sys sys.exit(0)

Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 144

Você também pode gostar