Pickle to source code

Olivier Dormond olivier.dormond at hispeed.ch
Wed Oct 26 10:53:59 EDT 2005


Gabriel Genellina wrote:
> Or perhaps:
> 
> xxx = new.instance(MyClass, {'a':1,'b':2,'done':1})
> 
> In other words, I need a *string* which, being sent to eval(), would
> return the original object state saved in the pickle.
> As has been pointed, repr() would do that for simple types. But I need
> a more general solution.

Doesn't pickle.loads just do what you need ? e.g.:

>>> pickled = file('test.dat', 'rb').read()
>>> obj = eval('pickle.loads(%r)'%pickled)
>>> obj
<__main__.MyClass instance at 0xb7bfb76c>
>>> obj.a, obj.b, obj.done
(1, 2, 1)
>>>

As I've never used the persistent_id mechanism of pickle I don't know
if that would do the trick.

Cheers,

	Olivier



More information about the Python-list mailing list