[Tutor] OOP design (w/ example :-)
Timothy Wilson
wilson@visi.com
Sat, 24 Mar 2001 12:16:11 -0600 (CST)
Hey everyone,
I'm back working on a little Python program, trying to scratch a specific
itch that I keep having with my students at school. Specifically, I often
want to put my students in small groups to work on class work, labs, or
whatever we're working on that day. It's too time consuming to go through a
big process every day and I want to rearrange the groups often. I thought a
small computer program would be just the ticket.
To keep things simple for now, I'm storing the students in a simple text
file with last name, first name, and gender. (At some point I'd like to have
the option of mixing gender in the groups.) Here's a sample file:
van Rossum,Guido,M
Boop,Betty,F
Gauld,Alan,M
Flintstone,Wilma,F
Rubble,Betty,F
Yoo,Danny,M
Wilson,Tim,M
Crawford,Cindy,F
Van Laningham,Ivan,M
Lutz,Mark,M
Ascher,David,M
Watters,Aaron,M
I'd like to try a OOP approach, but one of the things I struggle with is the
initial design stage. I've asked about this before and I've done some
reading, but I still haven't found a way of thinking about problems like
this that make a lot of sense to me.
My program, it would seem, would logically have classes the correspond to
individual students, groups, and perhaps the class as a whole. Here's what
I've come up with so far:
class Student:
def __init__(self, ln, fn, gender):
self.ln = ln
self.fn = fn
self.gender = gender
class Group:
def __init__(self, studentList):
self.studentList = studentList
def display(studentList):
pass
class Roster:
def __init__(self):
pass
def loadFile(self, filename):
import string
classList = []
f = open(filename, 'r')
while 1:
try:
record = f.readline()
student = string.split(record, ',')
ln = student[0]
fn = student[1]
gender = student[2][:1]
classList.append(Student(ln, fn, gender))
except EOFError:
break
return classList
Note, this code doesn't currently work. I get this:
Python 1.5.2 (#0, Apr 3 2000, 14:46:48) [GCC 2.95.2 20000313 (Debian
GNU/Linux)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import group
>>> roster = group.Roster()
>>> classList = roster.loadFile('class.dat')
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "group.py", line 29, in loadFile
fn = student[1]
IndexError: list index out of range
>>>
I'd love to hear some advice from others on the list. What design advice can
you give? Any other suggestions?
-Tim
--
Tim Wilson | Visit Sibley online: | Check out:
Henry Sibley HS | http://www.isd197.k12.mn.us/ | http://www.zope.org/
W. St. Paul, MN | | http://slashdot.org/
wilson@visi.com | <dtml-var pithy_quote> | http://linux.com/