473,465 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pickle problem

Hello everybody:
I'd like to use the pickle module to save the state of an object so to
be able to restore it later. The problem is that it holds a list of
other objects, say numbers, and if I modify the list and restore the
object, the list itself is not reverted to the saved one, but stays
with one element deleted.
An example session is the following:

Data is A [1, 2, 3, 4]
saving a with pickle
Deleting an object: del a[3]
Now data is A [1, 2, 3]
Oops! That was an error: can you please recover to the last saved data?
A [1, 2, 3] #### I'd like to have here A[1,2,3,4]!!!!!!

Is it the intended behavior for pickle? if so, are there any way to
save the state of my object?

Code follows
-----------------------
class A(object):
objects = []
-----------------------
then I run the code:
---------------------------------------
import pickle
from core import A

a = A()

for i in [1,2,3,4]:
a.objects.append(i)

savedData = pickle.dumps(a)
print "Saved data is ",a
print "Deleting an object"
del a.objects[3]
print a
print "Oops! This was an error: can you please recover the last saved data?"

print pickle.loads(savedData)
--------------------------------------------

Thank you for any help!

Mario
Jun 27 '08 #1
2 1496
On Apr 18, 11:55*am, "Mario Ceresa" <cere...@gmail.comwrote:
Hello everybody:
I'd like to use the pickle module to save the state of an object so to
be able to restore it later. The problem is that it holds a list of
other objects, say numbers, and if I modify the list and restore the
object, the list itself is not reverted to the saved one, but stays
with one element deleted.
An example session is the following:

Data is *A [1, 2, 3, 4]
saving a with pickle
Deleting an object: del a[3]
Now data is A [1, 2, 3]
Oops! That was an error: can you please recover to the last saved data?
A [1, 2, 3] * * #### I'd like to have here A[1,2,3,4]!!!!!!

Is it the intended behavior for pickle? if so, are there any way to
save the state of my object?

Code follows
-----------------------
class A(object):
* * * * objects = []
-----------------------
then I run *the code:
---------------------------------------
import pickle
from core import A

a = A()

for i in [1,2,3,4]:
* * * * a.objects.append(i)

savedData = pickle.dumps(a)
print "Saved data is ",a
print "Deleting an object"
del a.objects[3]
print a
print "Oops! This was an error: can you please recover the last saved data?"

print pickle.loads(savedData)
--------------------------------------------

Thank you for any help!

Mario
The problem is that the way you define 'objects', it is an attribute
of the A *class*, not the instance you create. Change the A class to:

class A(object):
def __init__(self):
self.objects = []

and rerun it; it should now work as you intended.

HTH,
George
Jun 27 '08 #2
Dear Jerry and George:
it works like a charm! I always thought that the first way was a
quicker alternative to defining the init method... shame on me!
>From now on I'll read the list every day repeating to myself:
"Premature optimization is the root of all evil", "Premature
optimization is the root of all evil", .... :)

Thanks a lot,

Mario
On Fri, Apr 18, 2008 at 8:00 PM, George Sakkis <ge***********@gmail.comwrote:
On Apr 18, 11:55 am, "Mario Ceresa" <cere...@gmail.comwrote:
Hello everybody:
I'd like to use the pickle module to save the state of an object so to
be able to restore it later. The problem is that it holds a list of
other objects, say numbers, and if I modify the list and restore the
object, the list itself is not reverted to the saved one, but stays
with one element deleted.

An example session is the following:
>
Data is A [1, 2, 3, 4]
saving a with pickle
Deleting an object: del a[3]
Now data is A [1, 2, 3]
Oops! That was an error: can you please recover to the last saved data?
A [1, 2, 3] #### I'd like to have here A[1,2,3,4]!!!!!!
>
Is it the intended behavior for pickle? if so, are there any way to
save the state of my object?
>
Code follows
-----------------------
class A(object):
objects = []
-----------------------
then I run the code:
---------------------------------------
import pickle
from core import A
>
a = A()
>
for i in [1,2,3,4]:
a.objects.append(i)
>
savedData = pickle.dumps(a)
print "Saved data is ",a
print "Deleting an object"
del a.objects[3]
print a
print "Oops! This was an error: can you please recover the last saved data?"
>
print pickle.loads(savedData)
--------------------------------------------
>
Thank you for any help!
>
Mario

The problem is that the way you define 'objects', it is an attribute
of the A *class*, not the instance you create. Change the A class to:
class A(object):
def __init__(self):
self.objects = []

and rerun it; it should now work as you intended.

HTH,
George
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #3

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

Similar topics

1
by: Simon Burton | last post by:
Hi, I am pickling big graphs of data and running into this problem: File "/usr/lib/python2.2/pickle.py", line 225, in save f(self, object) File "/usr/lib/python2.2/pickle.py", line 414, in...
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...
0
by: Mike P. | last post by:
Hi all, I'm working on a simulation (can be considered a game) in Python where I want to be able to dump the simulation state to a file and be able to load it up later. I have used the standard...
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...
2
by: Victor Lin | last post by:
Hi, I encounter a problem with pickle. I download a html from: ...
3
by: fizilla | last post by:
Hello all! I have the following weird problem and since I am new to Python I somehow cannot figure out an elegant solution. The problem reduces to the following question: How to pickle a...
2
by: Nagu | last post by:
I am trying to save a dictionary of size 65000X50 to a local file and I get the memory error problem. How do I go about resolving this? Is there way to partition the pickle object and combine...
0
by: Nagu | last post by:
I am trying to save a dictionary of size 65000X50 to a local file and I get the memory error problem. How do I go about resolving this? Is there way to partition the pickle object and combine...
1
by: IceMan85 | last post by:
Hi to all, I have spent the whole morning trying, with no success to pickle an object that I have created. The error that I get is : Can't pickle 'SRE_Match' object: <_sre.SRE_Match object at...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.