-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More Python 3 changes. Tests no longer error (but some don't pass). I…
…terators for map, filter, and range passed to list() as needed in many places. Module membership search uses object identity rather than equality now. Reconciled almost all differences between the built-in dumb dbm module and the patched version in weave. Rewrote dumb shelve module to rely more heavily on the built-in shelve module, with the zlib compression functionality factored out.
- Loading branch information
Showing
9 changed files
with
267 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,53 @@ | ||
from __future__ import division, print_function, absolute_import | ||
|
||
from shelve import Shelf | ||
import collections | ||
try: | ||
import zlib | ||
except ImportError: | ||
# Some python installations don't have zlib. | ||
pass | ||
zlib = None | ||
|
||
import pickle | ||
class ZlibMapping(collections.MutableMapping): | ||
"""Mapping adapter that applies zlib compression. | ||
""" | ||
|
||
def __init__(self, dict): | ||
self.dict = dict | ||
if hasattr(dict, 'close'): | ||
self.close = dict.close | ||
if hasattr(dict, 'sync'): | ||
self.sync = dict.sync | ||
if hasattr(dict, 'keys'): | ||
self.keys = dict.keys | ||
|
||
class DbfilenameShelf(Shelf): | ||
"""Shelf implementation using the "anydbm" generic dbm interface. | ||
def __iter__(self): | ||
return iter(self.dict) | ||
|
||
This is initialized with the filename for the dbm database. | ||
See the module's __doc__ string for an overview of the interface. | ||
""" | ||
def __len__(self): | ||
return len(self.dict) | ||
|
||
def __init__(self, filename, flag='c'): | ||
from . import _dumbdbm_patched | ||
Shelf.__init__(self, _dumbdbm_patched.open(filename, flag)) | ||
def __contains__(self, key): | ||
return key in self.dict | ||
|
||
def __getitem__(self, key): | ||
compressed = self.dict[key] | ||
try: | ||
r = zlib.decompress(compressed) | ||
except zlib.error: | ||
r = compressed | ||
except NameError: | ||
r = compressed | ||
|
||
return pickle.loads(r) | ||
return zlib.decompress(self.dict[key]) | ||
|
||
def __setitem__(self, key, value): | ||
s = pickle.dumps(value,1) | ||
try: | ||
self.dict[key] = zlib.compress(s) | ||
except NameError: | ||
#zlib doesn't exist, leave it uncompressed. | ||
self.dict[key] = s | ||
self.dict[key] = zlib.compress(value) | ||
|
||
def __delitem__(self, key): | ||
del self.dict[key] | ||
|
||
|
||
def open(filename, flag='c'): | ||
"""Open a persistent dictionary for reading and writing. | ||
Argument is the filename for the dbm database. | ||
See the module's __doc__ string for an overview of the interface. | ||
The filename parameter is the base filename for the underlying | ||
database. As a side-effect, an extension may be added to the | ||
filename and more than one file may be created. The optional flag | ||
parameter has the same interpretation as the flag parameter of | ||
dbm.open(). | ||
""" | ||
writeback = False | ||
|
||
return DbfilenameShelf(filename, flag) | ||
from . import _dumbdbm_patched | ||
d = _dumbdbm_patched.open(filename, flag) | ||
return Shelf(ZlibMapping(d) if zlib is not None else d) |
Oops, something went wrong.