[Tutor] please critique my OOP

Timothy Wilson wilson@visi.com
Sun, 1 Jul 2001 23:20:24 -0500 (CDT)


Hey everyone,

I've been doing some reading on OOA, OOD, and OOP lately and I've gone back
to the random student group assignment program that I wrote a while back. I
reworked it a bit and I think it's quite a bit clearer. I'd appreciate it if
some of the more experienced persons on this list would give it a look and
offer critiques. I'm especially interested in OOP issues here.

My previous effort had all of the group assignment logic in one of the
Teacher class's methods. I've removed that in this version and it's quite a
bit simpler as a result. Have I sinned? :-)

Once I feel like this is a reasonably good example of OOP I promise to write
up a How-To describing the thought processes that led me do it this way. I
know this is a very simple program, but as an OOP beginner, I haven't found
a lot of examples of simple OO programs that describe how to get started.

Here's the code and a sample run:

--snip--
#!/usr/bin/env python

################################################################
#                                                              #
# mkgrp.py by Tim Wilson <wilson@visi.com>                     #
#   Assign a class of students randomly to a series of groups. #
#
#                                                              #
# July, 2001                                                   #
#                                                              #
################################################################

import string, random

class Teacher:
    def __init__(self, name):
        self.name = name
        self.roster = []
        self.unassigned = []
        self.groupList = []
        
    def __str__(self):
        return "%s has %s students." % (self.name, len(self.roster))
        
    def addStudent(self, ln, fn, gender):
        newStudent = Student(ln, fn, gender)
        self.roster.append(newStudent)
        self.unassigned.append(newStudent)
        
    def loadFromFile(self, filename):
        """
        File format has one student per line in the form:
            
        lastname, firstname, gender
        
        For example:
            
        van Rossum,Guido,M
        """
        
        file = open(filename, 'r')
        studentList = file.readlines()
        for i in studentList:
            studentRecord = string.split(i, ',')
            self.addStudent(studentRecord[0], \
            studentRecord[1], studentRecord[2][:-1]) #remove '\012'

    def printStudentRoster(self):
        for student in self.roster:
            print student
               
    def printGroupList(self):
        for group in self.groupList:
            print group.name
        

class Student:
    def __init__(self, ln, fn, gender):
        self.ln = ln
        self.fn = fn
        self.gender = gender
        
    def __str__(self):
        return "%s %s" % (self.fn, self.ln)


class Group:
    def __init__(self, name):
        self.name = name
        self.groupMembers = []
        
    def printGroup(self):
        for student in self.groupMembers:
            print student
            
    def addStudent(self, student):
        self.groupMembers.append(student)
        
if __name__ == '__main__':
    teacherName = raw_input('Your name? ')
    teacher = Teacher(teacherName)
    classList = raw_input('Class file? ')
    teacher.loadFromFile(classList)
    print teacher
    numGroups = raw_input('How many groups? ')
    for i in range(int(numGroups)):
        group = Group(i+1)
        teacher.groupList.append(group)
    while len(teacher.unassigned) != 0:
        for group in teacher.groupList:
            if len(teacher.unassigned) == 0:
                break
            else:
                selectedStudent = random.choice(teacher.unassigned)
                group.addStudent(selectedStudent)
                teacher.unassigned.remove(selectedStudent)
    print
    for group in teacher.groupList:
        print "Group %s" % group.name
        print "======" + len(str(group.name))*'='
        group.printGroup()
        print
--snip--

[wilson@einstein python]$ python mkgrp.py
Your name? Wilson
Class file? class.dat
Wilson has 17 students.
How many groups? 4

Group 1
=======
Marie Curie
Tim Wilson
Jenny Lange
James Watt
Ryan Carlson
 
Group 2
=======
Ryan Carlson
Craig Skalicky
Richard Feynman
John Kelly
 
Group 3
=======
Guido van Rossum
Lise Meitner
Albert Einstein
Mark Dockin
 
Group 4
=======
Greg Schmidt
Pat Killian
Joe Blow
Lawrence Cannon

--
Tim Wilson      |   Visit Sibley online:   | Check out:
Henry Sibley HS |  http://www.isd197.org   | http://www.zope.org
W. St. Paul, MN |                          | http://slashdot.org
wilson@visi.com |  <dtml-var pithy_quote>  | http://linux.com