Notater: INF3331 Veronika Heimsbakk veronahe@student.matnat.uio.no December 4, 2013 Contents 1 Introduction 3 2 Bash 3 2.1 Variables.............................. 3 2.2 Loops............................... 4 2.2.1 For loop.......................... 4 2.3 Pipes................................ 4 2.4 Functions............................. 5 3 Basic Python 5 3.1 Variables.............................. 5 3.2 Lists and tuples.......................... 6 3.3 Strings............................... 6 3.4 Conditionals............................ 7 3.5 Loops............................... 7 3.6 Functions............................. 7 3.7 Read from file........................... 8 3.8 Using modules........................... 8 3.9 Classes............................... 8 4 Stuff that is nice to know 9 4.1 Copy, rename and remove files.................. 9 4.2 Traverse directory tree...................... 9 1
5 Regular expression 10 5.1 Special chars........................... 10 5.2 Common expressions....................... 10 5.3 Quoting special characters.................... 11 5.4 Handy functions.......................... 11 5.5 Examples of regex......................... 12 5.5.1 Scientific or decimal or integer notation......... 12 6 Numerical Python 12 6.1 Operations on NumPy arrays.................. 12 6.2 Example of Python and NumPy................. 13 6.2.1 Vectorization of a function................ 13 7 Web applications in Python 13 7.1 CGI................................ 13 2
1 Introduction I took INF3331 - Problem solving with high-level languages in the fall 2013. These notes consists of a summary of my weekly assignments, lectures slides and resources online. 2 Bash Use plain Bourne shell (/bin/sh) when special features of Bash (/bin/bash) are not needed. 2.1 Variables Variables in Bash are untyped. 1 x = 3 2 y = 2 3 z = $x+$y 4 echo $z # output 3+2 5 6 z = $ ( ( x+y ) ) 7 ( ( v=x+y ) ) 8 l e t w = x+y 9 echo $z $v $w # output 5 5 5 Handle variables as they where strings, unless they are explicit declared as an Integer: 1 d e c l a r e i x = 3 Example of handling as string: 1 i f [ $?!= 0 ] ; then # s a f e 2 i f [ $?!= 0 ] ; then # may be unsafe Command line arguments are accessed like this: 1 $1 $2 $3 $4 # and s o on And they are initialised as this: 3
1 r=$1 # f o r f i r s t argument You may also store Unix-commands in variables: 1 s = echo s ( $r ) bc 1 or 1 s = $ ( echo s ( $r ) bc 1 ) 2.2 Loops 2.2.1 For loop Following script is removing all.tmp-files at its location. 1 f i l e s = / bin / l s. tmp 2 # use / bin / l s in case l s i s a l i a s e d 3 4 f o r f i l e in $ f i l e s 5 do 6 echo removing $ f i l e 7 rm f $ f i l e 8 done C-style for loop: 1 d e c l a r e i i 2 f o r ( ( i =0; i<$n ; i ++)) ; do 3 echo $c 4 done 2.3 Pipes Output from one command can be sent as input to another through a pipe. For sending files with size to reverse numerical sort (-rn) and get a list of files sorted after size: 1 / bin / l s s s o r t r 4
2.4 Functions Example of a function with argument: 1 f u n c t i o n q u i t { 2 e x i t 3 } 4 f u n c t i o n e { 5 echo $1 6 } 7 e Hello # c a l l i n g e with argument Hello 8 e World # c a l l i n g e with argument World 9 q u i t # c a l l i n g q u i t 3 Basic Python 1 #! / usr / bin /env python 2 3 p r i n t Hello, World!../code/hw.py To run Python, simply type ~$ python yourprogram.py <argument1> <argument2>... 3.1 Variables Variables in Python hold references to objects of any type. 1 a = 3 2 a = 3. 0 3 a = 3 4 a = [ 1, 2 ] 5 6 # t e s t f o r var type 7 i f i s i n s t a n c e ( a, i n t ) : # i n t? 8 i f i s i n s t a n c e ( a, ( l i s t, t u p l e ) : # l i s t or t u p l e? Common types in Python: Numbers: int, float, complex../code/var.py Sequences: str (string), list, tuple, NumPy array 5
Mappings: dict (dictionary/hash) User-defined type in terms of a class 3.2 Lists and tuples 1 m y l i s t = [ a, 2. 5, 42, b ] 2 mytuple = ( a, 2. 5, 42, b ] 3 m y l i s t [ 1 ] = 10 4 m y l i s t. append ( c ) 5 mytuple [ 1 ] = 10 # i l l e g a l, cannot change a t u p l e List functionality../code/li.py 1 a = [ ] # i n i t empty l i s t 2 a = [ 1, b ] # i n i t a l i s t 3 a. append ( e ) # add e to end o f l i s t 4 a + [ 1, 3 ] # add two l i s t s 5 a [ 3 ] # index a l i s t element 6 a [ 1] # get l a s t l i s t element 7 a [ 1 : 3 ] # copy data to s u b l i s t, here : index 1,2 8 d e l a [ 3 ] # d e l e t e element at index 3 9 a. remove ( 1 ) # remove element with value 1 10 a. index ( b ) # f i n d index with value b 11 b in a # t e s t i f value b e x s i s t in l i s t 12 a. count ( v ) # count number o f elements with value v 13 l e n ( a ) # number o f elements in a 14 min ( a ) # s m a l l e s t element in a 15 max( a ) # l a r g e s t element in a 16 sum( a ) # add a l l elements in a 17 a. s o r t ( ) # s o r t l i s t a ( changes a ) 18 as = s o r t e d ( a ) # s o r t l i s t a ( r e t u r n s new l i s t ) 19 a. r e v e r s e ( ) # r e v e r s e l i s t a ( changes a ) 20 b [ 3 ] [ 0 ] [ 2 ] # nested l i s t indexing 3.3 Strings../code/li2.py 1 s1 = somestring with number %g % r 2 s2 = another s t r i n g 3 t e x t = 4 here you may w r i t e 6
5 a l o t o f t e x t on 6 m u l t i p l e l i n e s 7 8 # S t r i n g o p e r a t i o n s 9 s = some#s t r i n g somestring 10 s [ 8 : 1 0 ] # e x t r a c t s u b s t r i n g 11 s. f i n d ( i ) # index where i i s found 12 s. s p l i t ( # ) # s p l i t i n t o s u b s t r i n g s 13 some in s # t e s t i f s u b s t r i n g i s in s 14 s. r e p l a c e ( i, s ) 15 s. lower ( ) # lower case only 16 s. upper ( ) # upper case only 17 s. s t r i p ( ) # remove blanks 18,. j o i n ( s1 ) 3.4 Conditionals 1 i f c o n d i t i o n : 2 # block o f statements 3 e l i f c o n d i t i o n : 4 # block o f statements 5 e l s e : 6 # block o f statements 3.5 Loops 1 while c o n d i t i o n : 2 # block o f statements 3 4 f o r element in s o m e l i s t : 5 # block o f statements 6 7 # Ranges and l o o p s 8 f o r i in range (10) : 9 p r i n t ( i ) 3.6 Functions../code/str.py../code/con.py../code/loop.py 1 def f i b ( n ) : # w r i t e Fibonacci s e r i e s up to n 2 Print a Fibonacci s e r i e s up to n 7
3 a, b = 0, 1 4 while b < n : 5 p r i n t b, 6 a, b = b, a+b 7 8 # Now c a l l the f u n c t i o n we j u s t d e f i n e d : 9 f i b (2000) 3.7 Read from file../code/func.py 1 # With for loop 2 f i l e = open ( n e w f i l e. txt, r ) 3 f o r l i n e in f i l e : 4 p r i n t l i n e 5 6 # With statement 7 with open ( newtext. txt ) as f i l e : 8 data = f i l e. read ( ) 9 # do something with data 10 11 # Read i n t o l i s t 12 with open ( h e l l o. txt ) as f : 13 data = f. r e a d l i n e s ( ) 3.8 Using modules../code/read.py 1 import sys 2 3 # Will s t o r e f i r s t command l i n e argument in x 4 x = f l o a t ( sys. argv [ 1 ] ) 5 6 # Same s h i t : 7 from s y s import argv 8 x = f l o a t ( argv [ 1 ] ) 3.9 Classes../code/mod.py All functions are virtual. No private or protected variables. self is a reference to this object. Data members are prefixed by self. 8
1 c l a s s MyClass : 2 def i n i t ( s e l f, i, j ) : # c t o r 3 s e l f. i = i ; s e l f. j = j 4 5 def w r i t e ( s e l f ) : 6 p r i n t MyClass : i=, s e l f. i, j=, s e l f. j../code/simpleclass.py 4 Stuff that is nice to know 4.1 Copy, rename and remove files 1 # Copy a f i l e 2 import s h u t i l 3 s h u t i l. copy ( myfile, t m p f i l e ) 4 5 # Rename a f i l e 6 import os 7 os. rename ( myfile, tmp. 1 ) 8 9 # Remove a f i l e 10 os. remove ( mydata )../code/crr.py 4.2 Traverse directory tree 1 import os 2 3 f o r dirname, dirnames, f i l e n a m e s in os. walk (. ) : 4 # p r i n t path to a l l s u b d i r e c t o r i e s f i r s t. 5 f o r subdirname i n dirnames : 6 p r i n t os. path. j o i n ( dirname, subdirname ) 7 8 # p r i n t path to a l l f i l e n a m e s. 9 f o r f i l e n a m e in f i l e n a m e s : 10 p r i n t os. path. j o i n ( dirname, f i l e n a m e )../code/trav.py 9
5 Regular expression 5.1 Special chars. any single character except newline ˆ the beginning of the line or string $ the end of line or string * zero or more of the last character + one or more of the last character? zero or one of the last character [A-Z ] match all upper case letters [abc ] match either a or b or c [ˆb ] does not match b [ˆa-z ] does not match lower case.* any sequence of characters (except newline) [.* ] the characters. and * ˆno the string no at the beginning of a line [ˆno ] neither n or o A-Z the 3-character string A-Z (eg le) gs match eggs or legs 5.2 Common expressions \n a newline \t a tab \w any alphanumeric (word) character, the same as [a-za-z0-9) ] \W any non-word character, the same as [^a-za-z0-9) ] \d any digit, the same as [0-9] 10
\D any non-digit, the same as [^0-9] \s any white space character: space, tab, newline etc. \S any non-white space character \b a word boundary, outside [] only \B no word boundary 5.3 Quoting special characters \. a dot \ a vertical bar \[ a open square bracket \) a closing parenthesis \ˆ a hat \\ a backslash. And so on.. 5.4 Handy functions 1 import re 2 3 # compile regex pattern to regex o b j e c t 4 # to be used f o r match ( ) and search ( ) 5 re. compile ( pattern ) 6 7 # scans s t r i n g f o r a l o c a t i o n where regex 8 # produces a match. Returns MatchObject. 9 re. search ( pattern, s t r i n g ) 10 11 # i f zero or more chars at beginning o f s t r i n g 12 # the regex r e t u r n s a corresponding MatchObject. 13 re. match ( pattern, s t r i n g ) 14 15 # s p l i t s t r i n g by the o c curences o f pattern 16 re. s p l i t ( pattern, s t r i n g ) 11
17 18 # r e t u r n s a l l non o v e r l a p p i n g matches o f 19 # pattern in s t r i n g, as a l i s t o f s t r i n g s. 20 re. f i n d a l l ( pattern, s t r i n g ) 5.5 Examples of regex../code/regexfunc.py 5.5.1 Scientific or decimal or integer notation 1 # One l i n e r : 2 pattern = r?(\d \.? \ d [ Ee][+\ ]?\d+ (\d+\.\d \ d \.\ d+) \ d +) 3 4 # Modularized : 5 r e a l i n = r \d+ 6 r e a l d n = r (\ d+\.\d \ d \.\ d+) 7 r e a l s n = r (\ d \.? \ d [ Ee][+\ ]?\d+ 8 r e a l =?( + r e a l s n + + r e a l d n + + r e a l i n + ) 6 Numerical Python../code/regexnum.py NumPy enables efficient numerical computing in Python. This is a package of modules, which offers efficient arrays with associated array operations coded in C or Fortran. 1 from numpy import 6.1 Operations on NumPy arrays 1 array ( ) # c r e a t e s a new array 2 z e r o s ( x ) # f i l l s the new array with x z e r o s 3 shape ( v,w) # turns i t i n t o a v times w matrix 4 reshape ( v,w) # r e t u r n s an array with the same 5 # data, but a new shape 6 f i l l ( x ) # f i l l array with s c a l a r value x 7 copy # r e t u r n s a copy o f the array 8 9 # Basic array indexing 12
10 a [ 2 : 4 ] = 1 # s e t a [ 2 ] and a [ 3 ] equal to 1 11 a [ 1] = a [ 0 ] # s e t l a s t elem equal to f i r s t elem 12 a [ : ] = 0 # s e t a l l elements to 0 13 a [ i, j ] = 10 # assignment to element ( i, j ) 14 p r i n t a [ :, k ] # p r i n t column with index k 15 p r i n t a [ 1, : ] # p r i n t second row 16 p r i n t a [ 0, 1 ] # p r i n t element ( 0, 1 )../code/numpy.py 6.2 Example of Python and NumPy 6.2.1 Vectorization of a function 1 #! / usr / bin /env python 2 from numpy import 3 4 def i n i t i a l c o n d ( x ) : 5 i f type ( x ) == type ( array ( [ ] ) ) : 6 r e s u l t = z e r o s ( x. shape ) 7 r e s u l t. f i l l ( 3. 0 ) 8 r e turn r e s u l t 9 10 i f name == m a i n : 11 p r i n t i n i t i a l c o n d ( array ( z e r o s ( 6 ) ). reshape ( 3, 2 ) )../code/numpyvec.py 7 Web applications in Python 7.1 CGI Example with Python, HTML and CGI. Computes sine of a number from user input. 1 <html> 2 <form a c t i o n= hwhtml. py. c g i method= post > 3 Hello, World! The s i n e o f 4 <input type= t e x t name= r s i z e= 10 value= 1. 2 > 5 <input type= submit value= e q u a l s name= equalsbutton > 6 </ form> 7 </html>../code/hw.html 13
1 #! / s t o r e / bin /python 2 import cgi, math 3 p r i n t Content type : t e x t /html\n 4 5 form = c g i. F i e l d S t o r a g e ( ) 6 r = form. g e t v a l u e ( r ) 7 i f r i s not None : 8 s = s t r ( math. s i n ( f l o a t ( r ) ) 9 e l s e : 10 s = ; r =../code/hwhtml.py Do not bother to start with CGI. Rather check out Django. References [1] Langtangen, Hans Petter and Sundnes, Joakim, Lectures. University of Oslo, Dept. of Informatics, 2013. [2] Python documentation 14