Pickle to source code

Gabriel Genellina gagenellina at softlab.com.ar
Wed Oct 26 09:15:35 EDT 2005


> I want to convert from pickle format to python source code. That is,
> given an existing pickle, I want to produce a textual representation
> which, when evaluated, yields the original object (as if I had
> unpickled the pickle).
> I know of some transformations pickle/xml (Zope comes with one such
> tool, gnosis xml is another) so I believe I could build something based
> on them.
> But I dont want to reinvent the wheel, I wonder if anyone knows of a
> library which could do what I want?

An example to make things clear:

class MyClass:
    def __init__(self,a,b):
        self.a=a
        self.b=b
    def foo(self):
        self.done=1
# construct an instance and work with it
obj = MyClass(1,2)
obj.foo()
# save into file
pickle.dump(obj,file('test.dat','wb'))

Then, later, another day, using another process, I read the file and
want to print a block of python code equivalent to the pickle saved in
the file.
That is, I want to *generate* a block of code like this:

xxx = new.instance(MyClass)
xxx.a = 1
xxx.b = 2
xxx.done = 1

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.

The real case is a bit more complicated because there may be references
to other objects, involving the persistent_id mechanism of pickles, but
I think it should not be too difficult. In this example, if xxx.z
points to another external instance for which persistent_id returns
'1234', would suffice to output another line like:
xxx.z = external_reference('1234')

I hope its more clear now.

Thanks,
Gabriel Genellina
Softlab SRL




More information about the Python-list mailing list