First time here? Check out the FAQ!

Ask Your Question
0

Object Persistence db_save error

asked 12 years ago

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():
            self.next = self.iterator.next()
    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
Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 12 years ago

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).

Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 12 years ago

Seen: 319 times

Last updated: Dec 03 '12