On 5/3/06, Kirill Simonov <xi@...> wrote:
> Hi Chris,
>
> On Tue, May 02, 2006 at 04:52:28PM -0400, Chris S wrote:
> > I have a few questions about how PySyck handles objects/instances. I
> > subclassed Dumper and overwrote represent_dict() to ignore keys
> > prefixed with '_v_', the aim being to temporarily store unserializable
> > things like locks, threads, sockets, etc in large nested structures
> > that I want to serialize.
>
> The easiest way to make PySyck ignore some attributes is to define
> the methods __getstate__ and __setstate__ in your classes. These
> methods are parts of the pickle protocol. __getstate__ should return a
> dictionary with keys and values to be serialized. In your case, it will
> look like this (note: untested code):
>
> def __getstate__(self):
> state =3D {}
> for key in self.__dict__:
> if not key.startswith('_v_'):
> state[key] =3D self.__dict__[key]
> return state
Yes, I do do this in most cases. However, like I mentioned, I'm
serializing large nested structures (dicts containing lists containing
instances containing dicts, etc), so it's impractical to define a
custom __getstate__ for every class I might use. Since they would all
use the same code anyways, it's far easier to implement this directly
in the Dumper. And so far it works pretty reliably, except with a
classes __dict__, which isn't wrapped by represent_dict for some
reason. I could submit a small patch for this if you'd like.
Regards,
Chris Spencer
|