[Tutor] my first object model, using an interface

Monika Jisswel monjissvel at googlemail.com
Sun Jul 6 13:39:24 CEST 2008

This looks like a database driven application.
When you send a package you must save the event on a database.
When a HomeWork is submited it must be entered in the database.
When you need to see what's the situation of any one student or all students
you just query teh database.
so you would be better off designing a database (do you like mysql ?) & a
python front end to it (web ?).
this database would be something like : students_table, courses_table,
teachers_table, homework_table
homework table would be like : idx, student_id, homework_id, statut where
statut is sent, finished, submited ...

the front end python program would be composed of a class  to load data into
database, and another to query it.

import MySQLdb

class NewEvent(object):

    ___init__(self, s = 'dbserv', u = 'username', p = 'password', dbn =
'database_name' ):
         self.s = s
         self.u = u
         self.p = p
         self.dbn = dbn
         self.con = MySQLdb.connect(host = self.s,  user  =  self.u ,
passwd = self.p, db = self.dbn)
         self.cur = self.con.cursor()

  def NewStudent(self, Name, Age, Sex, Address):
         cmd_NewStud = 'insert into student_table (Name, Age, Sex, Address)
values (%s, %d, %s, %s)' % (Name, Age, Sex, Address)

 def NewHW(self, student_id, HW_id):
         cmd_NewHW = 'insert into homework_table (student_id, homework_id)
values (%s, %s)' % (student_id, HW_id)

I hope this helps

About the code below :

2008/7/6 <tpc247 at gmail.com>:

> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080706/5fd6f82c/attachment.htm>

More information about the Tutor mailing list