how to go around non-picklable object types?
Aaron "Castironpi" Brady
castironpi at gmail.com
Mon Sep 15 14:59:54 EDT 2008
On Sep 15, 12:36 pm, "inhahe" <inh... at gmail.com> wrote:
> There are certain types of objects that can't be pickled, like file objects.
> My question is how could I pickle a hierarchy while automatically skipping
> all such objects instead of just aborting the pickle when it comes across
> one.
>
> The only thing I can think of is to make my own classes that wrap file
> objects and so forth and use __getstate__ to leave out the underlying file,
> etc. objects, or to set __getstate__ for every class that holds an offending
> object type and filter such objects out of dictionaries when I pickle those.
> And I don't even have a list of common object types that would make it
> break.
>
> Is there a better way? thx..
inhahe,
Nice to hear from you again. This is one possibility.
import pickle
class TolerantPickler( pickle.Pickler ):
def save(self, obj):
try:
pickle.Pickler.save( self, obj )
except TypeError:
pickle.Pickler.save( self, None )
Then every time the native Pickler object tries to call 'save' it gets
your protected call instead. If the call fails with TypeError, just
pickle None.
Here are 'dumps' and a test.
from StringIO import StringIO
def dumps(obj, protocol=None):
file = StringIO()
TolerantPickler(file, protocol).dump(obj)
return file.getvalue()
class A: pass
a= A()
a.a= open( 'temp.dat' )
a.b= 32
b= dumps( a )
c= pickle.loads( b )
print c
print c.a, c.b
/Output:
<__main__.A instance at 0x00A7D1E8>
None 32
It may accomplish some of what you want. If it works, a pat on the
back to the writer of the Pickler class for using encapsulation.
More information about the Python-list
mailing list