Вы находитесь на странице: 1из 35

Munchen Scientic Computing in Computer Science, Technische Universitat

Part IV More on Python

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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 Compact Course @ GRS, June 03 - 07, 2013 37

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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

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

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 40

Munchen Scientic Computing in Computer Science, Technische Universitat

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

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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]

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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 43

Munchen Scientic Computing in Computer Science, Technische Universitat

More on Functions (3)

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 44

Munchen Scientic Computing in Computer Science, Technische Universitat

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

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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 )

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

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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

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 47

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 48

# all # n Bytes # one line #

Munchen Scientic Computing in Computer Science, Technische Universitat

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 = f . readline () if not line : break print line

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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 = f . readline () if not line : break print line

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 49

Munchen Scientic Computing in Computer Science, Technische Universitat

Getting more. . .

Modules Outsource functionality in separate .py les Import them as library module Example (tools.py):
""" This module provides 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

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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 51

Munchen Scientic Computing in Computer Science, Technische Universitat

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 52

Munchen Scientic Computing in Computer Science, Technische Universitat

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 * Inspect namespace Inspect namespace of module with
dir ( tools )
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 52

Munchen Scientic Computing in Computer Science, Technische Universitat

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

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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

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 "

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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

Module search path


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 Compact Course @ GRS, June 03 - 07, 2013 54

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

# contents for " import tools " # for " import tools . files " # for " import tools . graphics " # for " import tools . stringtools " nesting

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 55

Munchen Scientic Computing in Computer Science, Technische Universitat

Command-Line Options
Using sys
sys.argv is list of command-line options First one (sys.argv[0]) is program name 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...

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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 57

Munchen Scientic Computing in Computer Science, Technische Universitat

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 58

Munchen Scientic Computing in Computer Science, Technische Universitat

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

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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

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

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 60

Munchen Scientic Computing in Computer Science, Technische Universitat

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]

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

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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]

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 61

Munchen Scientic Computing in Computer Science, Technische Universitat

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]

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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]

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

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

Munchen Scientic Computing in Computer Science, Technische Universitat

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 . s y m m e t r i c _ d i ffe re nc e ( l ) s . issubset ([3 , " foo " ]) s . issuperset ([3 , " foo " ]) For set s . add (42) s . remove (3) s . i n te r s ec t i on _u pdat e ( l )
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 63

# and more methods ...

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 )

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

Вам также может понравиться