Python 2x4

Embed Size (px)

Citation preview

  • 8/13/2019 Python 2x4

    1/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Scientific Computing in Computer Science,Technische Universitat Munchen

    Scripting with Python... and beyond

    Compact Course @ GRS

    Tobias Neckel

    June 03 - 07, 2013

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 1

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Part I

    Python Overview

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 2

    Scientific Computing in Computer Science, Technische Universitat Munchen

    What Python is all about. . .

    Python Invented in 1990 as teaching language

    Designed for clearness and readability

    Named after Monty Python (not the snake)

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 3

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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, HPCmachines 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 4

    Scientific Computing in Computer Science, Technische Universitat Munchen

    What Python is all about. . .

    Suitable for

    OS scripting (Python beats Bash ;-))

    Internet Programming

    Scientific Computing (up to whole Tsunami Simulations)

    Parallelisation

    GUI (Tkinter)

    Visualisation

    Rapid prototype development

    . . . and much, much more!

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 5

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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 6

    Scientific Computing in Computer Science, Technische Universitat Munchen

    What Python is all about. . .

    Some more features

    Dynamically typed

    Automatic garbage collection

    Different programming paradigmes possible (procedural, OO,

    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

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 6

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Requirements

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

    IPython (callipython)

    NumPy and SciPy

    Pylab (as it ships with Matplotlib)

    Tkinter

    swig (Simplified Wrapper and Interface Generator) independent of Python

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 7

  • 8/13/2019 Python 2x4

    2/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Part II

    Hands On. . .

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 8

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Lets get started. . .

    Start the Python interpreter

    At prompt >>> type command, press to confirm

    python

    P y t ho n 2 . 7 .3 ( d e fa u lt , A u g 1 2 0 12 , 0 5 : 14 : 3 9)

    [ G C C 4 . 6 . 3 ] o n l i n ux 2

    T y pe " h e l p " , " c o p y r ig h t " , " c r e d i t s " o r " l i c e ns e " f o r

    mo re in fo rm at io n .

    > > > h e lp

    T y pe h e lp ( ) f o r i n t e r a c ti v e h el p , o r h e lp ( o b j e ct ) f o r

    h e lp a b ou t o b j ec t .

    > > > h e lp ( )

    W el c om e t o P yt h on 2 .7 ! T hi s i s t h e o n l in e h e l p

    u t i l i t y . [ . . . ]

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 9

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Getting help

    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

    Literature

    David M. Beasley: Python - Essential Reference,Addison-Wesley Professional, 4th edition, 2009

    Hans Petter Langtangen: A Primer on ScientificProgramming with Python, Springer, 2009

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 10

    Scientific Computing in Computer Science, Technische Universitat Munchen

    A first Hello World!

    Python simplify your Code

    Just type

    >> > print " H e l l o W o r l d ! "

    H e l lo W o rl d !

    Compare with Java, e.g.:

    p u b li c c l a ss H e l l o {

    p u b li c s t a ti c v o id m a in ( S t r i n g a r g v [ ] ) {

    S y s t e m . o u t . p r i n t l n ( " H e l l o W o r l d ! " );

    }

    }

    j a v a c H e l l o . j a v a

    j a va H e l lo

    H e l lo W o rl d !

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 11

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Statements and assignments

    > > > 3 + 4 * * 2 / 8 + 1

    > > > ( 6 * 3 . 5 ) * 2 - 5

    > > > 1 0 e -4

    > > > a = 1 7

    > >> a

    > > > a - 1 .5

    > > > b = " H e l l o W o r l d ! "

    >> > print b

    >> > print a , b , ( 4 + 5 ** 0 . 5 )

    assignment: variableName = expression (different to math. =)

    Interactive session shows return values

    printprints variables and expressions as string

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 12

    Scientific Computing in Computer Science, Technische Universitat Munchen

    3.5 Ways to Python

    1. Start the Python interpreter with python

    2. Use IPython: ipython

    3. Run Python on a file

    3.5. Execute as a script

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 13

    Scientific Computing in Computer Science, Technische Universitat Munchen

    2. IPython

    i p y t h o n

    P y t ho n 2 . 7. 3 ( d e fa u lt , A u g 1 2 0 12 , 0 5 : 14 : 3 9)

    T y pe " c o p y ri g h t " , " c r e d i ts " o r " l i c en s e " f o r m o re

    i n f o r m a t i o n .

    I P y th o n 0 . 1 3. 2 . r c 2 - - A n e n h an c e d I n t e ra c t iv e P y t ho n .

    ? -> I nt ro du cti on and ov er vi ew of IPython s

    f e a t u r e s .

    % q u i c kr e f - > Q u ic k r e f er e n ce .

    h el p - > P yth on s o wn he lp sy st em .

    o b je c t ? - > D e t ai l s a b o ut o bj ec t . ? o b j ec t a ls o

    w o rk s , ? ? p r i nt s m o re .

    I n [ 1 ] : p r i nt " H e l lo W o r ld ! "

    H e l lo W o rl d !

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 14

    Scientific Computing in Computer Science, Technische Universitat Munchen

    2. IPython

    Highlights

    Enhanced interactive shell

    Additional shell syntax

    magic functions, starting with % such as %psearchtosearch for function in namespace

    Access shell commands %cd Run scripts with run filename.py Log session, define 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 15

  • 8/13/2019 Python 2x4

    3/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    3. Run Python on a File

    Filesquare_me.py:

    print 6**2

    Then run

    p y t h o n s q u a r e _ m e . p y

    36

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 16

    Scientific Computing in Computer Science, Technische Universitat Munchen

    3.5. Execute as a Script

    Shell script as before

    Just tell shell to execute python:

    # ! / u s r / b i n / p y t h o n

    print 6**2

    Then set executable flag and run

    c h m od u + x s q u ar e _ me . p y

    . / s q u a r e _ m e . p y

    36

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 17

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Part III

    Data Types and Control Structures

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 18

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Built-in Python Types

    Numeric Types bool boolean type

    print T r ue , F a ls e

    T r ue = = T r u e

    T r ue ! = T r u e

    int integer

    1+5-12

    Attention integer division: digits after the comma get lost!

    > > > 5 / 3

    1

    long arbitrary-precision integer

    1 2 3 4 5 * * 1 0 0

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 19

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Numeric Types (cont.) float floating point (double precision)

    3 . 1 4 1 5 9 2 6 5 * 5 . 6

    1 . 5 e - 1 0

    complex complex numbers

    1 + 4 j

    c = c o m pl e x ( 2 , 3)

    print c

    d = 2 * ( c * * 3 - 3 j )

    print d

    d . r e a l

    d . i m a g

    None type None special type (undefined)

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 20

    Scientific Computing in Computer Science, Technische Universitat Munchen

    General Remarks Everything is an object (int, float, tuple, classes, . . . ) Thus everything can have attributes and methods

    c = c o m pl e x ( 2 , 3)

    print c . r e al , c . i m ag

    Dynamic typing: Type of a variable determined duringassignment

    Automatic conversion where necessary and possible

    a = 1 23 4

    t y p e ( a )

    b = 1 0 0

    t y p e ( b )

    a = 1 2 34 * *1 0 0

    t y p e ( a )

    b = b * 1. 0 # eq u al s b = fl o at ( b ) *1 .0

    t y p e ( b )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 21

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Built-in Sequences

    Strings stringtype Sequence of characters Single, double or triple double (multiline) quotes

    " . . . t h e r e l i v e d a h o b b i t . "

    " a h o b b i t "

    a " h o b b i t "

    a \ h o b b i t \ " "" I n a h ol e i n t he g r ou n d

    t h er e " l i v ed " a h o b bi t " " "

    Concatenation

    s1 = " I n a h o l e i n t h e g r o u n d "

    s2 = " t h e r e l i v e d a h o b b i t "

    s = s 1 + + s2

    print s

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 22

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Built-in Sequences Strings (2)

    Replication

    " H i ! " *3

    Indexing and Slicing (immutable sequence type)

    s = " H e l l o H o b b i t "

    s [ 3 ]

    s [ - 2 ]

    s [ 2 : 4 ]

    s [ 6 : ]

    s [ : 6 ]

    s [ 0 : - 1 : 2 ] # s tr i de 2

    s [ 0 : : 2 ]

    s [2] = 4 # e r r or

    l e n ( s )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 23

  • 8/13/2019 Python 2x4

    4/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Built-in Sequences Strings (3)

    String formatting using %

    " B i l b o s % d . b i r t h d a y " % ( 1 11 )

    " F r o d o , % s , a n d B i l b o a t e % f a p p l e s " %( " S a m " ,1.5)

    Some format modifiers

    % 0 5 d % ( 13 ) # f i l l 0

    % - 5 d % ( 13 ) # l e ft j u s ti f i ed

    % + 5 d % ( 13 ) # s ig n

    % d % ( 1 3) # s ig n ed i n t eg e r

    % e % ( 1 3) # f l o a ti n g p o in t e x p o n en t i a l

    % g % g , % f % ( 13 , 1 e -6 , 1 e -6 )

    % s % ( h i ) # s t r in g

    % % # e s ca pe %

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 24

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Built-in Sequences

    Tuples

    Sequences of arbitrary objects (,)

    Immutable

    Indexing and slicing as before

    l = ( 1 , )

    l = ( 1 , " t w o " , ( 3 , 4 , 5) )

    l [ 0 ]

    l [ - 1 ]

    l [1] = 2 # e r r or

    l e n ( l )

    mi n (l ) # h e re : n u m be r s s m al l e r th a n s tr i n gs

    l = ( " H e l l o " , " H o b b i t " )mi n (l )

    ma x (l )

    l 2 = l + ( 6 ,7 ,8 )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 25

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Built-in Sequences

    Lists

    Sequences of arbitrary objects [,]

    Mutable

    l = [ 1 , " t w o " , ( 3 ,4 , 5) , 5 ]

    l [ 0 ]l [ - 1 ]

    l [1 ] = 2 # w o rk s !

    l e n ( l )

    Operations on lists

    l = [ 0 , 1 , 2 ,3 , 4 , 5 , 6 ,7 , 8 , 9]

    l [ 2: 4 ] = [ 10 , 1 1]

    l [ 1 :7 : 2] = [ -1 , - 2, - 3]

    de l l [ : : 2 ]

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 26

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Built-in Sequences

    Lists (2) List methods

    l = [ 0 ,1 , 2 ,3 ]

    l . a p p e n d ( " f o u r " )

    l . e x t e n d ( [ 5 , 6 , 7 , 8 , 9 ] )

    l . i n s e r t ( 8 , " f o u r " )

    l . c o u n t ( " f o u r " )

    l . s o r t ( )

    l . r e v e r s e ( )

    l . r e m o v e ( " f o u r " )

    l . p o p ( )

    Integer ranges: rangefunction

    r a n g e ( 1 0 )

    r a n ge ( 5 , 1 0 )

    r a n ge ( - 1 0 , 2 0 , 3 )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 27

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Control FlowGeneral

    Indentation used to group blocks!

    Code within same block has to be same level

    Needs getting used to, but encourages readable code

    if-statement

    if x < y :

    print " x i s s m a l l e r "

    print x

    elif x = = y :

    print " b o t h a r e e q u a l "

    else :

    print " y i s s m a l l e r "

    print y

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 28

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Comparison and Equality

    Check for equality

    " B i l b o " == " F r o d o "

    " B i l b o " != " F r o d o "

    Comparison

    " B i l b o " < " F r o d o "

    " B i l b o " > " F r o d o "

    " B i l b o " = " F r o d o "

    isand in

    " B i l b o " is " F r o d o " # o b j ec t i d e nt i t y

    " B i l b o " in [ " F r o d o " , " S a m " , " B i l b o " ] # m e m b er s h i p

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 29

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Comparison and Equality (2)

    Combining and grouping (and, or,not)

    if ( " B i l b o " != " F r o d o " a n d n o t (1 > 2 or 2 > 3 )) :

    print " S a m w a s h e r e "

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 30

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Control Flow - for

    Pythons for iterates over the items of a sequence type

    fo r i in r a n g e ( 5 , 1 0 ) :

    print " l o o p i n d e x : " , i

    a = ( "a " , " t u p l e " , " w i t h " , 5, " w o r d s " )

    fo r i in a:

    print i

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 31

  • 8/13/2019 Python 2x4

    5/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Control Flow - break and while

    breakbreakes out of the enclosing loop (foror while) continuecontinues with the lext iteration of the loop

    a = ( " l e t " , " u s " , " f i n d " , " B i l b o " , " a g a i n " )

    fo r word in a :

    if w o rd = = " B i l b o " :

    print " f o u n d B i l b o "

    break

    print word

    whileloops until condition isFalse

    a = 2 04 8; ex p = 0

    while a > 1:

    a / = 2 # a = a / 2

    exp+=1 # exp = exp + 1

    print a , "= " , 2, "^ " , e xp

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 32

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Functions (and Procedures)

    Functions are defined using def No return statement means returning None Call by reference (Note: Assignment creates a new local object)

    de f h i ( ) :

    print " H e l l o W o r l d ! "

    de f f a c t o r i a l ( n ) :

    f a c = 1

    fo r i in r a n g e ( 2 , n + 1 ) :

    f ac = f ac * i

    return fa c

    Functions are objects and can be assigned, too

    f = f a ct o ri a l

    f ( 3 )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 33

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Functions (2)

    Default arguments

    de f p r i n t m e ( s = " F r o d o " ):

    print s

    p r i n t m e ( )p r i n t m e (" B i l b o " )

    Keyword arguments

    de f e xp ( b a s i s , e x p o n e n t ) :

    return b a s i s * * e x p o n e n t

    e x p ( b a s i s = 2 , e x p o n e n t = 1 0 )

    e x p ( e x p o n e n t = 1 0 , b a s i s = 2 )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 34

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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 35

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Part IV

    More on Python

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 36

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on StringsSpecial string methods (excerpt)

    s = " F r o d o a n d S a m a n d B i l b o "

    s . i s l o w e r ( )

    s . i s u p p e r ( )

    s . s t a r t s w i t h ( " F r o d o " ) # t r y s . s t a r t s w it h ( " F r o do " , 2 )

    s . e n d s w i t h ( " B i l b o " )

    s = s . s tr i p ()

    s . u p p e r ( )

    s = s . l ow e r ()

    s . c a p i t a l i z e ( ) # c a p it a l i ze f i r st c h a ra c t er

    s = s . c e n te r ( l e n ( s )+ 4 )# c e n te r ( p a d di n g d e f au l t : s p a c e )

    s . l s t r i p ( )

    s . r s t r i p ( " " )

    s = s . s tr i p ()

    s . f i n d ( " s a m " )

    s . r f i n d ( " a n d " )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 37

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on Strings (2)

    Searching, splitting and joining (excerpt)

    s . f i n d ( " s a m " )

    s . r f i n d ( " a n d " )

    s . r e p l a c e ( " a n d " , " o r " )

    s . s p l i t ( ) # o r s . s p l it ( N o n e ) ; a r b i tr a r y n u m b . w h i t e sp .

    s . s p l i t ( " " )

    s . s p l i t ( " a n d " )s . s p l i t ( " a n d " , 1)

    s = " " " L i n e

    by

    L i n e " " "

    s . s p l i t l i n e s ( )

    " , " . j o i n ( [ " s e q u e n c e " , " o f " , " s t r i n g s " ])

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on Built-in Sequences

    Dictionaries

    Map keys to values, arbitrary types

    Only immutable objects as keys

    Can serve as database

    d = {}

    d[ " B i l b o " ] = " H o b b i t "

    d[ " E l r o n d " ] = " E l f "d [ 42 ] = 1 23 4

    d . h a s _ k e y ( " B i l b o " )

    d . k e y s ( ) # u n o rd e r e d

    d . i t e m s ( )

    d . v a l u e s ( )

    de l d [ 4 2 ]

    d . c l e a r ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 39

  • 8/13/2019 Python 2x4

    6/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on Functions

    Functions have to be defined before actually called for the firsttime

    Note: Python is an interpreted language

    Variables

    Variables defined or assigned to in functions have local scope

    Global variables (surrounding context) can be accessed

    To modify global variables use global

    c ou nt = 0

    .. .

    de f c o u n t _ i t ( n ) :global count

    c o un t + = 1

    In general: avoid global variables (pass as params instead)

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 40

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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

    >> > de f a d d _ o n e ( x ) :

    > >> x = x + 1

    >> >

    > >> x = 3

    > > > a d d _o n e ( x )

    > >> x

    3

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 41

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Call by Reference vs. Call by value (2)

    After assignment: local variable references to a new object.

    Using methods, the original object can be modified.

    >> > de f a p p e n d 1 ( l ) :>> > l = l + [4]

    >> >

    >> > de f a p p e n d 2 ( l ) :

    > >> l . ap pe nd ( 4)

    >> >

    > > > l = [ 1 ,2 , 3]

    > > > a p p e n d 1 ( l ); l

    [ 1 , 2 , 3 ]

    > > > a p p e n d 2 ( l ); l

    [ 1 , 2 , 3 , 4 ]

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 42

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on Functions (2)

    Return values

    Functions can have multiple return values (returned as tuple)

    de f g e t _ h o b b i t s ( ) :

    return " B i l b o " , " F r o d o "h = g e t _ ho b b i ts ( )

    ( a , b ) = g e t _h o b b it s ( )

    Assignment rules correspond to slice assignment

    l = [ 4 , 2 ]

    ( a , b ) = l # i m p li c i t t y pe c o n v er s i o n

    a , b = l

    l [ 0 :3 ] = ( 3 , 2 ) # i m p li c i t t y pe c o n v er s i o n

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 43

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on Functions (3)

    Docstrings

    Functions (and classes, modules, ...) can provide help text

    String after function header (can be multiline)

    de f a n s w e r ( ) :

    " R e t u r n s a n a n s w e r t o a q u e s t i o n "

    print " a n s w e r "

    h e l p ( a n s w e r )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 44

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Arithmetic Operations

    Classical operations

    x + y # A d d it i o n

    x - y # S u b t ra c t i on

    x * y # M u l t ip l i c at i o n

    x / y # D i v is i o n

    x / / y # T r u nc a t ed d i v i s io n

    x * * y # E xp o n e nt i a t io n

    x % y # M o d ul o

    x -= 2 ; x *= 4; .. .

    Division differs for int and float(Python < 3.0)

    7/ 4

    7.0/4

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 45

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More Operations

    General functions

    a b s ( x )

    d i v mo d ( x , y ) # ( x // y , x % y )

    p o w (x , y [ , m o d ul o ] ) # x * * y % m o d ul o

    r o u nd ( x , [ n ] ) # r ou n d t o 1 0* *( - n )

    Operations on integers

    x < < y # L ef t sh i ft

    x > > y # R ig ht s h if t

    x & y # B i t wi s e a n d

    x | y # B it w is e o r

    x ^ y # B i t wi s e x o r

    ~x # B i t wi s e n e g at i o n

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 46

    Scientific Computing in Computer Science, Technische Universitat Munchen

    File I/O

    Opening files

    Create file object (text files)

    f d = o pe n (" t e s t f i l e . t x t " ) # r ea d

    f d = o pe n (" t e s t f i l e . t x t " , r ) # r e ad

    f d = o pe n (" t e s t f i l e . t x t " , w ) # w r i te

    f d = o pe n (" t e s t f i l e . t x t " , a ) # a p p e nd

    Create file object (binary files)

    f d = o pe n (" t e s t f i l e . t x t " , r b ) # r e ad

    f d = o pe n (" t e s t f i l e . t x t " , w b ) # w r i te

    f d = o pe n (" t e s t f i l e . t x t " , a b ) # a p p e nd

    openhas more options (encoding, how to handle newlines, . . . )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 47

  • 8/13/2019 Python 2x4

    7/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    File I/O (2)

    Methods of file objects

    Reading

    f d . r e a d ( ) # a ll

    f d . r e a d ( n ) # n B yt es

    f d . r e a d l i n e ( ) # o n e l in e

    f d . r e a d l i n e s ( ) #

    Writing

    f d . w r i t e ( " N e w T e x t \ n " )

    f d . w r i t e l i n e s ( [ " f i r s t l i n e \ n " , " S e c o n d \ n " ])

    Dont forget to write newlines

    Closing

    f d . c l o s e ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 48

    Scientific Computing in Computer Science, Technische Universitat Munchen

    File I/O (3)

    Iterating over textfile

    forloop over file line by line

    f d = o pe n (" f u l l t e x t . t x t " )

    fo r line in fd : # r at h e r th a n : i n fd . r e a d li n e s ( )

    print line

    # o r:

    while T r u e :

    l i ne = f d . r e a dl i n e ( )

    i f n ot l i n e :

    break

    print line

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 49

    Scientific Computing in Computer Science, Technische Universitat Munchen

    File I/O (3)

    Iterating over textfile

    forloop over file line by line

    f d = o pe n (" f u l l t e x t . t x t " )

    fo r line in fd : # r a th e r t ha n : in f d . r ea d l in e s ( )

    print line# o r:

    while T r u e :

    l i ne = f d . r e ad l i ne ( )

    i f n ot l i n e :

    break

    print line

    Some more functions on file objects

    fd.tell() get current file position

    fd.seek(offset) set file to position

    fd.flush() flush output buffer. Note: buffered for efficiency

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 49

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Getting more. . .Modules

    Outsource functionality in separate.py files Import them as library module Example (tools.py):

    " " " T hi s m o du l e p r o vi d es s o me h e lp e r t o ol s .

    T r y i t . " " "

    c o un t e r = 4 2

    de f r e a d f i l e ( f n a m e ) :

    " R e a d t e x t f i l e . R e t u r n s l i s t o f l i n e s "

    f d = o p en ( f n am e , r )

    d a t a = f d . r e a d l i n es ( )

    f d . c l o s e ( )

    return data

    de f d o _ n o t h i n g ( ) :

    " D o r e a l l y n o t h i n g "

    pass

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 50

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Modules (2) Import module

    import tools

    t o o l s . d o _ n o t h i n g ( )

    print t o o l s . c o u n t e r

    Import module and change name

    import t o ol s a s t

    t . d o _ n o t h i n g ( )

    Import selected symbols to current namespace

    from tools import d o _ n o t hi n g , r e a d f i l e

    from tools import c o u nt e r a s c n tr

    d o _ n o t h i n g ( )

    print cntr

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 51

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Modules (3) Import all symbols to current namespace

    from tools import *

    d o _ n o t h i n g ( )

    print c o u n t e r

    Modules can control which symbols are imported byfrom module import *:

    # m o d ul e t o o ls . p y

    _ _ al l_ _ = [ r e a d f i l e , c o u n t e r ]

    Then do_nothing()is unknown after import *

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 52

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Modules (3)

    Import all symbols to current namespace

    from tools import *

    d o _ n o t h i n g ( )

    print counter

    Modules can control which symbols are imported byfrom module import *:

    # m o d ul e t o ol s . p y

    _ _ al l __ = [ r e a d f i l e , c o u n t e r ]

    Thendo_nothing()is unknown after import *

    Inspect namespace Inspect namespace of module with

    d i r ( t o o l s )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 52

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Modules (4)

    Getting help

    Access docstrings

    import tools

    h e l p ( t o o l s )

    h e l p ( t o o l s . d o _ n o t h i n g )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 53

  • 8/13/2019 Python 2x4

    8/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Modules (4)

    Getting help

    Access docstrings

    import tools

    h e l p ( t o o l s )

    h e l p ( t o o l s . d o _ n o t h i n g )

    Execute module as main program

    tools.pyshould serve as program and module

    # t o o ls . p y

    .. .

    if _ _ n a me _ _ = = _ _ m a i n _ _ :print " t o o l s . p y e x e c u t e d "

    else :

    print " t o o l s . p y i m p o r t e d a s m o d u l e "

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 53

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Modules (5)

    Reload module

    When debugging a module reload with reload(Python

  • 8/13/2019 Python 2x4

    9/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Standard Input, Output, and Error

    Module sys provides three standard file objects

    sys.stdin read only sys.stdout,sys.stderr write only

    import sy s

    s y s . s t d o u t . w r i t e ( " E n t e r l i n e : \ n " ) # = p r in t " . . ."

    l i ne = s y s . s td i n . r e ad l i ne ( )

    # o r : l i n e = r a w _i n p ut ( " E n t er l i ne : \ n " )

    if e r r o r :

    s y s . s t d e r r . w r i t e ( " E r r o r ! \ n " )

    writedoes 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 60

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on Lists

    List comprehension

    Short notation to create lists

    e v e n_ s q u ar e s = [ ]

    fo r i in r a n g e ( 1 0 ) :

    if i %2 = = 0 :

    e v e n _ s q u a r e s . a p p e n d ( i * i )

    e v e n_ s q u ar e s = [ i * * 2 fo r i in r a n g e ( 1 0 ) if i % 2 = = 0 ]

    Compare:

    i2 | i {0, . . . , 9}, ieven

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 61

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on Lists

    List comprehension

    Short notation to create lists

    e v e n _s q u a re s = [ ]

    fo r i in r a n g e ( 1 0 ) :

    if i %2 = = 0 :

    e v e n _ s q u a r e s . a p p e n d ( i * i )

    e v e n _s q u a re s = [ i * * 2 fo r i in r a n g e ( 1 0 ) if i % 2 = = 0 ]

    Compare:

    i2 | i {0, . . . , 9}, ieven

    Can contain more than one for ... in ... [if ...]

    [ ( x , y . u p p e r ( ) ) fo r x in r a n g e ( 4 ) if x % 2 = = 0

    fo r y in [ "a " , "b " , "c " ]]

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 61

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on Lists (2)

    Advanced slice assignement Note: assigning slices of mutable sequences can change size

    l = r an ge ( 5 )

    l [ 1 : 3] = [ 7 , 8 , 9]

    l [ 4 :6 ] = [ 6]

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 62

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on Lists (2)Advanced slice assignement

    Note: assigning slices of mutable sequences can change size

    l = r a ng e ( 5)

    l [ 1 : 3] = [ 7 , 8 , 9]

    l [ 4: 6 ] = [ 6]

    range and xrange range([i,] j [, stride])creates list Memory allocated for whole list, even when only iterating over

    them, especially in loops xrangeobject calculates values when accessed (generator)

    fo r i in r a n g e ( 6 5 0 0 0 0 ) :

    d o _ s o m e t h i n g ( )

    fo r i in x r a n g e ( 6 5 0 0 0 0 ) :

    d o _ s o m e t h i n g ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 62

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Even more on Built-in Sequencesset and frozenset

    setis mutable,frozensetis not

    s = s e t ([ 3 , 1 ,2 , 3 , " f o o " ,2])

    l e n ( s )

    l = [ 2 ,8 , 7] # a n y i t e ra b l e s e q ue n c e

    s . d i f f e r e n c e ( l )

    s . i n t e r s e c t i o n ( l )

    s . u n i o n ( l )

    s . s y m m e t r i c _ d i f f e r e n c e ( l )

    s . i s s u b s e t ( [ 3 , " f o o " ])

    s . i s s u p e r s e t ( [ 3 , " f o o " ])

    Forset

    s . a d d ( 4 2 )

    s . r e m o v e ( 3 )s . i n t e r s e c t i o n _ u p d a t e ( l ) # a n d mo r e m et h o ds . . .

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 63

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Even more on Functions

    Anonymous functions

    Can be defined using keywordlambda

    Lambda functions allow functional programming

    de f i n c ( i ) :

    return i +1

    i nc = lambda ( i ) : i + 1( lambda i : i + 1 ) ( 4)

    Common use: map function on list items / filter list

    l = r a ng e ( 10 )

    ma p ( lambda x : x * x + 1 , l )

    f i l t e r ( lambda x : x % 2= =0 , l )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 64

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Part V

    Object Oriented Programming

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 65

  • 8/13/2019 Python 2x4

    10/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    What is an object?

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 66

    Scientific Computing in Computer Science, Technische Universitat Munchen

    What is an object?

    a car

    a cat

    a chair

    ...

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 66

    Scientific Computing in Computer Science, Technische Universitat Munchen

    What is an object?

    a car

    a cat

    a chair

    ...

    a molecule

    a star

    a galaxy

    ...

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 66

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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 66

    Scientific Computing in Computer Science, Technische Universitat Munchen

    How to Program Object Oriented

    Change your way of thinking

    Change your point of view

    It is not just a technical question!

    In Python?

    You do it all the time

    You just dont know it

    In Python, everything is an object (event int)

    a =4

    a . _ _ s t r _ _ ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 67

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Theory: Classes and ObjectsClasses

    A class is similiar to the definition of a type

    Specifies 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

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 68

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Theory: Classes and Objects

    Classes

    A class is similiar to the definition of a type

    Specifies 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/objectsInstances / Objects

    An instance/object is a concrete thing of the type of a class

    A class specifies properties, an object has specific values for thespecified 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

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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 E x a m p l e :

    de f _ _ i n i t_ _ ( s e l f , . . . ) :

    d o _ s o m e t h i n g

    de f _ _ d e l _ _ ( s e lf , . . . ) :

    d o _ s o m e t h i n g

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 69

  • 8/13/2019 Python 2x4

    11/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    First Class Example

    # ! / u s r / b i n / p y t h o n

    class G R S E m p l o y e e ( o b j e c t ) :

    n u m _ em p l o ye e s = 0

    de f _ _ i n i t_ _ ( s e l f , n a m e ) :

    s e lf . n a m e = n a me

    G R S E mp l o y ee . n u m _ e m pl o y ee s + = 1

    de f g e t N a m e ( s e l f ) :

    return s e l f . n a m e

    @ s t a t i c m e t h o d

    de f n u m P e o p l e ( ) :

    return G R S E m p l o y e e . n u m _ e m p l o y e e s

    p e r so n 1 = G R S Em p l o ye e ( " J o h n " )p e r so n 2 = G R S Em p l o ye e ( " M a r y " )

    print p e r s o n 1 . g e t N a m e ( )

    print G R S E m p l o y e e . n u m P e o p l e ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 70

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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 definition

    Methods of a class do not know the object name

    All methods get additional formal parameter self

    selfalways is the first formal parameter, but is skipped at a call(actual parameter)

    Within methods: access to other methods and variables viaself.variableand self.method(), resp.

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 71

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Theory: Methods

    . . . 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

    change (at least) one object/member variable

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 72

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Procedural Approach to Operate an Oven

    # ! / u s r / b i n / p y t h o n

    de f d o B a k i n g ( t e m p e r a t ur e , m o d e ) :

    d o _ s o m e t h i n g

    o v e n te m p e ra t u r e = 1 8 0

    o v en m od e = 2

    d o B a k i n g ( o v e n t e m p e r a tu r e , o v e n m o d e )

    If ive got a whole bakery?

    o v e n te m p e ra t u r es = [ ]

    o v e nm o d es = [ ]

    o v e n t e m p e r a t u r e s . a p p e n d ( 1 9 0 )

    o v e n m o d e s . a p p e n d ( 2 )

    .. .

    fo r i in r a n g e ( l e n ( o v e n t e m p e r a t u r e s ) ) :

    d o _ b a k i n g ( o v e n t e m p e r a t u r e s [ i ] , o v e n m o d e s [ i ] )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 73

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Object Oriented Approach to Operate an Oven

    # ! / u s r / b i n / p y t h o n

    class O v e n ( o b j e c t ) :

    de f _ _ i n i t _ _ ( s e l f , t e m p e r at u r e , m o d e ) :

    s e l f . t e m p e r a t u r e = t e m p e r a t u r e

    s e l f . m o d e = m o d e

    de f d o B a k i n g ( s e l f ) :

    d o _ s o m e t h i n g

    my Ov en = Ov en (1 80 ,2 )

    my Ov en . do Ba ki ng ()

    Again a whole bakery

    o ve n s = [ ]

    o v e n s . a p p e n d ( O v e n ( 1 9 0 , 2 ) )

    .. .

    fo r oven in o v e n s :o v e n . d o B a k i n g ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 74

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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 itsproperties

    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 75

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Inheritance

    Classes can inherit from a base class / superclass This makes them a derived class / subclassof 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

    redefined

    # ! / u s r / b i n / p y t h o n

    class M a s t e r S t u d e n t ( G R S E m p l o y e e ) :de f _ _ i n i t __ ( s e lf , n am e , t h e si s ) :

    s e lf . t h e s is = t h e si s

    G R S E m p l o y e e . _ _ i n i t _ _ ( s e l f , n a m e )

    de f p r i n t I n f o ( s e l f ) :

    print " % s w o r k s o n % s " % ( s e lf . n a me , s e lf . t h e s is )

    p e r so n 3 = M a s t er S t u de n t (" T e r r y " , " T e r r y s t h e s i s t o p i c " )

    p e r s o n 3 . p r i n t I n f o ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 76

  • 8/13/2019 Python 2x4

    12/23

  • 8/13/2019 Python 2x4

    13/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module math (2)

    Trigonometric functions assume radians

    c o s ( x ); c o sh ( x ) ; a c os ( x )

    s i n ( x ); . . .

    t a n ( x ); . . .

    d e g r e e s ( x ) # r a d - > d eg

    r a d i a n s ( x ) # d e g - > r ad

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 82

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module math (2)

    Trigonometric functions assume radians

    c o s ( x ); c o sh ( x ) ; a c os ( x )

    s i n ( x ); . . .

    t a n ( x ); . . .

    d e g r e e s ( x ) # r ad - > d eg

    r a d i a n s ( x ) # d eg - > r ad

    inf/nan

    f l o a t (" i n f " )

    f l o a t (" - i n f " )

    f l o a t (" n a n " )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 82

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module math (2)

    Trigonometric functions assume radians

    c o s ( x ); c o sh ( x ) ; a c os ( x )

    s i n ( x ); . . .

    t a n ( x ); . . .

    d e g r e e s ( x ) # r a d - > d eg

    r a d i a n s ( x ) # d e g - > r ad

    inf/nan

    f l o a t (" i n f " )

    f l o a t (" - i n f " )

    f l o a t (" n a n " )

    Use module cmathfor complex numbers

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 82

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Now to Real Maths. . .

    Standard sequence types (list, tuple, . . . )

    Can be used as arrays

    Can contain different types of objects

    Very flexible, but slow Loops are not very efficient either

    For efficient scientific computing, other datatypes and methodsrequired

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 83

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Now to Real Maths. . .

    Standard sequence types (list,tuple, . . . )

    Can be used as arrays

    Can contain different types of objects

    Very flexible, but slow Loops are not very efficient either

    For efficient scientific computing, other datatypes and methodsrequired

    Modules

    NumPy

    Matplotlib

    SciPy

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 83

    Scientific Computing in Computer Science, Technische Universitat Munchen

    NumPy

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 84

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module numpy

    Homogeneous arrays

    NumPy provides arbitrary-dimensional homogeneous arrays

    Example

    from numpy import *

    a = a r r ay ( [ [ 1 , 2 , 3] , [ 4 , 5 , 6 ]] )

    print at y p e ( a )

    a . s h a p e

    print a [ 0 , 2 ]

    a [ 0 ,2 ] = - 1

    b = a * 2

    print b

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 85

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Array creation

    Create from (nested) sequence type Direct access with method[]

    a = a r ra y ( [ 1 ,2 , 3 , 4 , 5 , 6 , 7 ,8 ] )

    a [ 1 ]

    a = a r ra y ( [ [1 , 2 , 3 , 4 ] , [5 , 6 , 7 , 8 ]] )

    a [ 1 , 1 ]

    a = a r r a y ( [ [ [ 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 86

  • 8/13/2019 Python 2x4

    14/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Array creation

    Create from (nested) sequence type Direct access with method []

    a = a r r ay ( [ 1 , 2 , 3 ,4 , 5 , 6 , 7 , 8] )

    a [ 1 ]

    a = a r r ay ( [ [ 1 , 2 ,3 , 4 ] , [ 5 ,6 , 7 , 8 ] ])

    a [ 1 , 1 ]

    a = a r r ay ( [ [ [ 1 , 2] , [ 3 , 4 ] ] , [[ 5 , 6 ] , [ 7 , 8] ] ])

    a [ 1 , 1 , 1 ]

    Properties of arrays

    a . n d i m # n u m be r o f d i m e ns i o n s

    a . s h a p e # d i m e ns i o n s

    a . s i z e # n u m be r o f e l e me n t s

    a . d t y p e # d at a t yp e

    a . i t e m s i z e # n um b er o f b yt e s

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 86

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Data Types

    Exact, C/C++-motivated type of array elements can be specified

    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

    a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] , d t y p e = i n t )

    a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] , d t y p e = c o m p l e x )

    a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] , d t y p e = i n t 8 )a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 1 0 0 0 ] ] , d t y p e = i n t 8 ) # w r o ng

    a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , " h i " ] ] , d t y pe = o b j e ct )

    Exception: object_stores pointers to objects

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 87

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Create Arrays

    (Some) default matrices (optional parameter: dtype)

    a r a ng e ( [ a , ] b [ , s t r id e ] ) # a s r an ge , 1 D

    z e ro s ( ( 3 , 4) )

    o n es ( ( 1 , 3 , 4) )e m pt y ( ( 3 , 4) ) # u n i n it i a l iz e d ( f a s t )

    l i ns p ac e ( a, b [ , n ] ) # n e q ui d is t an t i n [ a , b]

    l o gs p ac e ( a, b [ , n ] ) # 1 0* * a t o 1 0* * b

    i d e n t i t y ( n ) # 2d

    f r o m f u n c t i o n ( lambda i , j : i + j , ( 3 , 4) , d t y pe = i n t )

    de f f ( i , j ) :

    return i+ j

    f r o m fu n c t io n ( f , ( 3 , 4) , d t yp e = i n t )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 88

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Manipulate Arrays

    Reshaping arrays

    a = a r a ng e ( 1 2 )

    b = a . r e s ha p e ( ( 3 , 4) )a . r e s i z e ( ( 3 , 4 ) ) # i n - p l ac e !

    a . t r a n s p o s e ( )

    a . f l a t t e n ( )

    # E x a mp l e u se - c a s e :

    a = a r a ng e ( 1 4 4)

    a . r e s i z e ( ( 1 2 , 1 2 ) )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 89

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Create Arrays (2) Create/Copy from existing data

    a = a r a ng e ( 1 2 ); a . r e s iz e ( ( 3 , 4 ))

    c o p y ( a )

    d i ag ( a ) ; t r il ( a ) ; t r iu ( a )

    e m p t y _ l i k e ( a ) # c op y s ha p e

    z e r o s _ l i k e ( a )

    o n e s _ l i k e ( a )

    a = l oa d tx t (" m a t r i x . t x t " ) # f r o m f i le ( ) i f b i n ar y

    # p l e nt y o f o p t io n s : c o mm e nt s , d e li m . , u s ec o ls , . . .

    Matrix output

    a . t o l i s t ( )

    s a v e t x t (" m a t r i x . t x t " , a) # t of il e() if b in ar y

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 90

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Array Access and Manipulation

    Typical slicing operations can be used

    Separate dimensions by comma

    a = a r a ng e ( 2 0 ); a . r e s iz e ( ( 4 , 5) )

    a [ 1 ]

    a [ 1 : 2 , : ]

    a [ : , : : 2 ]

    a [ : : 2 , : : 2 ]

    a [ : : 2 , :: 2 ] = [ [0 , - 2 , - 4 ] , [ - 10 , - 12 , - 1 4] ]

    a [ 1 : :2 , 1 : : 2 ] = - 1* a [ 1 : :2 , 1 : : 2]

    Selective access

    a [ a > 3 ]

    a [a > 3 ] = -1

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 91

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Array Access

    Iterating over entries

    fo r ro w in a:

    print ro w

    b = a r a ng e ( 3 0 ); b . r e s iz e ( ( 2 , 3 , 4) )

    fo r ro w in b:fo r co l in r o w :

    print co l

    fo r entry in a . f l a t :

    print entry

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 92

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Computing with Arrays

    Fast built-in methods working on arrays

    a = a r a ng e ( 1 2 ); a . r e s iz e ( ( 3 , 4) )

    3* a

    a * * 2

    a + a ^ 2

    s i n ( a )

    s q r t ( a )

    p r o d ( a )s u m ( a )

    i t = t r a ns p o s e ( a)

    x = a r ra y ( [ 1 , 2 , 3] )

    y = a r ra y ( [ 1 0 , 20 , 3 0 ] )

    i n n er ( x , y )

    d o t ( it , x )

    c r o s s ( x , y )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 93

  • 8/13/2019 Python 2x4

    15/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Computing with Arrays

    There is much more. . .

    var () cov () std ()

    me an () me di an ()

    mi n () ma x ()

    s v d ( )

    t e n s o r d o t ( )

    .. .

    Matrices (with mat) are subclasses of ndarray, but strictlytwo-dimensional, with additional attributes

    m = ma t (a )

    m. T # t r a n sp o s e

    m. I # i n v er s e

    m. A # as 2d ar ra y

    m. H # c o n j ug a t e t r a n sp o s e

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 94

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Submodules

    Modulenumpy.random

    Draw from plenty of different distributions

    More powerful than module random

    Work on and return arrays

    from n u m p y . r a n d o m import *

    b i n o m i a l ( 1 0 , 0 . 5 ) # 1 0 t ri a ls , s u c ce s s 5 0%

    b i n om i a l ( 10 , 0 .5 , 1 5 )

    r a n di n t ( 0 , 1 0 , 1 5 ) # [ 0 ,1 0) , i nt

    r a n d ( ) # [ 0 , 1)r a n d ( 3 , 4 ) # (3 x4 ) array

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 95

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Submodules (2)

    Modulenumpy.linalg

    Core linear algebra tools

    n o rm ( a ) ; n o rm ( x )

    i n v ( a )

    s o lv e ( a , b ) # L A PA C K L U d e co m p .

    d e t ( a )

    e i g ( a )

    c h o l e s k y ( a )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 96

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Submodules (2)

    Modulenumpy.linalg

    Core linear algebra tools

    n o rm ( a ) ; n o rm ( x )

    i n v ( a )

    s o l ve ( a , b ) # L A PA C K L U d ec o mp .

    d e t ( a )

    e i g ( a )

    c h o l e s k y ( a )

    Modulenumpy.fft

    Fourier transforms

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 96

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Submodules (2)

    Modulenumpy.linalg

    Core linear algebra tools

    n o rm ( a ) ; n o rm ( x )

    i n v ( a )

    s o lv e ( a , b ) # L A PA C K L U d e co m p .

    d e t ( a )

    e i g ( a )

    c h o l e s k y ( a )

    Modulenumpy.fft

    Fourier transforms

    There is more. . .

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 96

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Matplotlib

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 97

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Matplotlib

    What is it?

    Object-oriented library for plotting 2D

    Designed to be similar to the matlab plotting functionality

    Designed to plot scientific data, built on numpy datastructures

    Version Mania

    Numpy, scipy, ipython and matplotlib often used together

    They somehow depend on each other (e.g. matplotlib usesarrays from numpy)

    Pylab is a (unofficial) package containing all four

    Depending on your linux distribution, you might have to installdifferent 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 98

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Several Ways to do the Same

    python/ipython interactive

    > i p y th o n

    import s ci p y , m a t p l o t l i b . p y l a b

    x = s c ip y . r a n dn ( 1 0 0 0 0)

    ma tp lo tl ib . py la b . hi st (x , 10 0)

    > i p y th o n

    import n u m p y . r a n d om , m a t p l o t l i b . p y l a bx = n u mp y . r a n do m . r a n dn ( 1 0 0 0 0)

    ma tp lo tl ib . py la b . hi st (x , 10 0)

    ipython in pylab mode

    > i p y th o n - p y la b

    x = r a nd n ( 1 0 0 00 )

    h i st ( x , 1 0 0)

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 99

  • 8/13/2019 Python 2x4

    16/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Example - First Plot

    partially taken fromhttp://matplotlib.sourceforge.net/users/screenshots.html

    from pylab import *

    x = a r a ng e ( 0 .0 , 2 * pi , 0 . 01 )

    y = s in ( x)

    p l ot ( x , y , l i n ew i d th = 4 )

    p l o t ( x , y )

    x l a b e l ( L a b e l f o r x a x i s )

    y l a b e l ( L a b e l f o r y a x i s )

    t i t l e ( S i m p l e p l o t o f s i n )g r i d ( T r u e )

    s h o w ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 100

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Example Using Subplots

    from pylab import *

    de f f ( t ) :

    s 1 = c o s ( 2* p i * t )

    e 1 = e xp ( - t)

    return m u l t i p l y ( s 1 , e 1 )

    t 1 = a r an g e (0 .0 , 5 .0 , 0 .1 )

    t 2 = a r a ng e ( 0 .0 , 5 . 0 , 0 . 0 2 )

    t 3 = a r a ng e ( 0 .0 , 2 . 0 , 0 . 0 1 )

    s h o w ( ) # g i ve s e rr o r b u t h e lp s ; - )

    s u b p l o t ( 2 , 1 , 1 ) # r o ws , c o lu m ns , w h i ch t o s h ow p l ot ( t 1 , f ( t 1 ) , g o , t 2 , f ( t 2 ), k - - )

    s u b p l o t ( 2 , 1 , 2 )

    p l o t ( t 3 , c o s ( 2 * p i * t 3 ) , r . )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 101

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Example Using Subplots

    # p r e vi o u s s l i d e c o n t in u e d

    s u b p l o t ( 2 , 1 , 1 )g r i d ( T r u e )

    t i t l e ( A t a l e o f 2 s u b p l o t s )

    y l a b e l ( D a m p e d o s c i l l a t i o n )

    s u b p l o t ( 2 , 1 , 2 )

    g r i d ( T r u e )

    x l a b e l ( t i m e ( s ) )

    y l a b e l ( U n d a m p e d )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 102

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Example - Histogram

    # s ta rt i p yt h on - p yl ab o r u se i m po r ts :

    # f r o m m a t p lo t l i b . m la b i m p or t *

    # f r o m m a t p lo t l i b . p yp l o t i m p or t *

    from numpy import *mu , si gm a = 10 0 , 15

    x = m u + s i g ma * r a n d om . r a n d n ( 1 00 0 0 )

    n , b in s , p at c he s = h is t (x , 5 0 , n o r me d =1 , \

    f a c e c o l o r = g r e e n , a l ph a = 0 . 7 5)

    # a dd a be st f it l in e

    y = n o rm p df ( b in s , m u , s i g ma )

    p l ot ( b i ns , y , r - - , l i n ew i d t h = 1)

    a x is ( [ 4 0 , 1 6 0 , 0 , 0 . 0 3] )

    p l t . s h o w ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 103

    Scientific Computing in Computer Science, Technische Universitat Munchen

    SciPy

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 104

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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 importnecessary)

    A lot more libraries for scientific computing are available, some ofthem using NumPy and SciPy

    Here, just a short overview will be given

    www.scipy.orgfor more material (incl. the content of thefollowing slides)

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 105

    Scientific Computing in Computer Science, Technische Universitat Munchen

    SciPy Organisation - Subpackages

    cluster Clustering algorithmsconstants Physical and mathematical constantsfftpack Fast Fourier Transform routinesintegrate Integration and ordinary differential equation solversinterpolate Interpolation and smoothing splinesio Input and Outputlinalg Linear algebramaxentropy Maximum entropy methods

    ndimage N-dimensional image processingodr Orthogonal distance regressionoptimize Optimization and root-finding routinessignal Signal processingsparse Sparse matrices and associated routinesspatial Spatial data structures and algorithmsspecial Special functionsstats Statistical distributions and functionsweave C/C++ integration

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 106

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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 107

  • 8/13/2019 Python 2x4

    17/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Example: Interpolation - Linear

    import n u mp y a s n p

    import m a t p l o tl i b . p y pl o t a s p l t

    from scipy import i n t e r p o l a t e

    x = n p . a r an g e ( 0 , 10 )

    y = n p . e xp ( - x / 3 .0 )

    f = i n t e rp o l a te . i n t e r p1 d ( x , y )

    x n ew = n p . a r an g e ( 0 , 9 , 0. 1 )

    p l t . p l o t ( x , y , o , x n e w , f ( x n e w ) , - )

    p l t . t i t l e ( L i n e a r i n t e r p o l a t i o n )p l t . s h o w ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 108

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Example: Interpolation - Cubic Spline

    import n u mp y a s n p

    import m a t p l o tl i b . p y p lo t a s p l t

    from scipy import i n t e r p o l a t e

    x = n p . a r an g e ( 0 , 2 . 2 5 * n p . pi , n p . pi / 4 )

    y = n p . si n ( x)

    s p l i n e = i n t e r p o l a t e . s p l r e p ( x , y , s = 0 )

    x n e w = n p . a r a n g e ( 0 , 2 . 0 2 * n p . p i , n p . p i / 5 0 )

    y n ew = i n t er p o l at e . s p l ev ( x n ew , s p l in e )

    p l t . p l o t ( x , y , o , x n e w , y n e w )

    p l t . l e g e n d ( [ L i n e a r , C u b i c S p l i n e ])p l t . a x i s ( [ - 0 . 0 5 , 6 . 3 3 , - 1 . 0 5 , 1 . 0 5 ] )

    p l t . t i t l e ( C u b i c - s p l i n e i n t e r p o l a t i o n )

    p l t . s h o w ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 109

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Part VII

    Misc Modules, Functionality, and More

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 110

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module random

    Compute and use pseudo-random numbers

    from random import *

    print r a n d o m ( )

    print c h o i c e ( [ 1 , 4 , " H e l l o " ])

    (Re)initialize random with seed([x])(def: time)

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 111

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module random

    Compute and use pseudo-random numbers

    from random import *

    print r a n d o m ( )

    print c h o i c e ( [ 1 , 4 , " H e l l o " ])

    (Re)initialize random with seed([x])(def: time)

    Random floats

    r a n d o m ( ) # [ 0. 0 , 1 .0 )

    u n i f o r m ( a , b ) # [ a, b )

    e x p o v a r i a t e ( l a m b d ) # [ 0. 0 , + i n f )

    n o r m a l v a r i a t e ( m u , s i g m a )

    .. .

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 111

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module random (2)

    Random integers

    r a n di n t ( a , b ) # a

  • 8/13/2019 Python 2x4

    18/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module subprocess (2)

    Example: Call Gnuplot

    # ! / u s r / b i n / p y t h o n

    import s u b p ro c e s s a s s p

    p = s p . Po pe n ( [" g n u p l o t " , " - p e r s i s t " ],

    s t d i n = s p . P I P E , s t d o u t = s p . P I P E , s t d e r r = s p . S T D O U T )

    p . s t d i n . w r i t e ( " p l o t s i n ( x ) \ n " )

    p . s t d i n . f l u s h ( )

    r a w _ i n p u t (" P r e s s E n t e r t o c o n t i n u e " )

    p . s t d i n . w r i t e ( " p l o t s i n ( x ) , c o s ( x ) \ n " )

    p . s t d i n . c l o s e ( )

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

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 114

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Operating System Services

    Moduleos

    Interface to OS services Variables

    import os

    o s . e n v i r o n # e n v i ro n m en t v a r ia b le s , d i ct

    o s . l i n e s e p # l i n e s e p ar a ti o n ; \ r \n w i n , \ n l i n ux

    o s . n a m e # t o f in d o ut wh at sy s te m

    Some general purpose methods

    o s . g e t c w d ( )

    o s . c h d i r ( p a t h )

    o s . g e t g r o u p s ( ) # ( U NI X )

    o s . u n a m e ( ) # s y s te m i n f or m a t io n ( U N IX )

    o s . t i m e s ( ) # ( u sr , sy s , c _u sr , c _s ys , w ct )

    .. .

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 115

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Operating System Services (2)

    Some methods related to files

    o s . c h mo d ( p at h , m o de ) # c h m od

    o s . c h m o d ( " t m p . p y " , 0 6 6 4) # f o u r d i g i t s r e q u ir e d

    l i s t d i r ( p a t h ) # ls

    mk di r ( pa th ) # m k d ir

    ma ke di rs ( pa th ) # m k di r - p

    r e n am e ( s rc , d s t ) # mv

    r e m o v e ( p a t h ) # rm

    r m d i r ( p a t h ) # r m d ir

    r e m o v e d i r s ( p a t h ) # r m - rf

    s t a t ( p a t h ) # s t at s ( a ti me , . .. )

    w a lk ( p a t h [ , t o p do w n ] ) # t ra ve r se d ir r ec .

    # ( d i r pa t h , d i rn a me s , f n a me s ) f o r e a ch d i r

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 116

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Operating System Services (3)

    Submoduleos.path

    To manipulate pathnames and check for file properties

    a b s p a t h ( p a t h )

    b a s e n a m e ( p a t h )d i r n a m e ( p a t h )

    e x i s t s ( p a t h )

    i s f il e ( p a th ) ; i s di r ( p a th ) ; i s l in k ( p a th )

    g e t s i z e ( p a t h )

    g e t a t i m e ( p a t h )

    j o in ( p a th 1 , p a th 2 , . . .)

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 117

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Operating System Services (4)Example traversing a directory

    # ! / u s r / b i n / p y t h o n

    import os

    de f p r i n t _ t r e e ( p a t h ) :

    " " " P ri n t t h e c o n t e n t s o f t h e d i r e c t o ry s p e ci f i ed

    i n p a t h a n d a l l i t s s u b d i r e c to r i e s . "" "

    fo r ( d i r p a t h , d i r n a me s , f n a m e s ) in o s . w a l k ( p a t h ) :

    # g et t he d i re c to r y l e ve l

    l e v el = l e n ( d ir p a th . s p l i t ( os . s e p ) ) -1

    # p r i nt d i r e ct o r y

    print "| " * l e v e l + " + " + o s . p a t h . b a s e n a m e ( d i r p a t h )

    # p r in t a ll f i le s

    fo r f in f n a m e s :

    print "| " * l e v el + " | " + f

    p r i n t _ t r e e (" ." )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 118

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Regular ExpressionsPattern 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 setA|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 119

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Character Escape Sequences

    Characters with special meaning (., *, ...) can be used withtheir 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

    \number match the text matched by group number (starting from 1)

    \d same as

    [0-9]\D same as[^0-9]\s matches any whitespace (same as[\t\n\r\f\v]\S matches nonwhitespace\w matches any alphanumeric character\W matches nonalphanumeric characters\A matches the start of a string\Z matches the end of a string

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 120

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Regular Expressions (cont.)

    findall(patt, string) find all non-overlapping matches sub(patt, repl, string) replaces matches by repl

    from re import *

    r e gs t r = r \ d * \ . \ d * | \ d *

    s = " 1 2 . 3 / 5 / 4 . 4 ; 5 . 7 ; 6 "

    findall(regstr ,s)

    r e gs t r = r ( \ d * \ . \ d * | \ d * )

    s u b ( r e g s tr , r \ 1 x x x , s)

    greedy vs. non-greedy

    r e g _g r e e dy = r < . * >

    s = " title "

    m_ g = se ar ch ( re g_ gr ee dy ,s ) # w h o l e < H 1 > t i t le < / H 1 > m a t c h e d

    r e g _ no n g r ee d y = r < . * ? > # m a tc h a s f ew c ha r s a s p o ss i bl e

    m_ ng = se ar ch ( re g_ no ng re ed y , s) # o n l y < H1 > m a t ch e d

    print m _ g . g r ou p ( ) + " , " + m _ ng . g r o u p ()

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 121

  • 8/13/2019 Python 2x4

    19/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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 first match

    finditer(patt, string)like findall, but returns iterator

    m.group([group1, group2]) returns subgroups of the match

    import re

    s = " d a t e s 0 5 / 0 2 / 2 0 1 0 - 0 5 / 1 4 / 2 0 1 0 "

    p at t = r ( \ d + ) / ( \ d + ) / ( \ d + )

    fo r m in f i n d i t e r ( p a t t , s ) :print m . g r o u p ( )

    print ( " % s - % s " % ( m . g r ou p ( 2 ) , m . g r o up ( 3 ) ) )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 122

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Exceptions: Dealing with Errors

    Consider the user enters characters:

    x = r a w_ i np u t (" P l e a s e e n t e r a n u m b e r : " )

    print f l o a t ( x )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 123

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Exceptions: Dealing with Errors

    Consider the user enters characters:

    x = r a w_ i np u t (" P l e a s e e n t e r a n u m b e r : " )

    print f l o a t ( x )

    Failed conversion raises anValueErrorexception

    P l e as e e n te r a n u m be r : x y z

    .. .

    T r a ce b a c k ( m o s t r e c en t c a ll l a st ) :

    File " < s t d i n > " , l in e 1 , in

    V a l u eE r r o r : i n v al i d l i t er a l fo r f l o a t ( ): x y z

    To deal with erroneous behaviour catch exception and handle it

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 123

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Exceptions: Dealing with Errors (2)

    Syntax is

    tr y :

    # d o s o m et h i ng except V a l u e E rr o r , e : # P y > 3. 0: " E x c ep t io n a s e "

    # h a n dl e V a l u eE r r o r

    except E x c e p t io n , e :

    # h a nd l e a ll o t he r e r ro r s

    else :

    # w ha t t o d o i f n o e r r or o c cu r ed

    f i n a l l y:

    # c od e t ha t s h ou l d b e e xe c ut e d i n a ny c as e

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 124

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Exceptions: Dealing with Errors (3) Back to the previous example

    g o t _ an s w e r = F a l se

    w h il e n o t g o t _ a n s w e r :

    x = r a w_ i np u t (" P l e a s e e n t e r a n u m b e r : " )

    tr y :

    print f l o a t ( x )

    g o t _ an s w e r = T r ue

    except V a l u e E r ro r , e :

    print " T h a t w a s n t a n u m b e r "

    # d o s o me t hi n g w i t h e

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 125

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Exceptions: Dealing with Errors (3) Back to the previous example

    g o t _a n s we r = F a ls e

    w h i le n o t g o t _ a n s w e r :

    x = r a w_ i np u t (" P l e a s e e n t e r a n u m b e r : " )

    tr y :

    print f l o a t ( x )

    g o t _ an s w er = T r ue

    except V a l u e E r ro r , e :

    print " T h a t w a s n t a n u m b e r "

    # d o s o me t hi n g w it h e

    To raise your own errors (use one of many default types or createyour own):

    raise R u n t i m e E r r o r ( " O o o p s ! S o m e t h i n g w e n t w r o n g ! " )

    raise I O E r r o r ( " F i l e n o t e x i s t e n t . . . " )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 125

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Built-in Exceptions

    B a s e E x c e p t i o n # r oo t o f a ll e xc .

    G e n e r a t o r E x i t

    K e y b o a r d I n t e r r u p t # C tr l +C , e . g .

    S y s t e m E x i t # P r o gr a m E x it ( s y s . e xi t ( ) )

    E x c e p t i o n # B as e f or n on - e x it e xc .

    S t o p I t e r a t i o n

    S t a n d a r d E r r o r # o nl y P y th o n 2 . x

    A r i t h m e t i c E r r o r

    F l o a t i n g P o i n t E r r o r

    Z e r o D e v i s i o n E r r o r

    A s s e r t i o n E r r o r

    A t t r i b u t e E r r o r

    E n v i r o n m e n t E r r o r

    I O E r r o r # o p en ( " f i l e . tx t " ) , e . g .

    O S E r r o r

    E O F E r r o r

    I m p o r t E r r o r # m o du l n ot f ou nd , e . g .

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 126

    Scientific Computing in Computer Science, Technische Universitat Munchen

    L o o k u p E r r o r

    I n d e x E r r o r # t u p el s / l i st s , e . g .

    KeyError # d i c t i o n ar i e s , e . g .

    M e m o r y E r r o r

    N a m e E r r o r # n am e n ot f ou nd

    U n b o u n d e d L o c a l E r r o r

    R e f e r e n c e E r r o r

    R u n t i m e E r r o r

    N o t I m p l e m e n t e d E r r o r

    S y n t a x E r r o r

    I n d e n t a t i o n E r r o rTabError

    S y s t e m E r r o r

    T y p e E r r o r # t yp e e rr or ( 2 + " 3" )

    V a l u e E r r o r # v a l ue e r r or r ( f l o at ( " t h r ee " ) )

    U n i c o d e E r r o r

    U n i c o d e D e c o d e E r r o r

    U n i c o d e E n c o d e E r r o r

    U n i c o d e T r a n s l a t e E r r o r

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 127

  • 8/13/2019 Python 2x4

    20/23

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Throw Built-in Exceptions

    de f r e a d f l o a t 1 ( ) :

    while T r u e :

    tr y :

    a = r a w_ i np u t (" N u m b e r b e t w e e n 0 . 0 a n d 1 . 0 : " )

    a = f l oa t ( a)

    if ( a < 0 . 0 or a > 1 . 0 ) :

    raise V a l u e E r r o r ( " N u m b e r n o t i n [ 0 . 0 ; 1 . 0 ] " )

    except V a l u eE r r or a s e :

    print " E r r o r : % s " % e

    else :

    return a

    At a = float(a), an error is thrown automatically; the rest of thetry Block is not being executed

    If a convertable, the value range is checked and a new exceptionis possibly thrown

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 128

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module pickle

    Serializing Python objects Save objects as they are (current state) to file

    import pickle

    f d = o pe n ( m y O b j e c t s , w b )

    p i c kl e . d u mp ( x , f d )

    p i c kl e . d u mp ( y , f d )

    f d . c l o s e ( )

    Most objects can be pickled Not valid for objects such as opened files

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 129

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module pickle

    Serializing Python objects

    Save objects as they are (current state) to file

    import pickle

    f d = o pe n ( m y O b j e c t s , w b )

    p i c kl e . d u m p (x , f d )p i c kl e . d u m p (y , f d )

    f d . c l o s e ( )

    Most objects can be pickled Not valid for objects such as opened files Then load in same order

    f d = o pe n ( m y O b j e c t s , r b )

    x = p i c k l e . l o a d ( f d )

    y = p i c k l e . l o a d ( f d )

    f d . c l o s e ( )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 129

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Misc: Python 2 to 3

    Which version to use? Seehttp://wiki.python.org/moin/Python2orPython3for hintsConverting from Python 2.x to Python 3.x

    Convert your code to Python 2.6

    To check, there is a Py3k warnings mode

    p y t ho n - 3 s c r ip t n a me . p y

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 130

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Misc: Python 2 to 3Which version to use? Seehttp://wiki.python.org/moin/Python2orPython3for hintsConverting from Python 2.x to Python 3.x

    Convert your code to Python 2.6

    To check, there is a Py3k warnings mode

    p y t ho n - 3 s c r i pt n a me . p y

    If no warnings remain, use 2to3tool

    Ships with Python Converts many things automatically Cant do everything

    Rest manually

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 130

    Scientific Computing in Computer Science, Tec