saving and reloading variables for interactive work

Darren Dale dd55 at cornell.edu
Wed Jul 28 14:48:32 EDT 2004


> 
> Unfortunately, there is no quick & easy way to do this in Python, since 
> there is no easy way to differentiate unpicklable things (such as modules 
> and functions) from picklable ones (your variables), nor to make Pickle 
> gracefully ignore such objects.  If you can get all your variable names 
> into a list, however, you could use this code:
> 

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})

pickle.dump(myvars,open('myvars','wb'),-1)


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.

Further comments would be greatly appreciated.



More information about the Python-list mailing list