Mailing List Archive

A question of style
I'm using BaseHTTPServer. I need to maintain a long-lived database
connection (using oracledb), and the only persistant object is my server,
so the db connection is stored there.

I've got a class for each type of page that gets displayed, with widgets
on each page that need to access the database. Thus, I end up with stuff
like:

self.page.handler.server.db.cursor()

inside my widget class methods. This works, but it's kind of messy. I'm
thinking of giving my base widget class a cursor method, which looks like:

def cursor(self):
return self.page.handler.server.db.cursor()

and then I can just do self.cursor() inside my widgets.

I can't think of any particularly compelling technical reasons why that's
a good or bad thing. Seems more like a matter of style than anything
else. Comments?

--
Roy Smith <roy@popmail.med.nyu.edu>
New York University School of Medicine
A question of style [ In reply to ]
Roy Smith wrote:

> on each page that need to access the database. Thus, I end up
> with stuff like:
>
> self.page.handler.server.db.cursor()
>
> inside my widget class methods. This works, but it's kind of
> messy. I'm thinking of giving my base widget class a cursor
> method, which looks like:
>
> def cursor(self):
> return self.page.handler.server.db.cursor()

Either that, or you could just make an alias like
dbconn = self.page.handler.server.db
and use it like this
dbconn.cursor()

I personally like the cursor, but I thought I'd mention
the aliasing technique in case you had forgotten about it.
A question of style [ In reply to ]
Roy Smith wrote:
> I'm thinking of giving my base widget class a cursor method, which looks like:
>
>def cursor(self):
> return self.page.handler.server.db.cursor()
>
>and then I can just do self.cursor() inside my widgets.

If you make alias like
cursor = self.page.handler.server.db.cursor
then you can simply call cursor() function - it is much faster than your
version.

David