Re: [Tutor] setstate trouble when unpickling

2008-01-27 Thread rspil

I load a cellule with 
self.cellule=pickle.load(prjfile)

Within the class cellule I use this method  to add an attribute note
,with success:
def __setstate__(self, state):
#20080127 ajoute note
if 'note' not in state:
self.note=***
self.__dict__.update(state)

self.note=*** is the new attribute in __init__

I checked with a debug: after the pickle.load the class instance is created
without the attribute note. The method __setstate__ is called and the new
attribute is added.

With python 2.5.1

Robert



Jeff Peery-2 wrote:
 
 Hello,

   I've got a fairly simple class instance that I'm pickling. I'm using
 setstate to update the instance when I unpickle. Although the setstate
 doesn't seem to be called upon unpickling... here's an example, if anyone
 see's what I'm doing wrong, please let me know. Thanks!

   so I say start out with this class:

   class dog:
   def __init__(self):
   self.num_legs = 4

   then I pickle the instance of:
   sparky = dog()

   then I update my dog class to:
   class dog:
   def __init__(self):
   self.hair_color = 'brown'
   self.num_legs = 4

   def __setstate__(self, d):
   if 'hair_color' not in d:
   d['hair_color'] = 'brown'
   self.__dict__.update(d)
   print 'did this work?'

   Now when I unpickle the original pickled object sparky I would hope to
 see 'did this work' and I would also hope that sparky would have updated
 to have the attribute 'hair_color' but nothing of the sort happens. if
 just unpickles sparky without updating the attributes as I was hoping.

   Thanks!

   Jeff
 
 
 

 -
 Never miss a thing.   Make Yahoo your homepage.
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
 

-- 
View this message in context: 
http://www.nabble.com/setstate-trouble-when-unpickling-tp15082091p15121172.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] setstate trouble when unpickling

2008-01-25 Thread Kent Johnson
Jeff Peery wrote:
 Hello,
  
 I've got a fairly simple class instance that I'm pickling. I'm using 
 setstate to update the instance when I unpickle. Although the setstate 
 doesn't seem to be called upon unpickling... here's an example, if 
 anyone see's what I'm doing wrong, please let me know. Thanks!
  
 so I say start out with this class:
  
 class dog:
 def __init__(self):
 self.num_legs = 4
  
 then I pickle the instance of:
 sparky = dog()
  
 then I update my dog class to:
 class dog:
 def __init__(self):
 self.hair_color = 'brown'
 self.num_legs = 4
  
 def __setstate__(self, d):
 if 'hair_color' not in d:
 d['hair_color'] = 'brown'
 self.__dict__.update(d)
 print 'did this work?'
  
 Now when I unpickle the original pickled object sparky I would hope to 
 see 'did this work' and I would also hope that sparky would have updated 
 to have the attribute 'hair_color' but nothing of the sort happens. if 
 just unpickles sparky without updating the attributes as I was hoping.

Are you sure the unpickle program is getting the new definition of dog? 
Can you show more code and tell us how you are running it?

This does what you expect, when run in a single program:

import pickle

class dog:
 def __init__(self):
 self.num_legs = 4

sparky = dog()
p = pickle.dumps(sparky)

class dog:
 def __init__(self):
 self.hair_color = 'brown'
 self.num_legs = 4

 def __setstate__(self, d):
 if 'hair_color' not in d:
 d['hair_color'] = 'brown'
 self.__dict__.update(d)
 print 'did this work?'

sparky2 = pickle.loads(p)
print sparky2.hair_color


prints:
did this work?
brown

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] setstate trouble when unpickling

2008-01-24 Thread Jeff Peery
Hello,
   
  I've got a fairly simple class instance that I'm pickling. I'm using setstate 
to update the instance when I unpickle. Although the setstate doesn't seem to 
be called upon unpickling... here's an example, if anyone see's what I'm doing 
wrong, please let me know. Thanks!
   
  so I say start out with this class:
   
  class dog:
  def __init__(self):
  self.num_legs = 4
   
  then I pickle the instance of:
  sparky = dog()
   
  then I update my dog class to:
  class dog:
  def __init__(self):
  self.hair_color = 'brown'
  self.num_legs = 4
   
  def __setstate__(self, d):
  if 'hair_color' not in d:
  d['hair_color'] = 'brown'
  self.__dict__.update(d)
  print 'did this work?'
   
  Now when I unpickle the original pickled object sparky I would hope to see 
'did this work' and I would also hope that sparky would have updated to have 
the attribute 'hair_color' but nothing of the sort happens. if just unpickles 
sparky without updating the attributes as I was hoping.
   
  Thanks!
   
  Jeff



   
-
Never miss a thing.   Make Yahoo your homepage.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor