7
7stud
The following code demonstrates that a collections.defaultdict is
shelve worthy:
import shelve
import collections as c
dd = c.defaultdict(int)
dd["Joe"] = 3
print(dd)
my_shelve = shelve.open('data.shelve')
my_shelve['strike_record'] = dd
my_shelve.close()
my_shelve = shelve.open('data.shelve')
data = my_shelve['strike_record']
my_shelve.close()
dd.clear()
dd.update(data)
print(dd)
--output:--
defaultdict(<class 'int'>, {'Joe': 3})
defaultdict(<class 'int'>, {'Joe': 3})
And the following code demonstrates that a class that inherits from
dict can shelve itself:
import collections as c
import shelve
class Dog(dict):
def __init__(self):
super().__init__(Joe=1)
print('****', self)
def save(self):
my_shelve = shelve.open('data22.shelve')
my_shelve['x'] = self
my_shelve.close()
def load(self):
my_shelve = shelve.open('data22.shelve')
data = my_shelve['x']
my_shelve.close()
print(data)
d = Dog()
d.save()
d.load()
--output:--
**** {'Joe': 1}
{'Joe': 1}
But I cannot get a class that inherits from collections.defaultdict to
shelve itself:
import collections as c
import shelve
class Dog(c.defaultdict):
def __init__(self):
super().__init__(int, Joe=0)
print('****', self)
def save(self):
my_shelve = shelve.open('data22.shelve')
my_shelve['dd'] = self
my_shelve.close()
def load(self):
my_shelve = shelve.open('data22.shelve')
data = my_shelve['dd']
my_shelve.close()
print(data)
d = Dog()
d.save()
d.load()
--output:--
**** defaultdict(<class 'int'>, {'Joe': 30})
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/
python3.2/shelve.py", line 111, in __getitem__
value = self.cache[key]
KeyError: 'dd'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "3.py", line 95, in <module>
d.load()
File "3.py", line 87, in load
data = my_shelve['dd']
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/
python3.2/shelve.py", line 114, in __getitem__
value = Unpickler(f).load()
TypeError: __init__() takes exactly 1 positional argument (2 given)
I deleted all *.shelve.db files between program runs. I can't figure
out what I'm doing wrong.
shelve worthy:
import shelve
import collections as c
dd = c.defaultdict(int)
dd["Joe"] = 3
print(dd)
my_shelve = shelve.open('data.shelve')
my_shelve['strike_record'] = dd
my_shelve.close()
my_shelve = shelve.open('data.shelve')
data = my_shelve['strike_record']
my_shelve.close()
dd.clear()
dd.update(data)
print(dd)
--output:--
defaultdict(<class 'int'>, {'Joe': 3})
defaultdict(<class 'int'>, {'Joe': 3})
And the following code demonstrates that a class that inherits from
dict can shelve itself:
import collections as c
import shelve
class Dog(dict):
def __init__(self):
super().__init__(Joe=1)
print('****', self)
def save(self):
my_shelve = shelve.open('data22.shelve')
my_shelve['x'] = self
my_shelve.close()
def load(self):
my_shelve = shelve.open('data22.shelve')
data = my_shelve['x']
my_shelve.close()
print(data)
d = Dog()
d.save()
d.load()
--output:--
**** {'Joe': 1}
{'Joe': 1}
But I cannot get a class that inherits from collections.defaultdict to
shelve itself:
import collections as c
import shelve
class Dog(c.defaultdict):
def __init__(self):
super().__init__(int, Joe=0)
print('****', self)
def save(self):
my_shelve = shelve.open('data22.shelve')
my_shelve['dd'] = self
my_shelve.close()
def load(self):
my_shelve = shelve.open('data22.shelve')
data = my_shelve['dd']
my_shelve.close()
print(data)
d = Dog()
d.save()
d.load()
--output:--
**** defaultdict(<class 'int'>, {'Joe': 30})
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/
python3.2/shelve.py", line 111, in __getitem__
value = self.cache[key]
KeyError: 'dd'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "3.py", line 95, in <module>
d.load()
File "3.py", line 87, in load
data = my_shelve['dd']
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/
python3.2/shelve.py", line 114, in __getitem__
value = Unpickler(f).load()
TypeError: __init__() takes exactly 1 positional argument (2 given)
I deleted all *.shelve.db files between program runs. I can't figure
out what I'm doing wrong.