[Spambayes-checkins] SF.net SVN: spambayes:[3215] trunk/spambayes/spambayes/storage.py

montanaro at users.sourceforge.net montanaro at users.sourceforge.net
Tue Nov 25 05:09:59 CET 2008


Revision: 3215
          http://spambayes.svn.sourceforge.net/spambayes/?rev=3215&view=rev
Author:   montanaro
Date:     2008-11-25 04:09:59 +0000 (Tue, 25 Nov 2008)

Log Message:
-----------
use safepickle functions
pylint nits

Modified Paths:
--------------
    trunk/spambayes/spambayes/storage.py

Modified: trunk/spambayes/spambayes/storage.py
===================================================================
--- trunk/spambayes/spambayes/storage.py	2008-11-25 04:01:34 UTC (rev 3214)
+++ trunk/spambayes/spambayes/storage.py	2008-11-25 04:09:59 UTC (rev 3215)
@@ -50,8 +50,8 @@
 ### situations prints to sys.stdout will garble the message (e.g., in
 ### hammiefilter).
 
-__author__ = "Neale Pickett <neale at woozle.org>, \
-Tim Stone <tim at fourstonesExpressions.com>"
+__author__ = ("Neale Pickett <neale at woozle.org>,"
+              "Tim Stone <tim at fourstonesExpressions.com>")
 __credits__ = "All the spambayes contributors."
 
 try:
@@ -69,11 +69,11 @@
 import tempfile
 from spambayes import classifier
 from spambayes.Options import options, get_pathname_option
-import cPickle as pickle
 import errno
 import shelve
 from spambayes import cdb
 from spambayes import dbmstorage
+from spambayes.safepickle import pickle_write
 
 # Make shelve use binary pickles by default.
 oldShelvePickler = shelve.Pickler
@@ -85,36 +85,6 @@
 NO_UPDATEPROBS = False   # Probabilities will not be autoupdated with training
 UPDATEPROBS = True       # Probabilities will be autoupdated with training
 
-def safe_pickle(filename, value, protocol=0):
-    '''Store value as a pickle without creating corruption'''
-
-    # Be as defensive as possible.  Always keep a safe copy.
-    tmp = filename + '.tmp'
-    fp = None
-    try: 
-        fp = open(tmp, 'wb') 
-        pickle.dump(value, fp, protocol) 
-        fp.close() 
-    except IOError, e: 
-        if options["globals", "verbose"]: 
-            print >> sys.stderr, 'Failed update: ' + str(e)
-        if fp is not None: 
-            os.remove(tmp) 
-        raise
-    try:
-        # With *nix we can just rename, and (as long as permissions
-        # are correct) the old file will vanish.  With win32, this
-        # won't work - the Python help says that there may not be
-        # a way to do an atomic replace, so we rename the old one,
-        # put the new one there, and then delete the old one.  If
-        # something goes wrong, there is at least a copy of the old
-        # one.
-        os.rename(tmp, filename)
-    except OSError:
-        os.rename(filename, filename + '.bak')
-        os.rename(tmp, filename)
-        os.remove(filename + '.bak')
-    
 class PickledClassifier(classifier.Classifier):
     '''Classifier object persisted in a pickle'''
 
@@ -136,16 +106,12 @@
         # tempbayes object is reclaimed when load() returns.
 
         if options["globals", "verbose"]:
-            print >> sys.stderr, 'Loading state from',self.db_name,'pickle'
+            print >> sys.stderr, 'Loading state from', self.db_name, 'pickle'
 
-        tempbayes = None
         try:
-            fp = open(self.db_name, 'rb')
-        except IOError, e:
-            if e.errno != errno.ENOENT: raise
-        else:
-            tempbayes = pickle.load(fp)
-            fp.close()
+            tempbayes = pickle_read(self.db_name)
+        except:
+            tempbayes = None
 
         if tempbayes:
             # Copy state from tempbayes.  The use of our base-class
@@ -169,9 +135,9 @@
         '''Store self as a pickle'''
 
         if options["globals", "verbose"]:
-            print >> sys.stderr, 'Persisting',self.db_name,'as a pickle'
+            print >> sys.stderr, 'Persisting', self.db_name, 'as a pickle'
 
-        safe_pickle(self.db_name, self, PICKLE_TYPE)
+        pickle_write(self.db_name, self, PICKLE_TYPE)
 
     def close(self):
         # we keep no resources open - nothing to do
@@ -198,7 +164,8 @@
     def close(self):
         # Close our underlying database.  Better not assume all databases
         # have close functions!
-        def noop(): pass
+        def noop():
+            pass
         getattr(self.db, "close", noop)()
         getattr(self.dbm, "close", noop)()
         # should not be a need to drop the 'dbm' or 'db' attributes.
@@ -210,13 +177,13 @@
         if hasattr(self, "dbm"):
             del self.dbm
         if options["globals", "verbose"]:
-            print >> sys.stderr, 'Closed',self.db_name,'database'
+            print >> sys.stderr, 'Closed', self.db_name, 'database'
 
     def load(self):
         '''Load state from database'''
 
         if options["globals", "verbose"]:
-            print >> sys.stderr, 'Loading state from',self.db_name,'database'
+            print >> sys.stderr, 'Loading state from', self.db_name, 'database'
 
         self.dbm = dbmstorage.open(self.db_name, self.mode)
         self.db = shelve.Shelf(self.dbm)
@@ -244,7 +211,8 @@
         '''Place state into persistent store'''
 
         if options["globals", "verbose"]:
-            print >> sys.stderr, 'Persisting',self.db_name,'state in database'
+            print >> sys.stderr, 'Persisting', self.db_name,
+            print >> sys.stderr, 'state in database'
 
         # Iterate over our changed word list.
         # This is *not* thread-safe - another thread changing our
@@ -471,7 +439,7 @@
     def fetchall(self, c):
         return c.dictfetchall()
 
-    def commit(self, c):
+    def commit(self, _c):
         self.db.commit()
 
     def load(self):
@@ -480,7 +448,7 @@
         import psycopg
 
         if options["globals", "verbose"]:
-            print >> sys.stderr, 'Loading state from',self.db_name,'database'
+            print >> sys.stderr, 'Loading state from', self.db_name, 'database'
 
         self.db = psycopg.connect('dbname=' + self.db_name)
 
@@ -545,7 +513,7 @@
     def fetchall(self, c):
         return c.fetchall()
 
-    def commit(self, c):
+    def commit(self, _c):
         self.db.commit()
 
     def load(self):
@@ -554,7 +522,7 @@
         import MySQLdb
 
         if options["globals", "verbose"]:
-            print >> sys.stderr, 'Loading state from',self.db_name,'database'
+            print >> sys.stderr, 'Loading state from', self.db_name, 'database'
 
         params = {
           'host': self.host, 'db': self.db_name,
@@ -724,12 +692,11 @@
             object.__setattr__(self, att, value)
 
     def create_storage(self):
-        import ZODB
         from ZODB.FileStorage import FileStorage
         try:
             self.storage = FileStorage(self.db_filename,
                                        read_only=self.mode=='r')
-        except IOError, msg:
+        except IOError:
             print >> sys.stderr, ("Could not create FileStorage from",
                                   self.db_filename)
             raise
@@ -768,7 +735,6 @@
     def store(self):
         '''Place state into persistent store'''
         try:
-            import ZODB
             import ZODB.Transaction
         except ImportError:
             import transaction
@@ -971,7 +937,7 @@
         '''Untrain the database with the message'''
 
         if options["globals", "verbose"]:
-            print >> sys.stderr, 'untraining with',message.key()
+            print >> sys.stderr, 'untraining with', message.key()
 
         self.bayes.unlearn(message.tokenize(), self.is_spam)
 #                           self.updateprobs)
@@ -1005,6 +971,7 @@
 
 class NoSuchClassifierError(Exception):
     def __init__(self, invalid_name):
+        Exception.__init__(self, invalid_name)
         self.invalid_name = invalid_name
     def __str__(self):
         return repr(self.invalid_name)
@@ -1088,7 +1055,7 @@
         try:
             unused, unused, is_path = _storage_types[typ]
         except KeyError:
-            raise NoSuchClassifierError(db_type)
+            raise NoSuchClassifierError(typ)
         if is_path:
             nm = get_pathname_option(*default_name)
         else:
@@ -1142,7 +1109,7 @@
     try:
         os.mkdir(dirname)
         if options["globals", "verbose"]:
-            print >>sys.stderr, "Creating directory", dirname
+            print >> sys.stderr, "Creating directory", dirname
     except OSError, e:
         if e.errno != errno.EEXIST:
             raise


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


More information about the Spambayes-checkins mailing list