Re: [SQLObject] Using SQLObjects as Abstract Classes
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Ian B. <ia...@co...> - 2003-07-25 16:08:07
|
On Fri, 2003-07-25 at 08:22, Brad Bollenbach wrote: > On Thu, Jul 24, 2003 at 08:57:19PM -0500, Ian Bicking wrote: > > On Thu, 2003-07-24 at 14:42, Brad Bollenbach wrote: > > > Hi all, > > > > > > How can I use an SQLObject-derived class as an abstract base class? > > > > Hmm... I'm not exactly clear what an abstract base class is. Is there a > > particular problem you are trying to solve? > > An abstract base class is a class that's not intended to be directly > instantiated, instead defining methods that are intended to be > overridden in derived classes. Oh, right right, I don't know why the terminology was confusing me. You'll have to do something like: class MonitorBase: ... class Monitor(SQLObject, MonitorBase): ... > The problem I'm trying to solve is a web monitoring system: > > MonitorBase > | | | > | | -> StatusMonitor > | --> KeywordMonitor > ----> MD5ChecksumMonitor > > So, I want to be able to do something like (untested code): > > class MonitorBase(SQLObject): > """I'm the base class from which all monitors shall > be derived. > """ > > created_date = DateTimeCol(notNone = True) > last_run_date = DateTimeCol(notNone = True) > active = IntCol() > ... > > def doMonitorCheck(self): > """Run the check for this monitor.""" > raise NotImplementedError("doMonitorCheck is an abstract method.") > > > class StatusMonitor(MonitorBase): > """I monitor websites for the 200 OK status.""" > > def doMonitorCheck(self): > ...logic to check for 200 OK... > > Except this breaks, because any property access on StatusMonitor tries > to look for a table called status_monitor. Unless I specify _table = > 'monitor' in every derived class, which seems a bit clunky. If you have that class layout, you are supposing that there will be other subclasses of Monitor. However, each of these subclasses must be a separate table, or great confusion will ensue. Python inheritance simply doesn't map to RDBMS tables. There are things that can be called inheritance among RDBMS tables, but they aren't the same thing. So SQLObject just takes a one-class-one-table approach. You can still use inheritance, and a class can be abstract so long as you don't use it, but it's still one-class-one-table. It's improper to use _table = 'monitor' in each class, because that breaks that rule. If you have one table you shouldn't have more than one class that you use with that table. Ian |