On Tue, Apr 9, 2013 at 9:08 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve+comp.lang.python@pearwood.info" target="_blank">steve+comp.lang.python@pearwood.info</a>></span> wrote:<br><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Tue, 09 Apr 2013 07:50:11 +0200, Morten Guldager wrote:<br>
<br>> I'm about to write an API against a huge propitiatory Oracle based<br>
> network inventory database. The database have many different concepts<br>
> stored in it's tables, can one concept can span over multiple tables.<br>
><br>
> I would like to write a class for accessing each concept, but only have<br>
> a single database connection throughout the whole program.<br>
<br>
</div>Sounds reasonable.<br>
<div class="im"><br>
> I imagine some code along these lines, but cant figure out how to<br>
> declare the classes that will make it work:<br>
><br>
> # create a connection to the database and perform come basic login and<br>
> initialization<br>
> nib = NwInvDb("scott/tiger@ora")<br>
> # find a device by ip<br>
> interesting_device = nib.Device.lookup_by_ip("192.168.1.1")<br>
<br>
</div>What's "nib" mean? And "NwInvDb"? I can imagine that the "Db" at the end<br>
stands for Database, but the rest is just word-salad. I can guess that<br>
NwInvDb is some sort of database connection. Am I close?<br></blockquote><div><br></div><div>NwInvDb = NetworkInventoryDatabase, yes you are correct, it creates the database handle and makes it ready for use. </div><div>
<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
> In this example I access the concept Device.<br>
><br>
> Should I make the Device class inherit from NwInvDb? Or should I keep<br>
> them separate?<br>
<br>
</div>Why are you asking us? We don't know what functionality you expect NwInvDb<br>
and Device to have, what they represent, or whether a Device can be<br>
meaningfully considered an instance of a NwInvDb, whatever that is.<br>
<br>
But given my *guess* that NwInvDb represents a database connection, and<br>
that Device represents data fetched from that database, then no of course<br>
you should not inherit. Inheritance implies an "is-a" relationship. If<br>
you inherit from NwInvDb for Device, that implies:<br>
<br>
- interesting_device Is-A database;<br></blockquote><div><br></div><div>which it is not. </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
- anywhere you can use a NwInvDb database object, you can use<br>
  a Device object.<br></blockquote><div><br></div><div>which you can not.  </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
And the same would apply to every other concept in the database.<br>
<br>
That does not sound like a clean and useful design to me.<br></blockquote><div><br></div><div>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.</div>
<div><br></div><div>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!</div>
<div><br></div><div>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:</div>
<div><br></div><div>nib = NwInvDb("scott/tiger@ora")</div><div>dev = NwInvDb.Device(nib)</div><div>interesting_device = dev.lookup_by_ip("192.168.1.1")</div><div><br></div><div><br></div></div>-- <br>/Morten %-)