IS 313 Today
Schedule
Where we've been…
Week 0:
Week 1:
files
Week 4:
Functions
and Data
Week 2:
Week 5:
Loops and
language
Week 6:
Where we're going…
Week 8:
Week 9:
Week 12:
Dictionaries
and Classes
Week 10:
dictionaries
Week 13:
Week 14:
You're saying that
Python's got
class?
Projects
Tomorrow's HW
Problem #1: Project idea
Key! This is NOT the final project!
This just a chance to create an
assignment of your own!
Tomorrow's HW
Problem #1: Project idea
Key! This is NOT the final project!
This just a chance to create an
assignment of your own!
If you want to try something out, suggest something!
I'd be excited to help put together a feasible plan for a
two-week pair of assignments:
• first half would be due on 11/2
• final version would be due 11/9
Then you could reassess to see if you'd like to continue...
Tomorrow's HW
ideas?
Image-salon web
application
Google maps application
https://www.cs.hmc.edu/twiki/bin/
view/CS5/GoogleMapsPart1
Problem #1: Project idea
Thanks to Alex Hagen !
Do it yourself datastructures...
Files
In Python reading files is no problem…
>>> f = file( 'a.txt' )
>>> text = f.read()
>>> text
'This is a file.\nLine 2\nLast line!\n'
use split as needed…
>>> f.close()
Files
In Python reading files is no problem…
>>> f = file( 'a.txt' )
opens the file and calls it f
>>> text = f.read()
reads the whole file and calls it f
>>> text
'This is a file.\nLine 2\nLast line!\n'
use text.split() for a list of each raw word
>>> f.close()
closes the file (closing
Python does the same)
use split as needed…
for example, text.split( '\n' )
split
Breaking up is easy to do… !
>>> text = f.read()
'This is a file.\nLine 2\nLast line!\n'
>>> text.split()
['This', 'is', 'a', 'file.', 'Line', '2', 'Last', 'line!']
>>> text.split('\n')
['This is a file.', 'Line 2', 'Last line!', '']
>>> text.split('in')
use split as needed…
How many pieces will there be?
split
Breaking up is easy to do… !
>>> text = f.read()
'This is a file.\nLine 2\nLast line!\n'
>>> text.split()
['This', 'is', 'a', 'file.', 'Line', '2', 'Last', 'line!']
>>> text.split('\n')
['This is a file.', 'Line 2', 'Last line!', '']
>>> text.split('in')
use split as needed…
['This is a file.\nL', 'e 2\nLast l', 'e!\n']
Whoa – how many elements are in these lists?
File writing…
is as simple as print !
f = file( 'newfile.txt', 'w' )
print >> f, "This is in the file"
opens the file
for writing
print >> f, "named newfile.txt"
x = 42
print >> f, "My favorite number is", x
f.close()
File writing…
is as simple as print !
f = file( 'newfile.txt', 'w' )
print >> f, "This is in the file"
print >> f, "named newfile.txt"
x = 42
print >> f, "My favorite number is", x
f.close()
Try it!
def wordCount( filename ):
Write a function wordCount
that counts the number of words
in a file named filename and
then returns the result.
>>> wordCount( 'a.txt' )
8
Try it!
def wordCount( filename ):
Write a function wordCount
that counts the number of words
in a file named filename and
then returns the result.
def wordList( filename ):
>>> wordCount( 'a.txt' )
8
>>> wordList( 'a.txt' )
Write a function wordList that reads
filename and then it writes a new
file named 'words.txt' which is a
list of the words present, one per line.
Thought experiment…
def wordChal( filename ):
How could you get a count of the
number of times that each different
word was present in the file?
>>> wordChal( 'a.txt' )
Lists vs. Dictionaries
Lists are not perfect…
If I had a dictionary, I guess
I could look up what it was!
reference
L
5
42
L[0]
L[1]
Lists vs. Dictionaries
Lists are not perfect…
You can't choose what to name data.
L[0], L[1], …
If I had a dictionary, I guess
I could look up what it was!
reference
L
5
42
L[0]
L[1]
Lists vs. Dictionaries
Lists are not perfect…
You can't choose what to name data.
L[0], L[1], …
You have to start at 0.
L[1985] = 'ox'
L[1986] = 'tiger'
If I had a dictionary, I guess
I could look up what it was!
reference
L
5
42
L[0]
L[1]
Lists vs. Dictionaries
Lists are not perfect…
You can't choose what to name data.
L[0], L[1], …
You have to start at 0.
L[1985] = 'ox'
L[1986] = 'tiger'
Some operations can be slow for big lists …
if 'tiger' in L:
If I had a dictionary, I guess
I could look up what it was!
reference
L
5
42
L[0]
L[1]
This seems like the key to
dictionaries' value…
Lists vs. Dictionaries
In Python a dictionary is a set of key - value pairs.
>>> d = {}
>>> d[1985] = 'ox'
>>> d[1986] = 'tiger'
>>> d
{1985: 'ox', 1986: 'tiger'}
>>> d[1985]
'ox'
>>> d[1969]
key error
It's a list where the index can
be any immutable-type key.
This seems like the key to
dictionaries' value…
Lists vs. Dictionaries
In Python a dictionary is a set of key - value pairs.
>>> d = {}
creates an empty dictionary, d
>>> d[1985] = 'ox'
1985 is the key
'ox' is the value
>>> d[1986] = 'tiger'
>>> d
1986 is the key
'tiger' is the value
{1985: 'ox', 1986: 'tiger'}
>>> d[1985]
Anyone seen
this before?
Retrieve data as with lists…
'ox'
>>> d[1969]
key error
or almost !
It's a list where the index can
be any immutable-type key.
More on dictionaries
Dictionaries have lots of built-in methods:
>>> d = {1985: 'ox', 1986: 'tiger'}
>>> d.keys()
get all keys
[ 1985, 1986 ]
>>> 1986 in d
True
check if a key is present
>>> 1969 in d
False
>>> d.pop( 1985 )
'ox'
delete a key
(and its value)
And if it's not present?
They don't seem
moronic to me!
A challenge…
states = { 'CA': 0, … }
def stateChallenge( states ):
""" states is a dictionary of the US western states
-- the challenge is to name them all! """
while True:
guess = raw_input("Name a western state: ")
if guess in states == False:
print 'Try again...'
elif states[guess] == 0:
print 'Yes!'
states[guess] += 1
else:
print 'Already guessed...'
print 'Phew!'
help?!
A family
dictionary?
A family dictionary…
T = {'abe'
:['homer','herb'],
'jackie':['marge','patty','selma'],
'homer' :['hugo','bart','lisa','maggie'],
'marge' :['hugo','bart','lisa','maggie']}
keys can be any immutable
type
T['abe']
How to get
'selma' from T?
values can be any type at all…
A functional family?
def favChild( person, Tree ):
""" person is a name (a string)
Tree is a dictionary of children
returns person's favorite child """
if person in Tree:
Kids = Tree[person]
Kids.sort()
return Kids[0]
else:
return 'no children'
Who is favored ?
Side effects ?
A functional family?
For example,
>>> addChild( 'lisa', T, 'abejr' )
def addChild( person, Tree, jr ):
""" adds person's new child to Tree """
Based on favChild, write favGChild to
return the first grandchild alphabetically - or
return 'no one' if there are none.
def favChild( person, Tree ):
if person in Tree:
Kids = Tree[person]
Kids.sort()
return Kids[0]
else:
return 'no children'
def favGChild( person, Tree ):
our list of GChildren
GC = []
Name that author… ?
Name that author… ?
Markov Model
Technique for modeling any sequence of natural data
Each item depends on only the item immediately before it .
The text file:
The Model:
I like spam. I like toast and spam.
I eat ben and jerry's ice cream too.
1st-order
Markov
Model
Markov Model
Technique for modeling any sequence of natural data
Each item depends on only the item immediately before it .
The text file:
The Model:
1st-order
Markov
Model
I like spam. I like toast and spam.
I eat ben and jerry's ice cream too.
{ 'toast': ['and'],
'and' : ['spam.', "jerry's"],
'like' : ['spam.', 'toast'], …
'ben' :
Markov Model
Technique for modeling any sequence of natural data
Each item depends on only the item immediately before it .
The text file:
The Model:
I like spam. I like toast and spam.
I eat ben and jerry's ice cream too.
{ 'toast':
'and' :
'like' :
'ben' :
'I'
:
['and'],
['spam.', "jerry's"],
['spam.', 'toast'],
['and'],
Markov Model
Technique for modeling any sequence of natural data
Each item depends on only the item immediately before it .
The text file:
The Model:
How to get to I?
I like spam. I like toast and spam.
I eat ben and jerry's ice cream too.
{ 'toast':
'and' :
'like' :
'ben' :
'I'
:
['and'],
['spam.', "jerry's"],
['spam.', 'toast'],
['and'],
['like', 'like', 'eat']
Markov Model
Technique for modeling any sequence of natural data
Each item depends on only the item immediately before it .
The text file:
The Model:
I like spam. I like toast and spam.
I eat ben and jerry's ice cream too.
{ 'toast':
'and' :
'like' :
'ben' :
'I'
:
'$'
:
['and'],
['spam.', "jerry's"],
['spam.', 'toast'],
['and'],
['like', 'like', 'eat'],
['I', 'I', 'I'],
sentence-starting string
Generative Markov Model
Technique for modeling any sequence of natural data
Each item depends on only the item immediately before it .
A key benefit is that the model can generate feasible data!
Generating text:
1) start with the '$' string
2) choose a word following '$', at random. Call it w
3) choose a word following w, at random. And so on…
4) If w ends a sentence, '$' becomes the next word.
Generative Markov Model
Technique for modeling any sequence of natural data
Each item depends on only the item immediately before it .
A key benefit is that the model can generate feasible data!
I like spam. I like spam. I like toast and jerry's ice cream too.
Generating text:
1) start with the '$' string
2) choose a word following '$', at random. Call it w
3) choose a word following w, at random. And so on…
4) If w ends a sentence, '$' becomes the next word.
Generative Markov Model
Technique for modeling any sequence of natural data
Each item depends on only the item immediately before it .
A key benefit is that the model can generate feasible data!
I like spam. I like spam. I like toast and jerry's ice cream too.
Original
text:
Generating
text:
I like spam. I like toast and spam.
eatthe
ben
andstring
jerry's ice cream too.
1) start Iwith
'$'
2) choose a word following '$', at random. Call it w
3) choose a word following w, at random. And so on…
4) If w ends a sentence, '$' becomes the next word.
Creating a Markov Model
def createDictionary( fileName ):
Generating Markov text
def generateText( d, n ):
d is the dictionary, and n is the
number of words to generate…
Number of distinct words?
Shakespeare used 31,534 different words and a grand total of
884,647 words counting repetitions (across his works)
http://www-math.cudenver.edu/~wbriggs/qr/shakespeare.html
Active vocabulary estimates range from 10,000-60,000.
Passive vocabulary estimates are much higher.
Many coinages:
Shakespeare
J. K. Rowling
WMSCI 2005
http://pdos.csail.mit.edu/scigen/
WMSCI 2005
Randomly-generated submission
accepted to WMSCI 2005
http://pdos.csail.mit.edu/scigen/
No end to the WMSCI emails…
Two other problem options
HW6, Problem 1:
pi from pie!
HW6, Problem 4:
the Mandelbrot set
Easy as p
(1,1)
Visualize:
Pie
(-1,-1)
Loops: for or while?
pi_one(e)
e == how close to π
we need to get
pi_two(n)
n == number of
darts to throw
Which function will use which kind of loop?
Lab 9: the Mandelbrot Set
Consider the following update rule for
all complex numbers c:
z0 = 0
zn+1 = zn2 + c
Benoit M.
If c does not diverge, it's in the M. Set.
Imaginary axis
z3
c
z1
z0
z2
z4
Real axis
Nov. 20, 1924 –
Oct. 14, 2010
Lab 9: the Mandelbrot Set
Consider the following update rule for
all complex numbers c:
z0 = 0
zn+1 = zn2 + c
Benoit M.
If c does not diverge, it's in the M. Set.
Nov. 20, 1924 –
Oct. 14, 2010
Imaginary axis
z3
c
z1
z0
z2
z4
Real axis
example of a non-diverging cycle in blue
Lab 9: the Mandelbrot Set
Consider the following update rule for
all complex numbers c:
z0 = 0
zn+1 = zn2 + c
The shaded area are points that do not diverge.
Python and images
from cs5png import *
for creating and saving images
image = PNGImage( 300, 200 )
creates a bitmap object
and names it image
What might the inputs be for?
Eco-friendly
pngs!
Python and images
from cs5png import *
for creating and saving images
image = PNGImage( 300, 200 )
creates a bitmap object
and names it image
inputs are height, width
objects are software abstractions
containing structured information
Eco-friendly
pngs!
Python and images and objects
from cs5png import *
image = PNGImage( 300, 200 )
here, a bitmap object named image
is calling an internal method named saveFile
image.saveFile( )
objects are variables that can contain
their own functions, often called methods
Python and images and objects
from cs5png import *
image = PNGImage( 300, 200 )
another
internal
method
image.plotPixel( 150, 100 )
image.plotPixel( 42, 42, (42,0,0) )
image.saveFile( )
objects are variables that can contain
their own functions, often called methods
taking 2 or 3
inputs...
from cs5png import *
def test():
""" image demonstration """
image = PNGImage( 200, 200 )
Imagining
Images
# a 2d loop over each pixel
for col in range(width):
for row in range(height):
if col == row:
image.plotPoint( col, row )
to see progress?
image.saveFile( )
thicker diagonal?
other diagonal / both?
triangle?
Mandelbrot Set: properties
The set is connected.
The set's boundary is a fractal of dimension 2 (!)
"infinite detail"
The points nearer the set escape to more slowly.
can color points based on
how quickly they escape…
not 100% self-similar but quasi-self-similar:
http://www.youtube.com/watch?v=gEw8xpb1aRA
Jonathan Coulton wrote a song about it
Nature > Imagination?
Happy Mandelbrotting!
www.cs.hmc.edu/~jgrasel
Software Engineering
Building atop the
work of others…
creating and
composing
functions
Insight into details, e.g.,
data storage and retrieval.
loops and
data handling
Invention, not reinvention.
creating new
data structures
Classes & Objects
An object-oriented programming language allows you
to build your own customized types of variables.
(1) A class is a type of variable.
(2) An object is one such variable.
Classes & Objects
An object-oriented programming language allows you
to build your own customized types of variables.
(1) A class is a type of variable.
(2) An object is one such variable.
There will typically
be MANY objects
of a single class.
Using objects and classes:
A particularly appropriate reference …
>>> d = dict()
>>> d['answer'] = 42
an example of the
dictionary constructor
>>> dir(d)
all of the data members and methods of the dictionary class (and thus the object d !)
>>> d.keys()
[ 'answer' ]
>>> d.values()
[ 42 ]
two methods (functions) within
all objects of class dictionary
The CONSTRUCTOR …
Code
class dict:
""" a dictionary class """
def __init__( self ):
""" the CONSTRUCTOR """
# sets all of the data needed
# there's no data here!
defines a new
datatype called
dict
the constructor is
named __init__
but is used via the
class name
It sets all the
Comments
DATA MEMBERS
of a new
object
the object is
called self
The REPR …
Code
class dict:
""" a dictionary class """
def __repr__( self ):
""" a grim method """
s = '{'
for key in self.keys():
s += str(key) + ':'
s += str( self[key] ) + ', '
s += '}'
return s
this method is named
__repr__ and it
provides a string that will
be used when printing
the object
being printed
is called
self
the string to
print gets
returned
Comments
Object-oriented programming
Do-it-yourself data structures!
class: your own TYPE of data
object: a variable of your own class type
data members: data an object contains
Blueprint for
an object
variables of
that type
methods: functions an object contains
benefits: encapsulation & abstraction
An object is alive,
responsible, and intelligent.
- C++ FAQs
Examples…
Python's class libraries…
Graphics libraries
Do reference
libraries have
library
references?
http://docs.python.org/lib/
>>> f = urllib.urlopen( 'http://www.cs.hmc.edu/~dodds/IS313/HW/index.html' )
>>> text = f.read()
>>> text[:50]
Objects
An object is a data structure (like a list), except
(1) Its data elements have names chosen by the programmer.
(2) Data elements are chosen & organized by the programmer
(3) An object can have behaviors built-in by the programmer.
usually called "methods"
instead of functions
User-defined structures that become part of
the language (at least locally…)
But Why ?
• Flexibility
create-your-own
• Reusability
write once, take anywhere
• Abstraction
worry once, use anywhere
ordinary data structures
Date
The Problem
The Design
This is a class. It is a user-defined datatype
(that you'll build in Lab this week!)
We want (or need) to
represent lots of
interacting calendar days
What data should be set
in the constructor?
What functionality will we
need to support?
Are you asking what
Date-a a Date needs?
d.dow()
usually some, but not all,
is known beforehand
Using Dates
>>> d = Date(1,1,2008)
this is a CONSTRUCTOR …
What does
it do?
>>> d
1/1/2008
this is an object of type Date
the representation of a particular object of type Date
>>> d.isLeapYear()
True
the isLeapYear method returns
True or False. How does it know
what year to check?
>>> d2 = Date(12,31,2007)
>>> d2
Another object of type Date again, via the constructor.
12/31/2007
>>> d2.isLeapYear()
False
How does it know to return False,
instead of True in this case ??
class Date:
def __init__( self, m, d, y ):
""" the Date constructor """
self.month = m
self.day = d
self.year = y
def __repr__( self ):
""" used for printing Dates """
s = "%02d/%02d/%04d" % (self.month,
return s
def isLeapYear( self ):
The Date
class
self.day, self.year)
Why is everyone
so far away?!
self
is the specific OBJECT THAT
CALLS A METHOD
>>> d = Date(1,1,2008)
>>> d
1/1/2008
These methods need
access to the object
that calls them
>>> d.isLeapYear()
True
>>> d2 = Date(12,31,2007)
>>> d2
12/31/2007
These methods need
access to the object
that calls them
>>> d2.isLeapYear()
False
Why not use d?
a Leap of faith….
class Date:
def __init__( self, mo, dy, yr ): (constructor)
def __repr__(self): (for printing)
def isLeapYear( self ):
""" here it is """
if self.yr%400 == 0: return True
if self.yr%100 == 0: return False
if self.yr%4 == 0: return True
return False
How about a
4000-year rule?
Date objects are mutable
>>> d = Date(1,1,2008)
but only if we
want them to be!
always created with the
CONSTRUCTOR …
>>> d
01/01/2008
>>> d.yesterday()
>>> d
the yesterday method returns
nothing at all. Is it doing anything?
d has changed!
12/31/2007
>>> d.tomorrow()
>>> d
01/01/2008
Some methods return a value; others
(like this one) change the object
that call it!
Date ids
What date is on your id?
What id is on your Date?
>>> d = Date(11,28,2007)
>>> d
11/28/2007
>>> d2 = Date(11,29,2007)
>>> d2
11/29/2007
>>> d == d2
>>> d2.yesterday()
>>> d == d2
>>> d.equals( d2 )
Will these be
true or false?
Excuse me -ids please!
Double Date
>>> d2 = d
>>> d
11/26/2007
>>> d.yesterday()
>>>
>>> d2
>>> d2 == d
>>> d2.equals(d)
What
happens
here?
Using copy
>>> d2 = d.copy()
But where
does copy
come from?
>>> d
11/25/2007
>>> d2
11/25/2007
>>> d.yesterday()
>>> d2
>>> d2 == d
What
happens
here?
class Date:
def __init__( self, mo, dy, yr ):
def __repr__(self):
def isLeapYear(self):
def copy(self):
""" returns a DIFFERENT object w/same date! """
def equals(self, d2):
""" returns True if they represent
the same date; False otherwise
"""
Where are these
TWO inputs
coming from?
class Date:
"Quiz"
def isBefore(self, d2):
""" True if self is before d2 """
if self.yr < d2.yr: return True
if self.mo < d2.mo: return True
if self.dy < d2.dy: return True
return False
Why is this method WRONG for the dates
1/1/2008
11/27/2007
how might you fix this problem?
def tomorrow(self):
""" moves the date that calls it ahead 1 day """
DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31]
This tomorrow method
should not return anything.
It just CHANGES the date
object that calls it.
DIM might be helpful…
Diamonds!
>>> printStripedDiamond( 8, 'A', 'B' )
A
A B
A B A
A B A B
A B A B A
A B A B A B
A B A B A B A
A B A B A B A B
Result
def printStripedDiamond( width, sym1, sym2):
for row in range (width,-1,-1):
nextSym = sym1
for col in range(width):
if row <= col:
print nextSym,
if nextSym == sym1:
nextSym = sym2
else: nextSym = sym1
else:
print '',
Code
print
Tomorrow's HW
Problem #1: Wandering
Starbucks
...
CGU
(W)
0
...
S
22 23 24 25 26 27 28
home
(E)
50
An overworked CGU student (S) leaves Starbucks after
their “late-night” breakfast and, each moment, randomly
stumbles toward campus (W) or toward home (E)
Once the student arrives at home or the classroom, the trip is complete.
The program should then print the total number of steps taken.
Write a program to model and analyze! this scenario...
rs()
take a random
step of +1 or -1
rwPos(s, nsteps)
take nsteps random
steps starting at s
rwSteps(s, low, hi)
take random steps starting at s until
you reach either low or hi
class Date:
What's wrong?
def isBefore(self, d2):
""" True if self is before d2 """
if self.yr < d2.yr: return True
if self.mo < d2.mo: return True
if self.dy < d2.dy: return True
return False
Why is this method WRONG for the dates
1/1/2008
11/27/2007
class Date:
def tomorrow(self):
""" moves the date that calls it ahead 1 day """
DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31]
Lab today / yesterday
Add these methods to Date
copy(self)
equals(self, d2)
yesterday(self)
tomorrow(self)
addNDays(self, N)
subNDays(self, N)
isBefore(self, d2)
isAfter(self, d2)
diff(self, d2)
dow(self)
and use your Date class to
analyze our calendar a bit…
Prof. Art Benjamin
no computer required…
Unusual calendar years…
Lab … and hw questions
© Copyright 2026 Paperzz