shelf membership

A

Aaron Brady

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):
self.__dict__['ready']=False
DbfilenameShelf.__init__(self, filename, flag, protocol,
writeback, binary)
self.ready=True
def __setattr__(self,name,value):
if not self.ready:
self.__dict__[name]=value
else:
print name, value, self.ready
self.__dict__[name]=value
DbfilenameShelf.__setitem__(self,name,value)

def open(filename, flag='c', protocol=None, writeback=False, binary=None):
return MyShelf(filename, flag, protocol, writeback, binary)
 
J

John Machin

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):
self.__dict__['ready']=False
DbfilenameShelf.__init__(self, filename, flag, protocol,
writeback, binary)
self.ready=True
def __setattr__(self,name,value):
if not self.ready:
self.__dict__[name]=value
else:
print name, value, self.ready
self.__dict__[name]=value
DbfilenameShelf.__setitem__(self,name,value)

def open(filename, flag='c', protocol=None, writeback=False, binary=None):
return MyShelf(filename, flag, protocol, writeback, binary)

Please supply the *full* traceback, using copy/paste.
 
G

Gabriel Genellina

Please supply the *full* traceback, using copy/paste.

Not this time. Exceptions in __del__ are ignored and all you got is that
message.

To the OP: This comes from Shelf.__del__, which in turn calls close(),
which tries to set the internal dict to 0 (?!). You may try overriding
close() - but what do you want to achieve, exactly?
 
P

Peter Otten

Aaron said:
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):
                  self.__dict__['ready']=False
                  DbfilenameShelf.__init__(self, filename, flag, protocol, 
writeback, binary)
                  self.ready=True
          def __setattr__(self,name,value):
                  if not self.ready:
                          self.__dict__[name]=value
                  else:
                          print name, value, self.ready
                          self.__dict__[name]=value
                          DbfilenameShelf.__setitem__(self,name,value)

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 # *
else:
# 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
attribute.

Peter
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top