Mercurial > cpython-lru_cache-weakref
annotate Lib/shelve.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | bfd4798b1f77 |
| children |
| rev | line source |
|---|---|
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
1 """Manage shelves of pickled objects. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
2 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
3 A "shelf" is a persistent, dictionary-like object. The difference |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
4 with dbm databases is that the values (not the keys!) in a shelf can |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
5 be essentially arbitrary Python objects -- anything that the "pickle" |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
6 module can handle. This includes most class instances, recursive data |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
7 types, and objects containing lots of shared sub-objects. The keys |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
8 are ordinary strings. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
9 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
10 To summarize the interface (key is a string, data is an arbitrary |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
11 object): |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
12 |
|
11742
840e18eec07f
Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents:
11187
diff
changeset
|
13 import shelve |
|
840e18eec07f
Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents:
11187
diff
changeset
|
14 d = shelve.open(filename) # open, with (g)dbm filename -- no suffix |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
15 |
|
11742
840e18eec07f
Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents:
11187
diff
changeset
|
16 d[key] = data # store data at key (overwrites old data if |
|
840e18eec07f
Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents:
11187
diff
changeset
|
17 # using an existing key) |
|
28774
c9bee11eaa8b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
28699
diff
changeset
|
18 data = d[key] # retrieve a COPY of the data at key (raise |
|
28699
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
19 # KeyError if no such key) -- NOTE that this |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
20 # access returns a *copy* of the entry! |
|
11742
840e18eec07f
Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents:
11187
diff
changeset
|
21 del d[key] # delete data stored at key (raises KeyError |
|
840e18eec07f
Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents:
11187
diff
changeset
|
22 # if no such key) |
|
25836
a89d014559e0
Patch #624936: Implement __contains__.
Martin v. Löwis <martin@v.loewis.de>
parents:
16613
diff
changeset
|
23 flag = d.has_key(key) # true if the key exists; same as "key in d" |
|
11742
840e18eec07f
Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents:
11187
diff
changeset
|
24 list = d.keys() # a list of all existing keys (slow!) |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
25 |
|
11742
840e18eec07f
Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents:
11187
diff
changeset
|
26 d.close() # close it |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
27 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
28 Dependent on the implementation, closing a persistent dictionary may |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
29 or may not be necessary to flush changes to disk. |
|
28699
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
30 |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
31 Normally, d[key] returns a COPY of the entry. This needs care when |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
32 mutable entries are mutated: for example, if d[key] is a list, |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
33 d[key].append(anitem) |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
34 does NOT modify the entry d[key] itself, as stored in the persistent |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
35 mapping -- it only modifies the copy, which is then immediately |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
36 discarded, so that the append has NO effect whatsoever. To append an |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
37 item to d[key] in a way that will affect the persistent mapping, use: |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
38 data = d[key] |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
39 data.append(anitem) |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
40 d[key] = data |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
41 |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
42 To avoid the problem with mutable entries, you may pass the keyword |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
43 argument writeback=True in the call to shelve.open. When you use: |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
44 d = shelve.open(filename, writeback=True) |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
45 then d keeps a cache of all entries you access, and writes them all back |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
46 to the persistent mapping when you call d.close(). This ensures that |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
47 such usage as d[key].append(anitem) works as intended. |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
48 |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
49 However, using keyword argument writeback=True may consume vast amount |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
50 of memory for the cache, and it may make d.close() very slow, if you |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
51 access many of d's entries after opening it in this way: d has no way to |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
52 check which of the entries you access are mutable and/or which ones you |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
53 actually mutate, so it must cache, and write back at close, all of the |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
54 entries that you access. You can call d.sync() to write back all the |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
55 entries in the cache, and empty the cache (d.sync() also synchronizes |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
56 the persistent dictionary on disk, if feasible). |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
57 """ |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
5728
04c28274c281
Use cPickle and cStringIO when available.
Guido van Rossum <guido@python.org>
parents:
5211
diff
changeset
|
59 # Try using cPickle and cStringIO if available. |
|
04c28274c281
Use cPickle and cStringIO when available.
Guido van Rossum <guido@python.org>
parents:
5211
diff
changeset
|
60 |
|
04c28274c281
Use cPickle and cStringIO when available.
Guido van Rossum <guido@python.org>
parents:
5211
diff
changeset
|
61 try: |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
62 from cPickle import Pickler, Unpickler |
|
5728
04c28274c281
Use cPickle and cStringIO when available.
Guido van Rossum <guido@python.org>
parents:
5211
diff
changeset
|
63 except ImportError: |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
64 from pickle import Pickler, Unpickler |
|
5728
04c28274c281
Use cPickle and cStringIO when available.
Guido van Rossum <guido@python.org>
parents:
5211
diff
changeset
|
65 |
|
04c28274c281
Use cPickle and cStringIO when available.
Guido van Rossum <guido@python.org>
parents:
5211
diff
changeset
|
66 try: |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
67 from cStringIO import StringIO |
|
5728
04c28274c281
Use cPickle and cStringIO when available.
Guido van Rossum <guido@python.org>
parents:
5211
diff
changeset
|
68 except ImportError: |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
69 from StringIO import StringIO |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 |
|
26238
edd2eb88fd6c
SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents:
25836
diff
changeset
|
71 import UserDict |
|
edd2eb88fd6c
SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents:
25836
diff
changeset
|
72 |
| 16613 | 73 __all__ = ["Shelf","BsdDbShelf","DbfilenameShelf","open"] |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
74 |
|
48071
182fca025ca0
Issue 1592: Better error reporting for operations on closed shelves.
Raymond Hettinger <python@rcn.com>
parents:
45525
diff
changeset
|
75 class _ClosedDict(UserDict.DictMixin): |
|
182fca025ca0
Issue 1592: Better error reporting for operations on closed shelves.
Raymond Hettinger <python@rcn.com>
parents:
45525
diff
changeset
|
76 'Marker for a closed dict. Access attempts raise a ValueError.' |
|
182fca025ca0
Issue 1592: Better error reporting for operations on closed shelves.
Raymond Hettinger <python@rcn.com>
parents:
45525
diff
changeset
|
77 |
|
182fca025ca0
Issue 1592: Better error reporting for operations on closed shelves.
Raymond Hettinger <python@rcn.com>
parents:
45525
diff
changeset
|
78 def closed(self, *args): |
|
182fca025ca0
Issue 1592: Better error reporting for operations on closed shelves.
Raymond Hettinger <python@rcn.com>
parents:
45525
diff
changeset
|
79 raise ValueError('invalid operation on closed shelf') |
|
182fca025ca0
Issue 1592: Better error reporting for operations on closed shelves.
Raymond Hettinger <python@rcn.com>
parents:
45525
diff
changeset
|
80 __getitem__ = __setitem__ = __delitem__ = keys = closed |
|
182fca025ca0
Issue 1592: Better error reporting for operations on closed shelves.
Raymond Hettinger <python@rcn.com>
parents:
45525
diff
changeset
|
81 |
|
182fca025ca0
Issue 1592: Better error reporting for operations on closed shelves.
Raymond Hettinger <python@rcn.com>
parents:
45525
diff
changeset
|
82 def __repr__(self): |
|
182fca025ca0
Issue 1592: Better error reporting for operations on closed shelves.
Raymond Hettinger <python@rcn.com>
parents:
45525
diff
changeset
|
83 return '<Closed Dictionary>' |
|
182fca025ca0
Issue 1592: Better error reporting for operations on closed shelves.
Raymond Hettinger <python@rcn.com>
parents:
45525
diff
changeset
|
84 |
|
26238
edd2eb88fd6c
SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents:
25836
diff
changeset
|
85 class Shelf(UserDict.DictMixin): |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
86 """Base class for shelf implementations. |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
87 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
88 This is initialized with a dictionary-like object. |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
89 See the module's __doc__ string for an overview of the interface. |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
90 """ |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
91 |
|
34100
8fd766bb76c9
Removed deprecated method arguments from the shelve module.
Raymond Hettinger <python@rcn.com>
parents:
29275
diff
changeset
|
92 def __init__(self, dict, protocol=None, writeback=False): |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
93 self.dict = dict |
|
28699
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
94 if protocol is None: |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
95 protocol = 0 |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
96 self._protocol = protocol |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
97 self.writeback = writeback |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
98 self.cache = {} |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
100 def keys(self): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
101 return self.dict.keys() |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
102 |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
103 def __len__(self): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
104 return len(self.dict) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
105 |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
106 def has_key(self, key): |
|
48173
4d8427d25f84
Remove dict.has_key() usage in the shelve module to silence warnings under -3.
Brett Cannon <bcannon@gmail.com>
parents:
48071
diff
changeset
|
107 return key in self.dict |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
108 |
|
25836
a89d014559e0
Patch #624936: Implement __contains__.
Martin v. Löwis <martin@v.loewis.de>
parents:
16613
diff
changeset
|
109 def __contains__(self, key): |
|
48173
4d8427d25f84
Remove dict.has_key() usage in the shelve module to silence warnings under -3.
Brett Cannon <bcannon@gmail.com>
parents:
48071
diff
changeset
|
110 return key in self.dict |
|
25836
a89d014559e0
Patch #624936: Implement __contains__.
Martin v. Löwis <martin@v.loewis.de>
parents:
16613
diff
changeset
|
111 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
112 def get(self, key, default=None): |
|
48173
4d8427d25f84
Remove dict.has_key() usage in the shelve module to silence warnings under -3.
Brett Cannon <bcannon@gmail.com>
parents:
48071
diff
changeset
|
113 if key in self.dict: |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
114 return self[key] |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
115 return default |
|
11187
f6e6cdbadbb5
Patch by Paul Sokolovsky to support the get() method.
Guido van Rossum <guido@python.org>
parents:
7882
diff
changeset
|
116 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
117 def __getitem__(self, key): |
|
28699
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
118 try: |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
119 value = self.cache[key] |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
120 except KeyError: |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
121 f = StringIO(self.dict[key]) |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
122 value = Unpickler(f).load() |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
123 if self.writeback: |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
124 self.cache[key] = value |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
125 return value |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
126 |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
127 def __setitem__(self, key, value): |
|
28699
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
128 if self.writeback: |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
129 self.cache[key] = value |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
130 f = StringIO() |
|
28699
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
131 p = Pickler(f, self._protocol) |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
132 p.dump(value) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
133 self.dict[key] = f.getvalue() |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
134 |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
135 def __delitem__(self, key): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
136 del self.dict[key] |
|
28699
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
137 try: |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
138 del self.cache[key] |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
139 except KeyError: |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
140 pass |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
142 def close(self): |
|
28699
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
143 self.sync() |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
144 try: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
145 self.dict.close() |
|
29275
a043af608e94
Fixup bare try/except.
Raymond Hettinger <python@rcn.com>
parents:
29179
diff
changeset
|
146 except AttributeError: |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
147 pass |
|
58862
bfd4798b1f77
Improve issue 7835 fix per MAL to handle the case that the
R. David Murray <rdmurray@bitdance.com>
parents:
58857
diff
changeset
|
148 # Catch errors that may happen when close is called from __del__ |
|
bfd4798b1f77
Improve issue 7835 fix per MAL to handle the case that the
R. David Murray <rdmurray@bitdance.com>
parents:
58857
diff
changeset
|
149 # because CPython is in interpreter shutdown. |
|
bfd4798b1f77
Improve issue 7835 fix per MAL to handle the case that the
R. David Murray <rdmurray@bitdance.com>
parents:
58857
diff
changeset
|
150 try: |
|
bfd4798b1f77
Improve issue 7835 fix per MAL to handle the case that the
R. David Murray <rdmurray@bitdance.com>
parents:
58857
diff
changeset
|
151 self.dict = _ClosedDict() |
|
bfd4798b1f77
Improve issue 7835 fix per MAL to handle the case that the
R. David Murray <rdmurray@bitdance.com>
parents:
58857
diff
changeset
|
152 except (NameError, TypeError): |
|
58857
172fdb1d7337
Issue 7835: Shelve's __del__ method calls its close method, and its
R. David Murray <rdmurray@bitdance.com>
parents:
48273
diff
changeset
|
153 self.dict = None |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
154 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
155 def __del__(self): |
|
38818
069ff3a70694
Bug #1339007: Shelf objects now don't raise an exception in their
Georg Brandl <georg@python.org>
parents:
34100
diff
changeset
|
156 if not hasattr(self, 'writeback'): |
|
069ff3a70694
Bug #1339007: Shelf objects now don't raise an exception in their
Georg Brandl <georg@python.org>
parents:
34100
diff
changeset
|
157 # __init__ didn't succeed, so don't bother closing |
|
069ff3a70694
Bug #1339007: Shelf objects now don't raise an exception in their
Georg Brandl <georg@python.org>
parents:
34100
diff
changeset
|
158 return |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
159 self.close() |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
160 |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
161 def sync(self): |
|
28699
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
162 if self.writeback and self.cache: |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
163 self.writeback = False |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
164 for key, entry in self.cache.iteritems(): |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
165 self[key] = entry |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
166 self.writeback = True |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
167 self.cache = {} |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
168 if hasattr(self.dict, 'sync'): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
169 self.dict.sync() |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
170 |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
171 |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
172 class BsdDbShelf(Shelf): |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
173 """Shelf implementation using the "BSD" db interface. |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
174 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
175 This adds methods first(), next(), previous(), last() and |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
176 set_location() that have no counterpart in [g]dbm databases. |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
177 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
178 The actual database must be opened using one of the "bsddb" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
179 modules "open" routines (i.e. bsddb.hashopen, bsddb.btopen or |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
180 bsddb.rnopen) and passed to the constructor. |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
181 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
182 See the module's __doc__ string for an overview of the interface. |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
183 """ |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
184 |
|
34100
8fd766bb76c9
Removed deprecated method arguments from the shelve module.
Raymond Hettinger <python@rcn.com>
parents:
29275
diff
changeset
|
185 def __init__(self, dict, protocol=None, writeback=False): |
|
8fd766bb76c9
Removed deprecated method arguments from the shelve module.
Raymond Hettinger <python@rcn.com>
parents:
29275
diff
changeset
|
186 Shelf.__init__(self, dict, protocol, writeback) |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
187 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
188 def set_location(self, key): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
189 (key, value) = self.dict.set_location(key) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
190 f = StringIO(value) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
191 return (key, Unpickler(f).load()) |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
192 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
193 def next(self): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
194 (key, value) = self.dict.next() |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
195 f = StringIO(value) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
196 return (key, Unpickler(f).load()) |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
197 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
198 def previous(self): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
199 (key, value) = self.dict.previous() |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
200 f = StringIO(value) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
201 return (key, Unpickler(f).load()) |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
202 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
203 def first(self): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
204 (key, value) = self.dict.first() |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
205 f = StringIO(value) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
206 return (key, Unpickler(f).load()) |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
207 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
208 def last(self): |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
209 (key, value) = self.dict.last() |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
210 f = StringIO(value) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
211 return (key, Unpickler(f).load()) |
|
2967
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
212 |
|
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
213 |
|
74953a41607c
renamed DbShelf->DbfilenameShelf;added BsdDbShelf (David Ely)
Guido van Rossum <guido@python.org>
parents:
2439
diff
changeset
|
214 class DbfilenameShelf(Shelf): |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
215 """Shelf implementation using the "anydbm" generic dbm interface. |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
216 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
217 This is initialized with the filename for the dbm database. |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
218 See the module's __doc__ string for an overview of the interface. |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
219 """ |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
220 |
|
34100
8fd766bb76c9
Removed deprecated method arguments from the shelve module.
Raymond Hettinger <python@rcn.com>
parents:
29275
diff
changeset
|
221 def __init__(self, filename, flag='c', protocol=None, writeback=False): |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
222 import anydbm |
|
34100
8fd766bb76c9
Removed deprecated method arguments from the shelve module.
Raymond Hettinger <python@rcn.com>
parents:
29275
diff
changeset
|
223 Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback) |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
224 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
225 |
|
34100
8fd766bb76c9
Removed deprecated method arguments from the shelve module.
Raymond Hettinger <python@rcn.com>
parents:
29275
diff
changeset
|
226 def open(filename, flag='c', protocol=None, writeback=False): |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
227 """Open a persistent dictionary for reading and writing. |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 |
|
28699
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
229 The filename parameter is the base filename for the underlying |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
230 database. As a side-effect, an extension may be added to the |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
231 filename and more than one file may be created. The optional flag |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
232 parameter has the same interpretation as the flag parameter of |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
233 anydbm.open(). The optional protocol parameter specifies the |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
234 version of the pickle protocol (0, 1, or 2). |
|
97890134c9c4
Patch #553171: Add writeback parameter. Also add protocol parameter.
Martin v. Löwis <martin@v.loewis.de>
parents:
27318
diff
changeset
|
235 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
236 See the module's __doc__ string for an overview of the interface. |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
237 """ |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11742
diff
changeset
|
238 |
|
34100
8fd766bb76c9
Removed deprecated method arguments from the shelve module.
Raymond Hettinger <python@rcn.com>
parents:
29275
diff
changeset
|
239 return DbfilenameShelf(filename, flag, protocol, writeback) |
