problems with shelve(), collections.defaultdict, self

7stud 7stud at excite.com
Fri Feb 10 22:30:47 EST 2012


On Feb 10, 7:52 pm, 7stud <7s... at excite.com> wrote:

I don't know if this helps, but I notice when I initially do this:

shelve.open('data22')

the file is saved as 'data22.db'.   But on subsequent calls to
shelve.open(), if I use the file name 'data22.db', I get a different
error:

--output:--

**** defaultdict(<class 'int'>, {'Joe': 30})
Traceback (most recent call last):
  File "3.py", line 95, in <module>
    d.load()
  File "3.py", line 86, in load
    my_shelve = shelve.open('data22.db')
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/
python3.2/shelve.py", line 232, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/
python3.2/shelve.py", line 216, in __init__
    Shelf.__init__(self, dbm.open(filename, flag), protocol,
writeback)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/
python3.2/dbm/__init__.py", line 83, in open
    raise error[0]("db type could not be determined")
dbm.error: db type could not be determined



The code that produced that error:



import collections as c
import shelve

class Dog(c.defaultdict):
    def __init__(self):
        super().__init__(int, Joe=30)
        print('****', self)

    def save(self):
        my_shelve = shelve.open('data22')
        my_shelve['dd'] = self
        my_shelve.close()

    def load(self):
        my_shelve = shelve.open('data22.db')
        data = my_shelve['dd']
        my_shelve.close()

        print(data)


d = Dog()
d.save()
d.load()


I'm using python 3.2.2.



More information about the Python-list mailing list