Pickle to Source - Gabriel Genellina?

$P!D3R DelSol sunsp1der at yahoo.com
Sat Apr 5 03:47:47 CEST 2008

I found this 3 year old message asking about doing the same exact thing I am trying to do. This is one wheel I REALLY don't want to re-invent.  Can anyone point me to code that does this?
Ivan ( sunsp1der at yahoo dot com)

> 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):
    def foo(self):
# construct an instance and work with it
obj = MyClass(1,2)
# save into file

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.

Gabriel Genellina
Softlab SRL 

NOTICE: Empowered by Presidential Executive Orders, the National Security Agency may read this email without warning, warrant, or notice. The NSA may do this without any judicial or legislative oversight. The President also claims the right to designate the sender or the reader as an enemy combatant and to imprison him/her indefinitely without access to legal counsel or anyone else, and to be "rendered" to a foreign government for possible torture or death.

You rock. That's why Blockbuster's offering you one month of Blockbuster Total Access, No Cost.  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080404/284e8238/attachment.html>

More information about the Python-list mailing list