Pickle to source code

G

Gabriel Genellina

Hello

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?

Thanks,
Gabriel Genellina
Softlab SRL
 
B

Benjamin Niemann

Gabriel said:
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?

If all objects correctly implement the __repr__ method (true for built-in
stuff like list, dict, set...):
Just unpickle it and call repr() on the resulting object.
 
M

Maksim Kasimov

As far i know, in pickle-file there are only attributes values of a pickled object, but not an object itself.

It is possible to unpickle object only if you have the sourse of the class that object you have pickled.
So, if you have class code and attribute values of the class instance, there is no problem to produce a textual representation of the object. Isn't it?

(sorry for my English)
 
G

Gabriel Genellina

Benjamin Niemann ha escrito:
If all objects correctly implement the __repr__ method (true for built-in
stuff like list, dict, set...):
Just unpickle it and call repr() on the resulting object.

Unfortunately I need a more general approach...

Gabriel Genellina
Softlab SRL
 
G

Gabriel Genellina

Maksim Kasimov ha escrito:
As far i know, in pickle-file there are only attributes values of a pickled object, but not an object itself.

It is possible to unpickle object only if you have the sourse of the class that object you have pickled.
So, if you have class code and attribute values of the class instance, there is no problem to produce a textual representation of the object. Isn't it?

Yes, but I need it for many different objects, some of them written by
other people. Please see my next post for clarification.

Gabriel Genellina
Softlab SRL
 
G

Gabriel Genellina

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
 
O

Olivier Dormond

Gabriel said:
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.:

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

Cheers,

Olivier
 
G

Gabriel Genellina

Jean-Paul Calderone ha escrito:
You may find twisted.persisted.aot of some use. Here is an example:

AOT is unmaintained in Twisted, and may not support some newer features of Python (eg, datetime or deque instances). If this seems useful, you may want to contribute patches to bring it up to the full level of functionality you need.

Oh, thanks. I have some problems installing the package but I hope it
will be useful. If any changes are needed I'll report them.

Gabriel Genellina
Softlab SRL
 
G

Gabriel Genellina

Olivier Dormond ha escrito:
Doesn't pickle.loads just do what you need ? e.g.:

(1, 2, 1)

Er... Touché :)

- What year did World War II finish?
- Same year the Potsdam Conference was held.
- When was that?
- The year World War II finished.

I should have stated that I need an *explicit* string...

Gabriel Genellina
Softlab SRL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top