[Grok-dev] Pickling an object stored in the Zodb. How do I skip the __parent__ attribute?

Sebastian Ware sebastian at urbantalk.se
Mon Aug 18 08:57:24 EDT 2008


After many days of experimenting and trying to understand docs here  
and there, I ended up adding the following code to the root object  
that I wanted to export:

       def __getstate__(self):
         state = dict(self.__dict__)
         state['__parent__'] = None
         return state

This seems to work, but I am worried about possible side effects since  
it is used for the Zodb persistence.

Other things I tried and failed at was:

-Using an adapter to "remove" the __parent__ attribute during  
pickling. It had the unfortunate side effect that the object which is  
recreated is of the wrong type and couldn't easily be retyped  
(complaining about wrong layout)

-I am having difficulties understanding how I would use zope.copy to  
perform an export

-Customising the pickler seems to be too tricky for me and possibly  
incur a performance penalty compared to cPickle

I was hoping I could use an adapter as a proxy that is pickled exactly  
in the same way as the adapted object but setting "__parent__" to None  
(thus preventing the entire object graph to be pickled). This didn't  
work out, but that would have been the nicest solution.

Mvh Sebastian

14 aug 2008 kl. 20.03 skrev Kevin Smith:

> Hi Sebastion,
>
> Please let me know if you make headway with this, I've tried a  
> number of things to make this work, without success.
>
> Thanks,
>
> Kevin Smith
>
> Sebastian Ware wrote:
>>
>> Thanks for the pointer! I have read:
>>
>>    http://www.python.org/dev/peps/pep-0307/
>>
>> And found that the pickling is done by calling the method you refered
>> to:
>>
>>    __getstate__()
>>
>> and that it really does exist on the objects in the Zodb. I will
>> override the method by means of an adapter for the root object that I
>> am pickling and remove the attribute [__parent__]. Feels as though
>> this might actually work :)
>>
>> Mvh Sebastian
>>
>> 14 aug 2008 kl. 15.11 skrev Jegenye 2001 Bt (Miklós Prisznyák):
>>
>>
>>> 2008/8/14 Sebastian Ware <sebastian at urbantalk.se>
>>> I am trying to export and import objects from the Zodb using
>>> cPickle.dumps().
>>>
>>> My problem is that the pickler traverses the __parent__ attribute  
>>> thus
>>> returning a much larger object graph than I wish to serialize.
>>>
>>> Do I have to subclass the pickler or is there som other smart way of
>>> making it skip the __parent__ attribute?
>>>
>>>
>>> You could  write an adapter  (or modify the class of the content
>>> objects itself) to use __setstate__ and __getstate__ , etc:
>>>
>>> http://www.python.org/doc/2.5/lib/node321.html
>>>
>>> -- 
>>> Best regards,
>>>    Miklós
>>>
>>> _______________________________________________
>>> Grok-dev mailing list
>>> Grok-dev at zope.org
>>> http://mail.zope.org/mailman/listinfo/grok-dev
>>>
>> _______________________________________________
>> Grok-dev mailing list
>> Grok-dev at zope.org
>> http://mail.zope.org/mailman/listinfo/grok-dev
>>
>>
>>
> <kevin.vcf>



More information about the Grok-dev mailing list