classes and sub classes?

Morten Guldager morten.guldager at gmail.com
Tue Apr 9 03:38:21 EDT 2013


On Tue, Apr 9, 2013 at 9:08 AM, Steven D'Aprano <
steve+comp.lang.python at pearwood.info> wrote:

> On Tue, 09 Apr 2013 07:50:11 +0200, Morten Guldager wrote:
>
> > I'm about to write an API against a huge propitiatory Oracle based
> > network inventory database. The database have many different concepts
> > stored in it's tables, can one concept can span over multiple tables.
> >
> > I would like to write a class for accessing each concept, but only have
> > a single database connection throughout the whole program.
>
> Sounds reasonable.
>
> > I imagine some code along these lines, but cant figure out how to
> > declare the classes that will make it work:
> >
> > # create a connection to the database and perform come basic login and
> > initialization
> > nib = NwInvDb("scott/tiger at ora")
> > # find a device by ip
> > interesting_device = nib.Device.lookup_by_ip("192.168.1.1")
>
> What's "nib" mean? And "NwInvDb"? I can imagine that the "Db" at the end
> stands for Database, but the rest is just word-salad. I can guess that
> NwInvDb is some sort of database connection. Am I close?
>

NwInvDb = NetworkInventoryDatabase, yes you are correct, it creates the
database handle and makes it ready for use.

> In this example I access the concept Device.
> >
> > Should I make the Device class inherit from NwInvDb? Or should I keep
> > them separate?
>
> Why are you asking us? We don't know what functionality you expect NwInvDb
> and Device to have, what they represent, or whether a Device can be
> meaningfully considered an instance of a NwInvDb, whatever that is.
>
> But given my *guess* that NwInvDb represents a database connection, and
> that Device represents data fetched from that database, then no of course
> you should not inherit. Inheritance implies an "is-a" relationship. If
> you inherit from NwInvDb for Device, that implies:
>
> - interesting_device Is-A database;
>

which it is not.

- anywhere you can use a NwInvDb database object, you can use
>   a Device object.
>

which you can not.

And the same would apply to every other concept in the database.
>
> That does not sound like a clean and useful design to me.
>

Good. I think I agree with you so far. Maybe that's why I'm asking, because
I'm not perfectly sure which path to follow to achieve  what I want.

Yes, I need some sort of database connection instance, if it wasn't because
I later on will be needing to access both test and production database I
_could_ have made it global, even if we easily could agree that globals
suck most of the time!

The concept classes like the Device one, will be using the database
instance, I just don't know how to pass db into the concept class. - should
I do it explicit making constructors accept an argument? Like:

nib = NwInvDb("scott/tiger at ora")
dev = NwInvDb.Device(nib)
interesting_device = dev.lookup_by_ip("192.168.1.1")


-- 
/Morten %-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130409/88f7165b/attachment.html>


More information about the Python-list mailing list