yaml for persistence

Diez B. Roggisch deets at nospam.web.de
Tue Mar 3 03:45:00 EST 2009


Paul schrieb:
> class User(object):
>     def __init__(self, uid):
>        self.uid = uid
>        self.__dict__.update(yaml.load(str('uid')+'.yaml'))
> 
>     def save(self):
>         f=open(str(self.uid)+'.yaml')
>         yaml.dump(self.__dict__, f)
> 
> 
> 
> is there a better way to persist using Yaml
> 
> Paul
> http://bidegg.com

AFAIK Yaml already supports persisting python objects:

 >>> from yaml import dump
 >>> class Foo(object):
...    def __init__(self, name):
...        self.name = name
...
 >>> f = Foo('bar')
 >>> dump(f)
'!!python/object:__main__.Foo {name: bar}\n'
 >>>


And if you want to write your own persistence, I'd do that as yaml and 
pickle do as a generic function that supports the pickle protocol.

http://docs.python.org/library/pickle.html#the-pickle-protocol

Diez



More information about the Python-list mailing list