473,549 Members | 4,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update with pickle

Is it possible to use the pickle module to update an object instead of
creating a new instance?
Thx
Nicolas
Jul 18 '05 #1
16 3503
Nicolas Fleury wrote:
Is it possible to use the pickle module to update an object instead of
creating a new instance?


I don't get what you want to do. Can you explain yourself better? What
do you mean by "updating" an object?

--
Ciao,
Matteo
Jul 18 '05 #2
Matteo Dell'Amico wrote:
Nicolas Fleury wrote:
Is it possible to use the pickle module to update an object instead of
creating a new instance?

I don't get what you want to do. Can you explain yourself better? What
do you mean by "updating" an object?


Use the same reference instead of creating a new instance. Basically
making pickle call __init__ of an existing object. The best solution I
see for now is to copy the __dict__ of the new object to the existing
one. I want to update the content of an object with a dumped object of
the same type so that all reference to existing object are still valid.

Regards,
Nicolas
Jul 18 '05 #3
Nicolas Fleury wrote:
I don't get what you want to do. Can you explain yourself better? What
do you mean by "updating" an object?

Use the same reference instead of creating a new instance. Basically
making pickle call __init__ of an existing object. The best solution I
see for now is to copy the __dict__ of the new object to the existing
one. I want to update the content of an object with a dumped object of
the same type so that all reference to existing object are still valid.


I think it's not possible to do automatically. Beware of copying
__dict__, since starting with python 2.2, it's possible to have data
that doesn't reside in __dict__, for instance when using __slots__.

I guess you have to find a way to encapsulate information, and maybe a
proxy could be OK for you (I can't find the recipe in the Python
Cookbook right now, since it appears to be down). That way, you could
keep all references to the proxy, and change the encapsulated data when
you need it.

--
Ciao,
Matteo
Jul 18 '05 #4
Nicolas Fleury wrote:
I want to update the content of an object with a dumped object of the
same type so that all reference to existing object are still valid.


Add interfaces to your object's class for mutability rather than using a
low-level hack. Something like this (untested):
class cls(object):
__slots__ = ('foo')
def __init__(self, foo=None):
self.foo = foo

def set_foo(self, foo):
self.foo = foo
foo = cls(42)
bar = foo
print bar.foo # 42
foo.set_foo(43)
print bar.foo # 43
Jul 18 '05 #5
Leif K-Brooks wrote:
Nicolas Fleury wrote:
I want to update the content of an object with a dumped object of the
same type so that all reference to existing object are still valid.


Add interfaces to your object's class for mutability rather than using a
low-level hack. Something like this (untested):


Thx for your answer, but according to what I read about __slots__, this
feature has never been intended to be used that way either, so it's also
a kinda low-level hack, no? It is still an interesting solution.

Regards,
Nicolas
Jul 18 '05 #6
Nicolas Fleury wrote:
Leif K-Brooks wrote:
Add interfaces to your object's class for mutability rather than using
a low-level hack. Something like this (untested):


Thx for your answer, but according to what I read about __slots__, this
feature has never been intended to be used that way either, so it's also
a kinda low-level hack, no? It is still an interesting solution.


Using __slots__ is just an optimization, it isn't directly related to
the example. This would also work:
class cls:
def __init__(self, foo=None):
self.foo = foo

def set_foo(self, foo):
self.foo = foo
foo = cls(42)
bar = foo
print bar.foo # 42
foo.set_foo(43)
print bar.foo # 43
Jul 18 '05 #7
Hi there (,hi Nicolas !),

I'v been looking for something similar recently,
so this thread caught my attention, but...

Leif K-Brooks wrote:
Using __slots__ is just an optimization, it isn't directly related to
the example. This would also work:
class cls:
def __init__(self, foo=None):
self.foo = foo

def set_foo(self, foo):
self.foo = foo
foo = cls(42)
bar = foo
print bar.foo # 42
foo.set_foo(43)
print bar.foo # 43


....I'm still lost: how is this interface-enriched declaration helping
in the task to retrieve ('internalize') an object's state from a pickle ?

Even if I set the object's state via some 'set' manipulators, I still have
to retrieve the state from the pickle and identify the object it is targetted at.

Looking again into the docs for the pickle protocol, I wonder whether there
isn't any place in the __init__/__reduce__/__setstate__ magic to throw in
some meta-programming (i.e. an intelligent metaclass doing the job), or providing
a clever __new__ operator that doesn't return a new object but an existing one,
etc., etc.

Regards,
Stefan

Jul 18 '05 #8
Matteo Dell'Amico wrote:
I guess you have to find a way to encapsulate information, and maybe a
proxy could be OK for you (I can't find the recipe in the Python
Cookbook right now, since it appears to be down).


Now it works again. Have a look at
http://aspn.activestate.com/ASPN/Coo.../Recipe/252151 . You
can, without problems, save _obj and then restore it with pickle.
I think this solves your problem.

--
Ciao,
Matteo
Jul 18 '05 #9
Stefan Seefeld wrote:
Hi there (,hi Nicolas !), ...I'm still lost: how is this interface-enriched declaration helping
in the task to retrieve ('internalize') an object's state from a pickle ?
Hi Stefan,
You're right, the example shows how to copy and it creates a new
reference, so it doesn't help in the problem (I realized it when going
back home yesterday).
Looking again into the docs for the pickle protocol, I wonder whether there
isn't any place in the __init__/__reduce__/__setstate__ magic to throw in
some meta-programming (i.e. an intelligent metaclass doing the job), or
providing
a clever __new__ operator that doesn't return a new object but an
existing one,
etc., etc.


I just added a feature request on sf to have an updating load function
in pickle. My preferred solution for now might still be the copy of
__dict__ (I could copy slots also), it's simple and not intrusive (using
proxies is, even if less a hack). However, the cleanest solution would
be to implement your solution, but I have no idea how to do it for now.

Regards,
Nicolas
Jul 18 '05 #10

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

Similar topics

3
4005
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 Pickle.memoize(): assert id(obj) not in self.memo I'm shrinking the offending data structure down to find the problem
0
1771
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 Python pickle module and it works fine pickling/unpickling from files. However, I want to be able to use a third party tool like an XML editor (or...
6
12317
by: Jim Lewis | last post by:
Pickling an instance of a class, gives "can't pickle instancemethod objects". What does this mean? How do I find the class method creating the problem?
10
4423
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 and it appears that shelve probably isn't the best option for various reasons. This lead me to try messing w/ pickle, but I can't figure out how to...
5
92944
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 error. In these cases, pickle knows the instance or class, and the method, so what's the problem? Pickle doesn't serialize code objects, so why can't it...
3
6083
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 collections.defaultdict object that has set the default_factory property? For Example (from the IDLE console): >>> words =...
2
3512
by: Michele Simionato | last post by:
Can somebody explain what's happening with the following script? $ echo example.py import pickle class Example(object): def __init__(self, obj, registry): self._obj = obj self._registry = registry
2
4495
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 later if this is a problem due to limited resources (memory) on the machine (it is 32 bit machine Win XP, with 4GB RAM). Here is the detail...
1
6331
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 0x2a969c0ad0> the complete stack is the following : Traceback (most recent call last): File "manager.py", line 305, in ? commandLineExec...
0
7526
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7723
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7480
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5373
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.