python class question

Andreas Kostyrka andreas at kostyrka.priv.at
Tue Jul 30 05:34:24 EDT 2002


Am Don, 2002-07-25 um 20.38 schrieb Chris Liechti:
> > class xrml(SimpleItem.SimpleItem):
> >      def init_db(self):
> >           try:
> >                dbc = getattr(self, connection)
> 
> why not:
>     	dbc = self.connection
Because this is eqivalent to 
dbc=getattr(self,"connection")

> > 
> > Now I tried to modify the last line as follows: 
> >           self.DB = dbc()
> > and getting a very strange error (Error Type: UnpickleableError
> > Error Value: Cannot pickle <type 'type'> objects).
> 
> don't know Zope but it look like that it uses pickle to store your instaces  
> and the DB object cannot be picked. thats probably because the connection 
> to a database cannot be stored, it must be terminated and rebuild, but 
> thats doesn't seem to be supported by the db object.
Well, Zope does use pickle to serialize the objects in ZODB.
You could try to use
self._v_DB=dbc()

Explanation:
attributes starting with _v_ are not stored. Basically, in every of your
methods you would need a check like:
if not hasattr(self,"_v_DB"):
	self.reconnectDB()
before using self._v_DB.

This way your connection stays alive as long the object is active in
memory. And when it gets reloaded, the db connection gets restarted.

Andreas





More information about the Python-list mailing list