Skip to content

Commit

Permalink
More Python 3 changes. Tests no longer error (but some don't pass). I…
Browse files Browse the repository at this point in the history
…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
piannucci committed Aug 26, 2015
1 parent f8ef226 commit 2ec3b46
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 186 deletions.
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,11 @@ def write_version_py(filename='weave/version.py'):
"""
FULLVERSION, GIT_REVISION = get_version_info()

a = open(filename, 'w')
try:
with open(filename, 'w') as a:
a.write(cnt % {'version': VERSION,
'full_version' : FULLVERSION,
'git_revision' : GIT_REVISION,
'isrelease': str(ISRELEASED)})
finally:
a.close()


try:
Expand Down
64 changes: 33 additions & 31 deletions weave/_dumb_shelve.py
100644 → 100755
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)
Loading

0 comments on commit 2ec3b46

Please sign in to comment.