Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

yaml for persistence

1 view
Skip to first unread message

Paul

unread,
Mar 2, 2009, 9:19:29 PM3/2/09
to
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

Aaron Brady

unread,
Mar 3, 2009, 12:53:01 AM3/3/09
to

Just a design consideration: Perhaps you want a separate dictionary to
contain the YAML attributes. You can delegate extra objects to it
with getattr and setattr. Or get extra objects with getitem and
setitem. Otherwise, you might want 'save' and 'uid' to have a leading
underscore.

By better, do you mean more concise, easier to access, more robust, or
etc.?

Diez B. Roggisch

unread,
Mar 3, 2009, 3:45:00 AM3/3/09
to
Paul schrieb:

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

0 new messages