The Current Topic: Python Announcements: Lecture Room
The Current Topic: Python Announcements: Lecture Room
Announcements
Lab 1 is due Monday at 10:30 am.
Don't wait till the last minute to figure out how to submit.
! Introduction
Object-oriented programming: Python
submitcsc326f -l 1
(the character after the dash is a lower case L) to see a list of the files you
submitted, the size of each file, and the time each file was submitted. Your list of
files should include ex1.py, ex2.py, ex3.py, ex4.py, ex5.py, ex6.py, and
MyList.py.
Fall 2008
Fall 2008
Regular expressions
Regular expressions
Fall 2008
string
aab
bab
bab
succeeds?
Yes
No
Yes
RE
a*
a*
a*
string
aab
bab
bbb
succeeds?
Yes
Yes
Yes
a*c
a*c
(ab)*c
(ab)*cd
a*b*d
a*b*d
a*ba*b*
abc
cde
ababc
ababdcd
aaabbbd
bbd
bac
No
Yes
Yes
No
Yes
Yes
Yes
Fall 2008
match
aa
(empty string)
(empty string)
c
ababc
aaabbbd
bbd
ba
Regular expressions
Regular expressions
string
aab
bab
succeeds?
Yes
No
a+c
a+bc
a+bc
(ab)+c
(ab)+cd
a+b+d
a+b+d
a+(ba)+c
abc
abc
aaabc
ababc
ababdcd
aaabbbd
bbd
ababac
No
Yes
Yes
Yes
No
Yes
No
Yes
Fall 2008
match
aa
abc
aaabc
ababc
aaabbbd
ababac
RE
a?
a?
string
aab
bab
succeeds?
Yes
Yes
a?c
a?bc
a?bc
(ab)?c
(ab)?cd
a?b?d?
a?b?d
a?(ba)?c
abc
abc
aaabc
cdef
ababdcd
dddd
bbd
acbaba
No
Yes
No
Yes
No
Yes
No
Yes
Fall 2008
Regular expressions
ac.
a.b*
a?..c
c?.e*f
(ab)?.b
d..de*
Fall 2008
string
aab
bab
succeeds?
Yes
Yes
abc
abc
aaabc
cdef
ababdcd
dddd
No
Yes
No
Yes
Yes
Yes
c
d
ac
match
aab
bab
Examples:
RE
.{1,3}
b{2}
b{2,4}
a{0,3}
(ab){2}.
ab
cdef
abab
dddd
abc
Regular expressions
match
a
(empty string)
Fall 2008
string
aab
bab
bbbac
aaabc
ababdcd
succeeds?
Yes
No
Yes
Yes
Yes
match
aab
bbb
aaa
ababd
Regular expressions
Regular expressions
string
aab
ababdb
ababdb
abab
abcdf
Fall 2008
succeeds?
Yes
Yes
Yes
No
No
match
aab
bab
bdb
Fall 2008
Regular expressions
10
Special characters:
equivalent to [0-9A-Za-z_]
Fall 2008
11
Fall 2008
12
Examples:
import re
m1 = re.search('a*b', 'aaaabb')
m1 == None
# False
m1.group()
# 'aaaab'
m.group() is the actual match !that is, the part of string s that matches
expression r.
m.start() and m.end() give the location of the match that is, the match is
s[m.start():m.end()].
m2 = re.search('a*b', 'cab')
m2 == None
# False
m2.group()
# 'ab'
m3 = re.match('a*b', 'cab')
m3 == None
# True
m4 = re.search('^a*b', 'cab')
m4 == None
# True
Fall 2008
13
Fall 2008
14
For example, since the symbol * has a special meaning, we use \* to mean that we
actually want to match a star.
For example,
print '\\'
actually prints a single backslash, not two.
Fall 2008
15
Fall 2008
16
This produces a regular expression object, which you can use any number of
times to perform matches by calling its search(), match(), and findall()
methods.
r = re.compile(r'a*b')
m1 = r.search('aab')
m1.group()
# 'aab'
For example,
print r'\\ \n'
actually prints '\\ \n', not a single slash followed by a newline.
m2 = r.match('bdca')
m2.group()
# 'b'
Fall 2008
17
Fall 2008
remoney = re.compile(r'(.*)(\d*\.\d\d)')
r = remoney.search('Current balance = 12345.67')
r.groups()
18
remoney = re.compile(r'(.*?)(\d*\.\d\d)')
r = remoney.search('Current balance = 12345.67')
Observe that when a regular expression includes parts that are enclosed
in brackets (there parts are called subpatterns), we can use the match
object's groups() method to find the parts of the match corresponding
to each subpattern.
r.groups()
19
Fall 2008
20
We can use the re.sub() and re.subn() functions (and the sub()
and subn() methods of regular expression objects) to replace matches
with a different string.
Fall 2008
21
Fall 2008
Doc strings
22
Doc strings
A simple example:
The help() function (that we saw earlier) displays the contents of doc strings.
def f(x, y):
"""Returns the sum of x and y."""
Modules
Classes
Functions
Methods
return x+y
Note that the first line of the doc string has to have the same indentation
as the rest of the function body.
To use multi-line doc strings, we need to use the multi-line version of string
literals. These begin and end with """ (three double-quotes).
For consistency, it's a good idea to always begin and end doc strings with """,
even if they only use a single line. This also makes it easier to add lines to the doc
string later on.
Fall 2008
help(f)
Output:
Help on function f in module __main__:
f(x, y)
Returns the sum of x and y.
23
Fall 2008
24
Doc strings
Doc strings
A multi-line example:
def __init__(self):
"""Constructor."""
pass
return x/y
Fall 2008
25
Fall 2008
Doc strings
26
Exercises
class A(__builtin__.object)
| Represents something...
|
| Methods defined here:
|
| __init__(self)
|
Constructor.
|
| m(self, x)
|
Does something.
|
|
x -- Some parameter.
|
|
-------------------------------------------------------------
Fall 2008
27
Fall 2008
28