473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Pickle to source code

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
8 2829
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


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
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

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
> 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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Antoon Pardon | last post by:
I'm writing a little game, a gridler application, where you can turn pixmaps into puzzle's and try to solve them. I already have the data structure for such a puzzle worked out, one of the...
3
by: Michael Hohn | last post by:
Hi, under python 2.2, the pickle/unpickle sequence incorrectly restores a larger data structure I have. Under Python 2.3, these structures now give an explicit exception from...
3
by: Guy Lateur | last post by:
Hi all, I've been writing an application containing a lot of settings which can be changed by the user. I'm using wx.Config to read/write these settings (to the windows registry). This means I...
4
by: Shi Mu | last post by:
I got a sample code and tested it but really can not understand the use of pickle and dump: >>> import pickle >>> f = open("try.txt", "w") >>> pickle.dump(3.14, f) >>> pickle.dump(, f) >>>...
0
by: greg.landrum | last post by:
Hi, I have a fairly sizable body of python code that I'm in the process of modernizing. One of the modernization steps is converting to use new-style classes, and this is leading to problems...
16
by: John Salerno | last post by:
Here's what I have: import pickle data = open(r'C:\pickle_data.txt', 'w') image = open(r'C:\peakhell.jpg') pickle.Pickler(data) data.dump(image) data.close() image.close()
10
by: crystalattice | last post by:
I'm creating an RPG for experience and practice. I've finished a character creation module and I'm trying to figure out how to get the file I/O to work. I've read through the python newsgroup...
5
by: Chris | last post by:
Why can pickle serialize references to functions, but not methods? Pickling a function serializes the function name, but pickling a staticmethod, classmethod, or instancemethod generates an...
10
by: est | last post by:
>>import md5 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python25\lib\pickle.py", line 1366, in dumps Pickler(file, protocol).dump(obj) File...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.