[Spambayes-checkins] spambayes/spambayes msgs.py,1.2,1.3

Tony Meyer anadelonbrin at users.sourceforge.net
Mon Oct 18 07:26:40 CEST 2004


Update of /cvsroot/spambayes/spambayes/spambayes
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2639/spambayes

Modified Files:
	msgs.py 
Log Message:
I keep running into this, and as far as I can tell, it doesn't hurt to add this, so:

Make msgs.Msg objects picklable.

Index: msgs.py
===================================================================
RCS file: /cvsroot/spambayes/spambayes/spambayes/msgs.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** msgs.py	14 Jan 2003 05:38:20 -0000	1.2
--- msgs.py	18 Oct 2004 05:26:37 -0000	1.3
***************
*** 35,38 ****
--- 35,44 ----
          return self.guts
  
+     # We have defined __slots__, so need these to be able to be pickled.
+     def __getstate__(self):
+         return self.tag, self.guts
+     def __setstate__(self, s):
+         self.tag, self.guts = s
+ 
  # The iterator yields a stream of Msg objects, taken from a list of
  # directories.
***************
*** 40,51 ****
      __slots__ = 'tag', 'directories', 'keep'
  
!     def __init__(self, tag, directories, keep=None):
          self.tag = tag
          self.directories = directories
          self.keep = keep
  
      def __str__(self):
          return self.tag
  
      def produce(self):
          if self.keep is None:
--- 46,70 ----
      __slots__ = 'tag', 'directories', 'keep'
  
!     def __init__(self, tag, directories, keep=None, use=None):
          self.tag = tag
          self.directories = directories
          self.keep = keep
+         self.use = use
  
      def __str__(self):
          return self.tag
  
+     def __len__(self):
+         """Number of messages in the stream, which is the number
+         of files in the directory."""
+         files = []
+         for directory in self.directories:
+             files.extend(os.listdir(directory))
+         if self.keep is not None:
+             del files[self.keep:]
+         elif self.use is not None:
+             files = files[self.use[0]:self.use[1]]
+         return len(files)
+ 
      def produce(self):
          if self.keep is None:
***************
*** 61,65 ****
              random.seed(hash(max(all)) ^ SEED) # reproducible across calls
              random.shuffle(all)
!             del all[self.keep:]
              all.sort()  # seems to speed access on Win98!
              for fname in all:
--- 80,87 ----
              random.seed(hash(max(all)) ^ SEED) # reproducible across calls
              random.shuffle(all)
!             if self.use is None:
!                 del all[self.keep:]
!             else:
!                 all = all[self.use[0]:self.use[1]]
              all.sort()  # seems to speed access on Win98!
              for fname in all:



More information about the Spambayes-checkins mailing list