~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/weave/dumb_shelve.py

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik
  • Date: 2008-06-16 22:58:01 UTC
  • mfrom: (2.1.24 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616225801-irdhrpcwiocfbcmt
Tags: 0.6.0-12
* The description updated to match the current SciPy (Closes: #489149).
* Standards-Version bumped to 3.8.0 (no action needed)
* Build-Depends: netcdf-dev changed to libnetcdf-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from shelve import Shelf
2
 
import zlib
3
 
import cPickle
4
 
import dumbdbm_patched
5
 
 
6
 
class DbfilenameShelf(Shelf):
7
 
    """Shelf implementation using the "anydbm" generic dbm interface.
8
 
 
9
 
    This is initialized with the filename for the dbm database.
10
 
    See the module's __doc__ string for an overview of the interface.
11
 
    """
12
 
 
13
 
    def __init__(self, filename, flag='c'):
14
 
        Shelf.__init__(self, dumbdbm_patched.open(filename, flag))
15
 
 
16
 
    def __getitem__(self, key):
17
 
        compressed = self.dict[key]
18
 
        try:
19
 
            r = zlib.decompress(compressed)
20
 
        except zlib.error:
21
 
            r = compressed
22
 
        return cPickle.loads(r)
23
 
 
24
 
    def __setitem__(self, key, value):
25
 
        s = cPickle.dumps(value,1)
26
 
        self.dict[key] = zlib.compress(s)
27
 
 
28
 
def open(filename, flag='c'):
29
 
    """Open a persistent dictionary for reading and writing.
30
 
 
31
 
    Argument is the filename for the dbm database.
32
 
    See the module's __doc__ string for an overview of the interface.
33
 
    """
34
 
 
35
 
    return DbfilenameShelf(filename, flag)