Ask Your Question

Object Persistence db_save error

asked 2012-12-02 13:06:17 -0500

SLOtoSF gravatar image

Take the following class. Why can't I use db_save to store an instance of this class? The reason I am using this class instead of just a set is because I will eventually derive this class from RingElement rather than SageObject, because I'm looking to create a Ring of Sets.

class FakeSet(SageObject):
    def __init__(self,_set):
        self.values = _set
        self.iterator = self.__iter__()
        if self.values != set():
    def __iter__(self):
        for this_entry in self.values:
            yield this_entry
    def get_set(self):
        return self.values
    def __repr__(self):
        return str(list(self.values))
    def __getattr__(self,attr):
        return getattr(self.values,attr)

Then I enter:

sage: fake = FakeSet([1,2,3])
sage: print fake
sage: db_save(fake,"test")

to which the interpreter responds:

Traceback (click to the left of this block for traceback)
cPickle.PicklingError: Can't pickle <type 'generator'>: attribute
lookup __builtin__.generator failed
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-12-03 00:29:57 -0500

Python does not know how to save the state of the generator object. You need to define methods to "pickle" and "unpickle" your object, i.e., save a persistent state and read it back.

There is some documentation for this in the pickle section of the Python documentation. Especially this section on pickling normal class instances is relevant.

Essentially, you need to define two methods __getstate__() and __setstate__(state).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-12-02 13:06:17 -0500

Seen: 83 times

Last updated: Dec 03 '12