def save(self):
f=open(str(self.uid)+'.yaml')
yaml.dump(self.__dict__, f)
is there a better way to persist using Yaml
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.?
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