Notater: INF3331 Contents Veronika Heimsbakk

advertisement
Notater: INF3331
Veronika Heimsbakk
veronahe@student.matnat.uio.no
December 4, 2013
Contents
1 Introduction
3
2 Bash
2.1 Variables .
2.2 Loops . .
2.2.1 For
2.3 Pipes . . .
2.4 Functions
.
.
.
.
.
3
3
4
4
4
5
.
.
.
.
.
.
.
.
.
5
5
6
6
7
7
7
8
8
8
4 Stuff that is nice to know
4.1 Copy, rename and remove files . . . . . . . . . . . . . . . . . .
4.2 Traverse directory tree . . . . . . . . . . . . . . . . . . . . . .
9
9
9
. . .
. . .
loop
. . .
. . .
3 Basic Python
3.1 Variables . . . .
3.2 Lists and tuples
3.3 Strings . . . . .
3.4 Conditionals . .
3.5 Loops . . . . .
3.6 Functions . . .
3.7 Read from file .
3.8 Using modules .
3.9 Classes . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5 Regular expression
5.1 Special chars . . . . . . . . . . . . .
5.2 Common expressions . . . . . . . . .
5.3 Quoting special characters . . . . . .
5.4 Handy functions . . . . . . . . . . . .
5.5 Examples of regex . . . . . . . . . . .
5.5.1 Scientific or decimal or integer
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
notation
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
10
10
10
11
11
12
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
2
3
4
5
6
7
8
9
x = 3
y = 2
z = $x+$y
echo $z
# output 3+2
z = $ ( ( x+y ) )
( ( v=x+y ) )
l e t w = x+y
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
# safe
2 i f [ $ ? != 0 ] ; then
# may be u n s a f e
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
2.2.1
Loops
For loop
Following script is removing all .tmp-files at its location.
1
2
3
4
5
6
7
8
f i l e s =‘/ b i n / l s ∗ . tmp ‘
# use / bin / l s in case l s i s a l i a s e d
for f i l e in $ f i l e s
do
echo removing $ f i l e
rm −f $ f i l e
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 / b i n / l s −s | s o r t −r
4
2.4
Functions
Example of a function with argument:
1
2
3
4
5
6
7
8
9
function quit
exit
}
function e {
echo $1
}
e Hello
#
e World
#
quit
#
3
{
c a l l i n g e with argument H e l l o
c a l l i n g e with argument World
c a l l i n g quit
Basic Python
1 #! / u s r / b i n / env python
2
3 p r i n t ” H e l l o , 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
2
3
4
5
6
7
8
a
a
a
a
=
=
=
=
3
3.0
’3 ’
[ ’1 ’ , 2]
# t e s t f o r var type
i f isinstance (a , int ) :
# int ?
i f isinstance (a , ( l i s t , tuple ) :
# l i s t or tuple ?
../code/var.py
Common types in Python:
• Numbers: int, float, complex
• Sequences: str (string), list, tuple, NumPy array
5
• Mappings: dict (dictionary/hash)
• User-defined type in terms of a class
3.2
1
2
3
4
5
Lists and tuples
mylist = [ ’ a ’ , 2 . 5 , 42 , ’b ’ ]
mytuple = ( ’ a ’ , 2 . 5 , 4 2 , ’ b ’ ]
m y l i s t [ 1 ] = −10
m y l i s t . append ( ’ c ’ )
mytuple [ 1 ] = −10 # i l l e g a l , cannot change a t u p l e
../code/li.py
List functionality
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
a = []
a = [1 , ’b ’ ]
a . append ( e )
a + [1 ,3]
a[3]
a [ −1]
a[1:3]
del a [ 3 ]
a . remove ( 1 )
a . index ( ’b ’ )
’b ’ in a
a . count ( v )
len (a)
min ( a )
max( a )
sum ( a )
a . sort ()
as = sorted ( a )
a . reverse ()
b[3][0][2]
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
i n i t empty l i s t
init a list
add e t o end o f l i s t
add two l i s t s
index a l i s t element
get l a s t l i s t element
copy data t o s u b l i s t , h e r e : i n d e x 1 , 2
d e l e t e element at index 3
remove e l e m e n t with v a l u e 1
f i n d i n d e x with v a l u e ’ b ’
t e s t i f value ’b ’ e x s i s t in l i s t
count number o f e l e m e n t s with v a l u e v
number o f e l e m e n t s i n a
s m a l l e s t element in a
l a r g e s t element in a
add a l l e l e m e n t s i n a
s o r t l i s t a ( changes a )
s o r t l i s t a ( r e t u r n s new l i s t )
r e v e r s e l i s t a ( changes a )
nested l i s t indexing
../code/li2.py
3.3
Strings
1 s 1 = ” s o m e s t r i n g with number %g ” % r
2 s2 = ’ another s t r i n g ’
3 t e x t = ”””
4
h e r e you may w r i t e
6
5
6
7
8
9
10
11
12
13
14
15
16
17
18
a l o t o f t e x t on
m u l t i p l e l i n e s ”””
# String operations
s = ’ some#s t r i n g s o m e s t r i n g ’
s [8:10]
# extract substring
s . find ( ’ i ’ )
# i n d e x where ’ i ’ i s found
s . s p l i t ( ’#’ )
# s p l i t into substrings
’ some ’ i n s
# test i f substring i s in s
s . replace ( ’ i ’ , ’ s ’ )
s . lower ()
# lower case only
s . upper ( )
# upper c a s e o n l y
s . strip ()
# remove b l a n k s
’ , ’ . j o i n ( s1 )
../code/str.py
3.4
Conditionals
1 i f condition :
2
# block of statements
3 e l i f condition :
4
# block of statements
5 else :
6
# block of statements
../code/con.py
3.5
1
2
3
4
5
6
7
8
9
Loops
while condition :
# block of statements
f o r element in s o m e l i s t :
# block of statements
# Ranges and l o o p s
f o r i in range (10) :
print ( i )
../code/loop.py
3.6
Functions
1 def f i b (n) :
# w r i t e F i b o n a c c i s e r i e s up t o n
2
” P r i n t a F i b o n a c c i s e r i e s up t o n”
7
3
a, b = 0, 1
4
while b < n :
5
print b ,
6
a , b = b , a+b
7
8 # Now c a l l t h e 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)
../code/func.py
3.7
1
2
3
4
5
6
7
8
9
10
11
12
13
Read from file
# With f o r −l o o p
f i l e = open ( ’ n e w f i l e . t x t ’ , ’ r ’ )
for l i n e in f i l e :
print line
# With s t a t e m e n t
with open ( ” newtext . t x t ” ) a s f i l e :
data = f i l e . r e a d ( )
# do something with data
# Read i n t o l i s t
with open ( h e l l o . t x t ) a s f :
data = f . r e a d l i n e s ( )
../code/read.py
3.8
1
2
3
4
5
6
7
8
Using modules
import s y s
# W i l l s t o r e f i r s t command l i n e argument i n x
x = f l o a t ( s y s . argv [ 1 ] )
# Same s h i t :
from s y s import argv
x = f l o a t ( argv [ 1 ] )
../code/mod.py
3.9
Classes
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 ( self , i , j ) : # ctor
3
self . i = i ; self . j = j
4
5
def write ( 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
4.1
1
2
3
4
5
6
7
8
9
10
Stuff that is nice to know
Copy, rename and remove files
# Copy a f i l e
import s h u t i l
s h u t i l . copy ( m y f i l e , t m p f i l e )
# Rename a f i l e
import o s
o s . rename ( m y f i l e , ’ tmp . 1 ’ )
# Remove a f i l e
o s . remove ( ’ mydata ’ )
../code/crr.py
4.2
Traverse directory tree
1 import o s
2
3 f o r dirname , dirnames , f i l e n a m e s i n o s . walk ( ’ . ’ ) :
4
# p r i n t path t o 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 o s . path . j o i n ( dirname , subdirname )
7
8
# p r i n t path t o a l l f i l e n a m e s .
9
for filename in filenames :
10
p r i n t o s . path . j o i n ( dirname , f i l e n a m e )
../code/trav.py
9
5
5.1
Regular expression
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Handy functions
import r e
# compile regex pattern to regex o b j e c t
# t o be used f o r match ( ) and s e a r c h ( )
re . compile ( pattern )
# s c a n s s t r i n g f o r a l o c a t i o n where r e g e x
# p r o d u c e s a match . Returns MatchObject .
re . search ( pattern , s t r i n g )
# i f z e r o o r more c h a r s a t b e g i n n i n g o f s t r i n g
# t h e r e g e x r e t u r n s a c o r r e s p o n d i n g MatchObject .
r e . match ( p a t t e r n , s t r i n g )
# s p l i t s t r i n g by t h e o c c u r e n c e s o f p a t t e r n
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 # p a t t e r n i n s t r i n g , a s a l i s t o f s t r i n g s .
20 r e . f i n d a l l ( p a t t e r n , s t r i n g )
../code/regexfunc.py
5.5
5.5.1
Examples of regex
Scientific or decimal or integer notation
1 # One−l i n e r :
2 p a t t e r n = 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 + ’ )
’
../code/regexnum.py
6
Numerical Python
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
1
2
3
4
5
6
7
8
9
Operations on NumPy arrays
array ()
zeros (x)
shape ( v , w)
r e s h a p e ( v , w)
f i l l (x)
copy
#
#
#
#
#
#
#
c r e a t e s a new a r r a y
f i l l s t h e new a r r a y with x z e r o s
t u r n s i t i n t o a v t i m e s w matrix
r e t u r n s an a r r a y with t h e same
data , but a new shape
f i l l a r r a y with s c a l a r v a l u e x
r e t u r n s a copy o f t h e a r r a y
# Basic array indexing
12
10
11
12
13
14
15
16
a [ 2 : 4 ] = −1
a [ −1] = a [ 0 ]
a[:] = 0
a [ i , j ] = 10
print a [ : , k ]
print a [ 1 , : ]
print a [0 ,1]
#
#
#
#
#
#
#
s e t a [ 2 ] and a [ 3 ] e q u a l t o −1
s e t l a s t elem e q u a l t o f i r s t elem
s e t a l l elements to 0
assignment to element ( i , j )
p r i n t column with i n d e x k
p r i n t s e c o n d row
p r i n t element ( 0 , 1 )
../code/numpy.py
6.2
6.2.1
1
2
3
4
5
6
7
8
9
10
11
Example of Python and NumPy
Vectorization of a function
#! / u s r / b i n / env python
from numpy import ∗
def i n i t i a l c o n d (x) :
i f type ( x ) == type ( a r r a y ( [ ] ) ) :
r e s u l t = z e r o s ( x . shape )
result . f i l l (3.0)
return r e s u l t
if
name
== ” m a i n ” :
print i n i t i a l c o n d ( array ( zeros (6) ) . reshape (3 ,2) )
../code/numpyvec.py
7
7.1
Web applications in Python
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=” p o s t ”>
3
H e l l o , World ! The s i n e o f
4
<i n p u t type=” t e x t ” name=” r ” s i z e=” 10 ” v a l u e=” 1 . 2 ”>
5
<i n p u t type=” submit ” v a l u e=” e q u a l s ” name=”
e q u a l s b u t t o n ”>
6
</ form>
7 </ html>
../code/hw.html
13
1
2
3
4
5
6
7
8
9
10
#! / s t o r e / b i n / python
import c g i , math
p r i n t ” Content−type : t e x t / html \n”
form = c g i . F i e l d S t o r a g e ( )
r = form . g e t v a l u e ( ’ r ’ )
i f r i s not None :
s = s t r ( math . s i n ( f l o a t ( r ) )
else :
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
Download