how to go around non-picklable object types?

I

inhahe

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..
 
A

Aaron \Castironpi\ Brady

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top