[Spambayes-checkins] spambayes dbdict.py,1.1.2.3,1.1.2.4

Tim Stone timstone4@users.sourceforge.net
Fri Nov 22 00:21:30 2002


Update of /cvsroot/spambayes/spambayes
In directory sc8-pr-cvs1:/tmp/cvs-serv21550

Modified Files:
      Tag: hammie-playground
	dbdict.py 
Log Message:
Modified to include wclass operand on constructor, which is used to
create special 'W' pickle strings when an instance of that class is
pickled, and to unpickle to the same class when 'W' pickle strings are
encountered.

Index: dbdict.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/dbdict.py,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -C2 -d -r1.1.2.3 -r1.1.2.4
*** dbdict.py	21 Nov 2002 02:58:56 -0000	1.1.2.3
--- dbdict.py	22 Nov 2002 00:21:28 -0000	1.1.2.4
***************
*** 4,13 ****
  
  Classes:
!     DBDict - wraps an anydbm file
!     LSDBDict - adds load/store/restore semantic to DBDict
  
  Abstract:
!     DBDict class wraps an anydbm file with a reasonably complete set
      of dictionary access methods.  DBDicts can be iterated like a dictionary.
  
      DBDict accepts an iterskip operand on the constructor.  This is a tuple
--- 4,19 ----
  
  Classes:
!     DBDict - wraps a dbhash file
  
  Abstract:
!     DBDict class wraps a dbhash file with a reasonably complete set
      of dictionary access methods.  DBDicts can be iterated like a dictionary.
+     
+     The constructor accepts a class name which is used specifically to
+     to pickle/unpickle an instance of that class.  When an instance of
+     that class is being pickled, the pickler (actually __getstate__) prepends
+     a 'W' to the pickled string, and when the unpickler (really __setstate__)
+     encounters that 'W', it constructs that class (with no constructor
+     arguments) and executes __setstate__ on the constructed instance.
  
      DBDict accepts an iterskip operand on the constructor.  This is a tuple
***************
*** 33,44 ****
          countme wakka
  
-     LSDBDict class addes load/store/restore functions to DBDict.  It does this
-     by creating a working copy of the dbm file, and using that for all
-     working access.  When the store() method is called, the working dbm hash
-     is closed, copied to the real copy, then reopened, in effect
-     committing any changes.  When restore() is called, the working copy
-     is closed, replaced with the real copy, then reopened.  Store and restore
-     methods are disallowed for readonly (mode MODE_READONLY) LSDBDicts.
- 
  To Do:
      """
--- 39,42 ----
***************
*** 53,57 ****
                 all the spambayes contributors."
  from __future__ import generators
! import dbhash
  try:
      import cPickle as pickle
--- 51,55 ----
                 all the spambayes contributors."
  from __future__ import generators
! 
  try:
      import cPickle as pickle
***************
*** 59,62 ****
--- 57,61 ----
      import pickle
  
+ import dbhash
  import errno
  import copy
***************
*** 81,85 ****
      like .keys() still list everything.  For instance:
  
!     >>> d = DBDict('goober.db', 'c', ('skipme', 'skipmetoo'))
      >>> d['skipme'] = 'booga'
      >>> d['countme'] = 'wakka'
--- 80,84 ----
      like .keys() still list everything.  For instance:
  
!     >>> d = DBDict('goober.db', MODE_CREATE, ('skipme', 'skipmetoo'))
      >>> d['skipme'] = 'booga'
      >>> d['countme'] = 'wakka'
***************
*** 92,98 ****
      """
  
!     def __init__(self, dbname, mode, iterskip=()):
          self.hash = dbhash.open(dbname, mode)
!         self.iterskip = iterskip
  
      def __getitem__(self, key):
--- 91,121 ----
      """
  
!     def __init__(self, dbname, mode, wclass, iterskip=()):
          self.hash = dbhash.open(dbname, mode)
!         if not iterskip:
!             self.iterskip = iterskip
!         else:
!             self.iterskip = ()
!         self.wclass=wclass
! 
!     def __getitem__(self, key):
!         v = self.hash[key]
!         if v[0] == 'W':
!             val = pickle.loads(v[1:])
!             # We could be sneaky, like pickle.Unpickler.load_inst,
!             # but I think that's overly confusing.
!             obj = self.wclass()
!             obj.__setstate__(val)
!             return obj
!         else:
!             return pickle.loads(v)
! 
!     def __setitem__(self, key, val):
!         if isinstance(val, self.wclass):
!             val = val.__getstate__()
!             v = 'W' + pickle.dumps(val, 1)
!         else:
!             v = pickle.dumps(val, 1)
!         self.hash[key] = v
  
      def __getitem__(self, key):





More information about the Spambayes-checkins mailing list