By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

Pickle to source code

P: n/a
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

Oct 26 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Gabriel Genellina wrote:
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.
--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Oct 26 '05 #2

P: n/a


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)

Gabriel Genellina wrote:
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

--
Best regards,
Maksim Kasimov
mailto: ma************@gmail.com
Oct 26 '05 #3

P: n/a
Benjamin Niemann ha escrito:
Gabriel Genellina wrote:
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


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

Oct 26 '05 #4

P: n/a

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

Oct 26 '05 #5

P: n/a
> 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

Oct 26 '05 #6

P: n/a
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
Oct 26 '05 #7

P: n/a

Jean-Paul Calderone ha escrito:
In other words, I need a *string* which, being sent to eval(), would
return the original object state saved in the pickle.


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

Oct 26 '05 #8

P: n/a
Olivier Dormond ha escrito:
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.


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)


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

Oct 26 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.