Could you determine the picklable types using something like this? Its
kinda ugly, but maybe something related would work:
myvars = {}
for key in vars().keys():
myvar = vars()[key]
if str(type(myvar)) == "<type 'module'>": pass
elif: str(type(myvar)) == "<type 'function'>": pass
elif: str(type(myvar)) == "<type 'unpicklable'>": pass
else: myvars.update({key:myvar})
You can do this a bit more directly using the types module to check the
types (with a little bit of code restructuring):
from types import *
myvars = {}
for key,myvar in vars().items():
if not isinstance(myvar,(ModuleType,FunctionType)):
myvars[key]=myvar
A list comprehension would get you a nice, unreadable one-liner if it's so
desired:
from types import *
myvars = dict([(key,myvar) for key,myvar in vars().items() \
if not isinstance(myvar,(ModuleType,FunctionType))])
The problem with these types is there's no real guarantee what can be
pickled and what can't, and of those things that can be pickled, what
makes sense (functions can be pickled, but their code isn't stored).
There's currently no ispicklable() function
Maybe a try/except would be appropriate, but the pickle docs ay that an
unspecified number of bytes may have been written to the file before the
PicklingError exception is raised.
You could use dumps() instead of dump() for testing picklability before
picklation, though it smells of hackishness:
def ispicklable(item):
try:
pickle.dumps(item)
except PicklingError:
return False
return True
Then you can just use ispicklable() in place of the 'not isinstance()'
mess I wrote.
I've done some poking though, and there a better way than that. It seems
all picklable objects define a __reduce__ method, so you could generalize
this check like this:
def ispicklable(item):
try:
item.__reduce__()
except KeyError,TypeError:
return False
return True
Dunno if any of this helps; sorry if parts make no sense, I've retyped
parts of this a couple times