-
-
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.
Merge remote-tracking branch 'piannucci/python3'
- Loading branch information
Showing
46 changed files
with
825 additions
and
1,396 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
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
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 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.