|
31 | 31 | # Try using cPickle and cStringIO if available. |
32 | 32 |
|
33 | 33 | try: |
34 | | - from cPickle import Pickler, Unpickler |
| 34 | + from cPickle import Pickler, Unpickler |
35 | 35 | except ImportError: |
36 | | - from pickle import Pickler, Unpickler |
| 36 | + from pickle import Pickler, Unpickler |
37 | 37 |
|
38 | 38 | try: |
39 | | - from cStringIO import StringIO |
| 39 | + from cStringIO import StringIO |
40 | 40 | except ImportError: |
41 | | - from StringIO import StringIO |
| 41 | + from StringIO import StringIO |
42 | 42 |
|
43 | 43 |
|
44 | 44 | class Shelf: |
45 | | - """Base class for shelf implementations. |
46 | | -
|
47 | | - This is initialized with a dictionary-like object. |
48 | | - See the module's __doc__ string for an overview of the interface. |
49 | | - """ |
50 | | - |
51 | | - def __init__(self, dict): |
52 | | - self.dict = dict |
53 | | - |
54 | | - def keys(self): |
55 | | - return self.dict.keys() |
56 | | - |
57 | | - def __len__(self): |
58 | | - return len(self.dict) |
59 | | - |
60 | | - def has_key(self, key): |
61 | | - return self.dict.has_key(key) |
62 | | - |
63 | | - def get(self, key, default=None): |
64 | | - if self.dict.has_key(key): |
65 | | - return self[key] |
66 | | - return default |
67 | | - |
68 | | - def __getitem__(self, key): |
69 | | - f = StringIO(self.dict[key]) |
70 | | - return Unpickler(f).load() |
71 | | - |
72 | | - def __setitem__(self, key, value): |
73 | | - f = StringIO() |
74 | | - p = Pickler(f) |
75 | | - p.dump(value) |
76 | | - self.dict[key] = f.getvalue() |
77 | | - |
78 | | - def __delitem__(self, key): |
79 | | - del self.dict[key] |
80 | | - |
81 | | - def close(self): |
82 | | - try: |
83 | | - self.dict.close() |
84 | | - except: |
85 | | - pass |
86 | | - self.dict = 0 |
87 | | - |
88 | | - def __del__(self): |
89 | | - self.close() |
90 | | - |
91 | | - def sync(self): |
92 | | - if hasattr(self.dict, 'sync'): |
93 | | - self.dict.sync() |
94 | | - |
| 45 | + """Base class for shelf implementations. |
| 46 | +
|
| 47 | + This is initialized with a dictionary-like object. |
| 48 | + See the module's __doc__ string for an overview of the interface. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__(self, dict): |
| 52 | + self.dict = dict |
| 53 | + |
| 54 | + def keys(self): |
| 55 | + return self.dict.keys() |
| 56 | + |
| 57 | + def __len__(self): |
| 58 | + return len(self.dict) |
| 59 | + |
| 60 | + def has_key(self, key): |
| 61 | + return self.dict.has_key(key) |
| 62 | + |
| 63 | + def get(self, key, default=None): |
| 64 | + if self.dict.has_key(key): |
| 65 | + return self[key] |
| 66 | + return default |
| 67 | + |
| 68 | + def __getitem__(self, key): |
| 69 | + f = StringIO(self.dict[key]) |
| 70 | + return Unpickler(f).load() |
| 71 | + |
| 72 | + def __setitem__(self, key, value): |
| 73 | + f = StringIO() |
| 74 | + p = Pickler(f) |
| 75 | + p.dump(value) |
| 76 | + self.dict[key] = f.getvalue() |
| 77 | + |
| 78 | + def __delitem__(self, key): |
| 79 | + del self.dict[key] |
| 80 | + |
| 81 | + def close(self): |
| 82 | + try: |
| 83 | + self.dict.close() |
| 84 | + except: |
| 85 | + pass |
| 86 | + self.dict = 0 |
| 87 | + |
| 88 | + def __del__(self): |
| 89 | + self.close() |
| 90 | + |
| 91 | + def sync(self): |
| 92 | + if hasattr(self.dict, 'sync'): |
| 93 | + self.dict.sync() |
| 94 | + |
95 | 95 |
|
96 | 96 | class BsdDbShelf(Shelf): |
97 | | - """Shelf implementation using the "BSD" db interface. |
| 97 | + """Shelf implementation using the "BSD" db interface. |
98 | 98 |
|
99 | | - This adds methods first(), next(), previous(), last() and |
100 | | - set_location() that have no counterpart in [g]dbm databases. |
| 99 | + This adds methods first(), next(), previous(), last() and |
| 100 | + set_location() that have no counterpart in [g]dbm databases. |
101 | 101 |
|
102 | | - The actual database must be opened using one of the "bsddb" |
103 | | - modules "open" routines (i.e. bsddb.hashopen, bsddb.btopen or |
104 | | - bsddb.rnopen) and passed to the constructor. |
| 102 | + The actual database must be opened using one of the "bsddb" |
| 103 | + modules "open" routines (i.e. bsddb.hashopen, bsddb.btopen or |
| 104 | + bsddb.rnopen) and passed to the constructor. |
105 | 105 |
|
106 | | - See the module's __doc__ string for an overview of the interface. |
107 | | - """ |
| 106 | + See the module's __doc__ string for an overview of the interface. |
| 107 | + """ |
108 | 108 |
|
109 | | - def __init__(self, dict): |
110 | | - Shelf.__init__(self, dict) |
| 109 | + def __init__(self, dict): |
| 110 | + Shelf.__init__(self, dict) |
111 | 111 |
|
112 | | - def set_location(self, key): |
113 | | - (key, value) = self.dict.set_location(key) |
114 | | - f = StringIO(value) |
115 | | - return (key, Unpickler(f).load()) |
| 112 | + def set_location(self, key): |
| 113 | + (key, value) = self.dict.set_location(key) |
| 114 | + f = StringIO(value) |
| 115 | + return (key, Unpickler(f).load()) |
116 | 116 |
|
117 | | - def next(self): |
118 | | - (key, value) = self.dict.next() |
119 | | - f = StringIO(value) |
120 | | - return (key, Unpickler(f).load()) |
| 117 | + def next(self): |
| 118 | + (key, value) = self.dict.next() |
| 119 | + f = StringIO(value) |
| 120 | + return (key, Unpickler(f).load()) |
121 | 121 |
|
122 | | - def previous(self): |
123 | | - (key, value) = self.dict.previous() |
124 | | - f = StringIO(value) |
125 | | - return (key, Unpickler(f).load()) |
| 122 | + def previous(self): |
| 123 | + (key, value) = self.dict.previous() |
| 124 | + f = StringIO(value) |
| 125 | + return (key, Unpickler(f).load()) |
126 | 126 |
|
127 | | - def first(self): |
128 | | - (key, value) = self.dict.first() |
129 | | - f = StringIO(value) |
130 | | - return (key, Unpickler(f).load()) |
| 127 | + def first(self): |
| 128 | + (key, value) = self.dict.first() |
| 129 | + f = StringIO(value) |
| 130 | + return (key, Unpickler(f).load()) |
131 | 131 |
|
132 | | - def last(self): |
133 | | - (key, value) = self.dict.last() |
134 | | - f = StringIO(value) |
135 | | - return (key, Unpickler(f).load()) |
| 132 | + def last(self): |
| 133 | + (key, value) = self.dict.last() |
| 134 | + f = StringIO(value) |
| 135 | + return (key, Unpickler(f).load()) |
136 | 136 |
|
137 | 137 |
|
138 | 138 | class DbfilenameShelf(Shelf): |
139 | | - """Shelf implementation using the "anydbm" generic dbm interface. |
| 139 | + """Shelf implementation using the "anydbm" generic dbm interface. |
| 140 | +
|
| 141 | + This is initialized with the filename for the dbm database. |
| 142 | + See the module's __doc__ string for an overview of the interface. |
| 143 | + """ |
140 | 144 |
|
141 | | - This is initialized with the filename for the dbm database. |
142 | | - See the module's __doc__ string for an overview of the interface. |
143 | | - """ |
144 | | - |
145 | | - def __init__(self, filename, flag='c'): |
146 | | - import anydbm |
147 | | - Shelf.__init__(self, anydbm.open(filename, flag)) |
| 145 | + def __init__(self, filename, flag='c'): |
| 146 | + import anydbm |
| 147 | + Shelf.__init__(self, anydbm.open(filename, flag)) |
148 | 148 |
|
149 | 149 |
|
150 | 150 | def open(filename, flag='c'): |
151 | | - """Open a persistent dictionary for reading and writing. |
| 151 | + """Open a persistent dictionary for reading and writing. |
| 152 | +
|
| 153 | + Argument is the filename for the dbm database. |
| 154 | + See the module's __doc__ string for an overview of the interface. |
| 155 | + """ |
152 | 156 |
|
153 | | - Argument is the filename for the dbm database. |
154 | | - See the module's __doc__ string for an overview of the interface. |
155 | | - """ |
156 | | - |
157 | | - return DbfilenameShelf(filename, flag) |
| 157 | + return DbfilenameShelf(filename, flag) |
0 commit comments