Giraudon Cyril | 18 Jul 2012 18:04
Picon
Favicon

Pickling compatiblity between traits3 and traits4

Hi everyone,

 From the following example I try to pickle a traits3 object and 
unpickle it as a traits4 instance :

#############################
from enthought.traits.api import HasTraits, Str, Int

class Person(HasTraits):
     name = Str("John")
     age = Int(0)

     def __getstate__(self):
         return {"name": self.name,
                       "age": self.age}

if __name__ == "__main__":
     from cPickle import dump, load
     from os import path
     filename = "person.pkl"
     if path.exists(filename):
         with open(filename, "r") as in_:
             p = load(in_)
         print "P name : ", p.name
         print "P age : ", p.age
     else:
         with open(filename, "w") as out:
             p = Person(name="Jacques", age=23)
             dump(p, out)
#############################
(Continue reading)

Robert Kern | 18 Jul 2012 18:28
Gravatar

Re: Pickling compatiblity between traits3 and traits4

On Wed, Jul 18, 2012 at 5:04 PM, Giraudon Cyril <cyril.giraudon@...> wrote:
> Hi everyone,
>
>  From the following example I try to pickle a traits3 object and
> unpickle it as a traits4 instance :
>
> #############################
> from enthought.traits.api import HasTraits, Str, Int
>
> class Person(HasTraits):
>      name = Str("John")
>      age = Int(0)
>
>      def __getstate__(self):
>          return {"name": self.name,
>                        "age": self.age}
>
> if __name__ == "__main__":
>      from cPickle import dump, load
>      from os import path
>      filename = "person.pkl"
>      if path.exists(filename):
>          with open(filename, "r") as in_:
>              p = load(in_)
>          print "P name : ", p.name
>          print "P age : ", p.age
>      else:
>          with open(filename, "w") as out:
>              p = Person(name="Jacques", age=23)
>              dump(p, out)
(Continue reading)

Cyril Giraudon | 19 Jul 2012 13:13
Picon
Favicon

Re: Pickling compatiblity between traits3 and traits4

Le 18/07/2012 18:28, Robert Kern a écrit :
> On Wed, Jul 18, 2012 at 5:04 PM, Giraudon Cyril <cyril.giraudon@...> wrote:
>> Hi everyone,
>>
>>   From the following example I try to pickle a traits3 object and
>> unpickle it as a traits4 instance :
>>
>> #############################
>> from enthought.traits.api import HasTraits, Str, Int
>>
>> class Person(HasTraits):
>>       name = Str("John")
>>       age = Int(0)
>>
>>       def __getstate__(self):
>>           return {"name": self.name,
>>                         "age": self.age}
>>
>> if __name__ == "__main__":
>>       from cPickle import dump, load
>>       from os import path
>>       filename = "person.pkl"
>>       if path.exists(filename):
>>           with open(filename, "r") as in_:
>>               p = load(in_)
>>           print "P name : ", p.name
>>           print "P age : ", p.age
>>       else:
>>           with open(filename, "w") as out:
>>               p = Person(name="Jacques", age=23)
(Continue reading)

Robert Kern | 20 Jul 2012 11:02
Gravatar

Re: Pickling compatiblity between traits3 and traits4

On Thu, Jul 19, 2012 at 12:13 PM, Cyril Giraudon <cyril.giraudon <at> free.fr> wrote:
> Le 18/07/2012 18:28, Robert Kern a écrit :
>> On Wed, Jul 18, 2012 at 5:04 PM, Giraudon Cyril <cyril.giraudon <at> free.fr> wrote:
>>> Hi everyone,
>>>
>>>   From the following example I try to pickle a traits3 object and
>>> unpickle it as a traits4 instance :
>>>
>>> #############################
>>> from enthought.traits.api import HasTraits, Str, Int
>>>
>>> class Person(HasTraits):
>>>       name = Str("John")
>>>       age = Int(0)
>>>
>>>       def __getstate__(self):
>>>           return {"name": self.name,
>>>                         "age": self.age}
>>>
>>> if __name__ == "__main__":
>>>       from cPickle import dump, load
>>>       from os import path
>>>       filename = "person.pkl"
>>>       if path.exists(filename):
>>>           with open(filename, "r") as in_:
>>>               p = load(in_)
>>>           print "P name : ", p.name
>>>           print "P age : ", p.age
>>>       else:
>>>           with open(filename, "w") as out:
(Continue reading)


Gmane