__peter__ at web.de
Sun Apr 1 05:20:45 EDT 2007
Aaron Brady wrote:
> can you shelve objects with membership?
> this gives you:
> TypeError: object does not support item assignment
> dict 0 True
> Exception exceptions.TypeError: 'object does not support item assignment'
> in ignored
> > ignored is a bit mysterious. tx in advance.
> from shelve import *
> class MyShelf(DbfilenameShelf):
> def __init__(self, filename, flag='c', protocol=None,
> writeback=False, binary=None):
> DbfilenameShelf.__init__(self, filename, flag, protocol,
> writeback, binary)
> def __setattr__(self,name,value):
> if not self.ready:
> print name, value, self.ready
> def open(filename, flag='c', protocol=None, writeback=False, binary=None):
> return MyShelf(filename, flag, protocol, writeback, binary)
The root cause of your problems is that you are mixing two namespaces: that
of the shelved items and that used internally by DbfilenameShelf to
implement the shelving functionality.
While the cleanest approach is to not do it, you can make such a mix work
in /some/ cases if you give precedence to the "implementation namespace".
This requires that you pass by any implementation attributes
pass_through_attributes = [...]
def __setattr__(self, name, value):
if name in pass_through_attributes:
self.__dict__[name] = value # *
# do whatever you like
(*) Assuming that DbfilenameShelve is an oldstyle class and does not itself
implement a __setattr__() method.
>From your error message one can infer that pass_through_attributes must
contain at least the name "dict"; for a complete list you have to inspect
the Dbfilename source code.
An approach that is slightly more robust is to wrap DbfilenameShelf -- make
it an attribute of MyShelf -- and pass the relevant method calls to the
More information about the Python-list