選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

shelve.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. """Manage shelves of pickled objects.
  2. A "shelf" is a persistent, dictionary-like object. The difference
  3. with dbm databases is that the values (not the keys!) in a shelf can
  4. be essentially arbitrary Python objects -- anything that the "pickle"
  5. module can handle. This includes most class instances, recursive data
  6. types, and objects containing lots of shared sub-objects. The keys
  7. are ordinary strings.
  8. To summarize the interface (key is a string, data is an arbitrary
  9. object):
  10. import shelve
  11. d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
  12. d[key] = data # store data at key (overwrites old data if
  13. # using an existing key)
  14. data = d[key] # retrieve data at key (raise KeyError if no
  15. # such key)
  16. del d[key] # delete data stored at key (raises KeyError
  17. # if no such key)
  18. flag = d.has_key(key) # true if the key exists
  19. list = d.keys() # a list of all existing keys (slow!)
  20. d.close() # close it
  21. Dependent on the implementation, closing a persistent dictionary may
  22. or may not be necessary to flush changes to disk.
  23. """
  24. # Try using cPickle and cStringIO if available.
  25. try:
  26. from cPickle import Pickler, Unpickler
  27. except ImportError:
  28. from pickle import Pickler, Unpickler
  29. try:
  30. from cStringIO import StringIO
  31. except ImportError:
  32. from StringIO import StringIO
  33. __all__ = ["Shelf","BsdDbShelf","DbfilenameShelf","open"]
  34. class Shelf:
  35. """Base class for shelf implementations.
  36. This is initialized with a dictionary-like object.
  37. See the module's __doc__ string for an overview of the interface.
  38. """
  39. def __init__(self, dict):
  40. self.dict = dict
  41. def keys(self):
  42. return self.dict.keys()
  43. def __len__(self):
  44. return len(self.dict)
  45. def has_key(self, key):
  46. return self.dict.has_key(key)
  47. def get(self, key, default=None):
  48. if self.dict.has_key(key):
  49. return self[key]
  50. return default
  51. def __getitem__(self, key):
  52. f = StringIO(self.dict[key])
  53. return Unpickler(f).load()
  54. def __setitem__(self, key, value):
  55. f = StringIO()
  56. p = Pickler(f)
  57. p.dump(value)
  58. self.dict[key] = f.getvalue()
  59. def __delitem__(self, key):
  60. del self.dict[key]
  61. def close(self):
  62. try:
  63. self.dict.close()
  64. except:
  65. pass
  66. self.dict = 0
  67. def __del__(self):
  68. self.close()
  69. def sync(self):
  70. if hasattr(self.dict, 'sync'):
  71. self.dict.sync()
  72. class BsdDbShelf(Shelf):
  73. """Shelf implementation using the "BSD" db interface.
  74. This adds methods first(), next(), previous(), last() and
  75. set_location() that have no counterpart in [g]dbm databases.
  76. The actual database must be opened using one of the "bsddb"
  77. modules "open" routines (i.e. bsddb.hashopen, bsddb.btopen or
  78. bsddb.rnopen) and passed to the constructor.
  79. See the module's __doc__ string for an overview of the interface.
  80. """
  81. def __init__(self, dict):
  82. Shelf.__init__(self, dict)
  83. def set_location(self, key):
  84. (key, value) = self.dict.set_location(key)
  85. f = StringIO(value)
  86. return (key, Unpickler(f).load())
  87. def next(self):
  88. (key, value) = self.dict.next()
  89. f = StringIO(value)
  90. return (key, Unpickler(f).load())
  91. def previous(self):
  92. (key, value) = self.dict.previous()
  93. f = StringIO(value)
  94. return (key, Unpickler(f).load())
  95. def first(self):
  96. (key, value) = self.dict.first()
  97. f = StringIO(value)
  98. return (key, Unpickler(f).load())
  99. def last(self):
  100. (key, value) = self.dict.last()
  101. f = StringIO(value)
  102. return (key, Unpickler(f).load())
  103. class DbfilenameShelf(Shelf):
  104. """Shelf implementation using the "anydbm" generic dbm interface.
  105. This is initialized with the filename for the dbm database.
  106. See the module's __doc__ string for an overview of the interface.
  107. """
  108. def __init__(self, filename, flag='c'):
  109. import anydbm
  110. Shelf.__init__(self, anydbm.open(filename, flag))
  111. def open(filename, flag='c'):
  112. """Open a persistent dictionary for reading and writing.
  113. Argument is the filename for the dbm database.
  114. See the module's __doc__ string for an overview of the interface.
  115. """
  116. return DbfilenameShelf(filename, flag)