[Tutor] my first object model, using an interface

tpc247 at gmail.com tpc247 at gmail.com
Sun Jul 6 06:03:58 CEST 2008


Dear fellow Python enthusiasts:

I want to run an idea by you to see if I understand modeling objects
adequately, after reading Alan Gauld's excellent tutorial and two brief
articles about interfaces in Python, here:

                 http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm

http://dirtsimple.org/2004/12/python-interfaces-are-not-java.html
                 http://nedbatchelder.com/text/pythonic-interfaces.html

I am attempting to model the following:
a correspondence school has asked me to help them solve a problem.  When the
school sends a student by mail a package containing several courses, each
course having several pieces of gradable homework, when a specific threshold
of homework completed and submitted by the student is met or exceeded,
another package is sent to the student by mail.  Now, this aforementioned
threshold, i.e., an integer indicating percentage, can vary, and is not just
for the totality of homework in the package, but also for certain courses
with many pieces of homework.  For example, say the school sends student Joe
a package (package_1) containing courses A, B and C_sub1.  A, B & C_sub1
have 10 pieces of gradable homework, and the school wants that we can set a
threshold for the totality of homework for package1, as well as a threshold
for C_sub1 alone.  When the thresholds are met or exceeded, independently,
then we send package_2 and C_sub2, respectively.  I envisioned a nascent
object model and noted the following observations:

- a Watchable interface that, when implemented, signifies that the
implementing object has a threshold and associated package.
- a Package class, that can be seen as a container for courses and/or one
part of a course
- a Course class, that can be seen as a container for gradable homework
- a Homework class, that has a flag to indicated whether it has been
received by the school
- most Packages are Watchable (except the last Package object), and only one
or two Courses in a Package need to be Watchable

Two questions:
1) Should I create a first-class Watchable interface object, and then have
my Package and Course objects implement it if they need to ?  If I start
with a Watchable interface, do I handle the name-space conflict, i.e.,
Package(object) vs Package(Watchable), by defining a Package class, and a
W_Package class that implements Watchable, and likewise for Course ?
2) am I even thinking the right way about attacking this problem ?  I am
curious what your experience in writing easy to maintain software might tell
you about my nascent object model.

class Watchable(object):
    def set_threshold(self, an_int):
        raise NotImplemented
    def get_threshold(self):
        raise NotImplemented
    def set_associated_load(self, a_load):
        raise NotImplemented
    def get_associated_load(self):
        raise NotImplemented

class Package(object):
    def __init__(self, courses):
        self.set_courses(courses)
    def set_courses(self, courses):
        self.courses = courses
    def get_courses(self):
        return self.courses

class Course(Watchable):
    def __init__(self, name, homework):
        self.name = name
        self.homework = homework
    def get_name(self):
        return self.name
    def get_homework(self):
        return self.homework

class Homework(object):
    def __init__(self, name):
        self.name = name
        self.academically_received = False
    def set_academically_received(self):
        self.academically_received = True
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080705/9f585546/attachment.htm>


More information about the Tutor mailing list