Pickle to Source - Gabriel Genellina?

$P!D3R DelSol sunsp1der at yahoo.com
Fri Apr 4 21:47:47 EDT 2008


Hello
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?
Thanks
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):
        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 
///\*_*/\\\ 

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.  
http://tc.deals.yahoo.com/tc/blockbuster/text5.com
-------------- 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