xml marshal of general (but non Python standard) class

"Martin v. Löwis" martin at v.loewis.de
Tue Mar 29 13:50:23 EST 2005


syd wrote:
> But for my identifiedPeaks class (for instance), it has trouble.  This
> class contains a list of "peak" classes IdentifiedPeaks.Peak...

What precisely is the name of the class. You say it is
IdentifiedPeaks.Peak, but...

>>>>from IdentifiedPeaks import IdentifiedPeaks

Here you import IdentifiedPeaks.IdentifiedPeaks, not
IdentifiedPeak.Peak.

> pickle.PicklingError: Can't pickle <class 'IdentifiedPeaks.Peak'>: it's
> not found as IdentifiedPeaks.Peak

Here it claims there is no class IdentifiedPeaks.Peak, and I tend to
believe it. Could it be that this class does not exist under this name?

Python needs to pickle the full class name so that unpickle can find
the class. It uses (klass.__module__).(klass.__name__); if the class
is nested in another class, pickle cannot find out. So I suggest
to move the Peak class toplevel into the module. My guess is
that it is nested inside IdentifiedPeaks. The simplest fix might be
be to put

Peak=IdentifiedPeaks.Peak

into IdentifiedPeaks.py; better would be to move the class.

> AttributeError: Marshaller instance has no attribute
> 'm_IdentifiedPeaks'

That would happen if IdentifiedPeaks is a new-style class (i.e.
inheriting from object). marshal has only generic support for instance
objects; each additional type needs separate support. You can provide
that support by inheriting from Marshaller, adding m_ functions for all
missing types. Each function needs to return a list of XML substrings,
e.g. through

   def m_IdentifiedPeaks(self, peaks, dict):
       L = [ '<IdentifiedPeaks>' ]
       for p in peaks.getPeaks():
           L += self._marshal(p)
       L += [ '</IdentifiedPeaks>' ]
       return L

The dict parameter keeps the object references for cycle and
shared reference detection. Whether or not you need cycle support
depends on your application.

Alternatively, patches to support new-style classes in a more
general way are welcome.

Regards,
Martin



More information about the Python-list mailing list