yaml for persistence

P

Paul

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
 
A

Aaron Brady

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

Paulhttp://bidegg.com

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.?
 
D

Diez B. Roggisch

Paul said:
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:
.... def __init__(self, name):
.... self.name = name
....

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top