473,396 Members | 2,029 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,396 software developers and data experts.

Trying to understand pickle.loads withou class declaration

Hi,

Shuffling files around in my project, I notice I broke everything when I
stopped declaring classes in a program that "pickled.loaded" existing
pickled object of type "classes".

The error message being that the class(es) was unknown.

However, I _think_, I did manage to do the following in the past:

1) pickle.dump class "test" with field "A" intialized in declaration
2) pickle.load class "test" and add field "B" then pickle.dumps it
3) pickl.load class "test" with no problem although field "B" was not
initialized in that declaration.
I have two questions:

1) Why cannot the interpreter accept to load an object which class is not
defined ?
2) assuming the above (.... I _think_ ....) is true, why then is there not
problem in loading an object which changes dynamically.

Hope I'm making sense.

Any clue ?

Thanks,

Philippe

Jul 19 '05 #1
3 1725
I confirm that all I have to do in order to successfully load a pickled
object of class A is to declare

class A:
def __init__(self):
pass

Although the object has tons of fields
Quid ?

Regards,

Philippe

Philippe C. Martin wrote:
Hi,

Shuffling files around in my project, I notice I broke everything when I
stopped declaring classes in a program that "pickled.loaded" existing
pickled object of type "classes".

The error message being that the class(es) was unknown.

However, I _think_, I did manage to do the following in the past:

1) pickle.dump class "test" with field "A" intialized in declaration
2) pickle.load class "test" and add field "B" then pickle.dumps it
3) pickl.load class "test" with no problem although field "B" was not
initialized in that declaration.
I have two questions:

1) Why cannot the interpreter accept to load an object which class is not
defined ?
2) assuming the above (.... I _think_ ....) is true, why then is there not
problem in loading an object which changes dynamically.

Hope I'm making sense.

Any clue ?

Thanks,

Philippe


Jul 19 '05 #2
Even

class A:
pass

should do the trick. Only the instance attributes are saved by a
pickle,
not the methods or the class itself. The unpickler tries to merge the
saved data and the class/method info that is not saved to recreate
the fully functional instance... but of course this info should still
be available. The unpickler will even import the class from the
appropriate module if necessary.

If you want your unpickler to manufacture a class for you when
it is not available, you may use the gnosis xml pickler with the
appropriate security ("paranoia") level.

http://gnosis.cx/download/gnosis/xml/pickle/

Cheers,

Seb

Jul 19 '05 #3
Thanks,

Philippe
Sébastien Boisgérault wrote:
Even

class A:
pass

should do the trick. Only the instance attributes are saved by a
pickle,
not the methods or the class itself. The unpickler tries to merge the
saved data and the class/method info that is not saved to recreate
the fully functional instance... but of course this info should still
be available. The unpickler will even import the class from the
appropriate module if necessary.

If you want your unpickler to manufacture a class for you when
it is not available, you may use the gnosis xml pickler with the
appropriate security ("paranoia") level.

http://gnosis.cx/download/gnosis/xml/pickle/

Cheers,

Seb


Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ted kelly | last post by:
It seems I can not deepcopy or pickle an object with an attribute that points to any function that is not a built in. I thought it should be ok as long as the function is defined in the top level...
2
by: Jane Austine | last post by:
Hi. class A: def __init__(self,tick): if tick: self.foo=self.bar else: self.foo=self.bur def bar(self): print 'bar'
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...
28
by: Grant Edwards | last post by:
I finally figured out why one of my apps sometimes fails under Win32 when it always works fine under Linux: Under Win32, the pickle module only works with a subset of floating point values. In...
2
by: Steven Bethard | last post by:
I'd like to be able to pickle instancemethod objects mainly because I want to be able to delay a call like ``foo(spam, badger)`` by dumping ``foo``, ``spam`` and ``badger`` to disk and loading them...
2
by: raghu | last post by:
can any one explain about pickle i read in the book but they have not provided any example for that so please explain with a simple example
9
by: andrewfelch | last post by:
Hello all, I'm using the metaclass trick for automatic reloading of class member functions, found at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164 My problem is that if I 1)...
3
by: Jeffrey Barish | last post by:
I have a class derived from string that is used in a pickle. In the new version of my program, I moved the module containing the definition of the class. Now the unpickle fails because it doesn't...
4
by: PBlitz | last post by:
I want to save the content of a custom made OrderedDict. (I got the OrderedDict from http://aspn.activestate.com/ASPN/Python) The pickle modul seemed to be the solution for the job. At first I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.