[Checkins] SVN: Products.ZCatalog/trunk/src/Products/PluginIndexes/ more pep8, modernize exception raising and avoid bare except statements

Hano Schlichting cvs-admin at zope.org
Sun Apr 8 01:10:35 UTC 2012


Log message for revision 125098:
  more pep8, modernize exception raising and avoid bare except statements
  

Changed:
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/BooleanIndex/BooleanIndex.py
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/DateIndex/DateIndex.py
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/DateRangeIndex.py
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/FieldIndex/FieldIndex.py
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/TopicIndex/FilteredSet.py
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/TopicIndex/TopicIndex.py
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/UUIDIndex/UUIDIndex.py
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/common/ResultList.py
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/common/UnIndex.py
  U   Products.ZCatalog/trunk/src/Products/PluginIndexes/common/util.py

-=-
Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/BooleanIndex/BooleanIndex.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/BooleanIndex/BooleanIndex.py	2012-04-08 01:09:47 UTC (rev 125097)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/BooleanIndex/BooleanIndex.py	2012-04-08 01:10:32 UTC (rev 125098)
@@ -207,9 +207,9 @@
             self._length.change(-1)
         except ConflictError:
             raise
-        except:
+        except Exception:
             LOG.debug('Attempt to unindex nonexistent document'
-                      ' with id %s' % documentId,exc_info=True)
+                      ' with id %s' % documentId, exc_info=True)
 
     def _apply_index(self, request, resultset=None):
         record = parseIndexRequest(request, self.id, self.query_options)

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/DateIndex/DateIndex.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/DateIndex/DateIndex.py	2012-04-08 01:09:47 UTC (rev 125097)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/DateIndex/DateIndex.py	2012-04-08 01:10:32 UTC (rev 125098)
@@ -138,7 +138,7 @@
                         del self._unindex[documentId]
                     except ConflictError:
                         raise
-                    except:
+                    except Exception:
                         LOG.error("Should not happen: ConvertedDate was there,"
                                   " now it's not, for document with id %s" %
                                   documentId)

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/DateRangeIndex.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/DateRangeIndex.py	2012-04-08 01:09:47 UTC (rev 125097)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/DateRangeIndex/DateRangeIndex.py	2012-04-08 01:10:32 UTC (rev 125098)
@@ -10,8 +10,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Date range index.
-"""
 
 import os
 from datetime import datetime
@@ -157,7 +155,7 @@
         self._until_only = IOBTree()
         self._since = IOBTree()
         self._until = IOBTree()
-        self._unindex = IOBTree() # 'datum' will be a tuple of date ints
+        self._unindex = IOBTree()  # 'datum' will be a tuple of date ints
         self._length = Length()
 
     #
@@ -196,7 +194,7 @@
         datum = (since, until)
 
         old_datum = self._unindex.get(documentId, None)
-        if datum == old_datum: # No change?  bail out!
+        if datum == old_datum:  # No change?  bail out!
             return 0
 
         if old_datum is not None:
@@ -396,9 +394,9 @@
             return value
         if isinstance(value, (str, datetime)):
             dt_obj = DateTime(value)
-            value = dt_obj.millis() / 1000 / 60 # flatten to minutes
+            value = dt_obj.millis() / 1000 / 60  # flatten to minutes
         elif isinstance(value, DateTime):
-            value = value.millis() / 1000 / 60 # flatten to minutes
+            value = value.millis() / 1000 / 60  # flatten to minutes
         if value > MAX32 or value < -MAX32:
             # t_val must be integer fitting in the 32bit range
             raise OverflowError('%s is not within the range of dates allowed'

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/FieldIndex/FieldIndex.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/FieldIndex/FieldIndex.py	2012-04-08 01:09:47 UTC (rev 125097)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/FieldIndex/FieldIndex.py	2012-04-08 01:10:32 UTC (rev 125098)
@@ -10,8 +10,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Simple column indices.
-"""
 
 from App.special_dtml import DTMLFile
 
@@ -19,12 +17,11 @@
 
 
 class FieldIndex(UnIndex):
-
     """Index for simple fields.
     """
-    meta_type="FieldIndex"
+    meta_type = "FieldIndex"
 
-    manage_options= (
+    manage_options = (
         {'label': 'Settings', 'action': 'manage_main'},
         {'label': 'Browse', 'action': 'manage_browse'},
     )
@@ -35,9 +32,9 @@
     manage_main._setName('manage_main')
     manage_browse = DTMLFile('../dtml/browseIndex', globals())
 
-
 manage_addFieldIndexForm = DTMLFile('dtml/addFieldIndex', globals())
 
+
 def manage_addFieldIndex(self, id, extra=None,
                 REQUEST=None, RESPONSE=None, URL3=None):
     """Add a field index"""

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/TopicIndex/FilteredSet.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/TopicIndex/FilteredSet.py	2012-04-08 01:09:47 UTC (rev 125097)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/TopicIndex/FilteredSet.py	2012-04-08 01:10:32 UTC (rev 125098)
@@ -10,8 +10,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Filtered set.
-"""
 
 from logging import getLogger
 import sys
@@ -33,19 +31,21 @@
     implements(IFilteredSet)
 
     def __init__(self, id, expr):
-        self.id   = id
+        self.id = id
         self.expr = expr
         self.clear()
 
     def clear(self):
-        self.ids  = IITreeSet()
+        self.ids = IITreeSet()
 
     def index_object(self, documentId, obj):
-        raise RuntimeError,'index_object not defined'
+        raise RuntimeError('index_object not defined')
 
-    def unindex_object(self,documentId):
-        try: self.ids.remove(documentId)
-        except KeyError: pass
+    def unindex_object(self, documentId):
+        try:
+            self.ids.remove(documentId)
+        except KeyError:
+            pass
 
     def getId(self):
         return self.id
@@ -66,7 +66,7 @@
         self.expr = expr
 
     def __repr__(self):
-        return '%s: (%s) %s' % (self.id,self.expr,map(None,self.ids))
+        return '%s: (%s) %s' % (self.id, self.expr, map(None, self.ids))
 
     __str__ = __repr__
 
@@ -86,16 +86,14 @@
                     pass
         except ConflictError:
             raise
-        except:
+        except Exception:
             LOG.warn('eval() failed Object: %s, expr: %s' %\
-                     (o.getId(),self.expr), exc_info=sys.exc_info())
+                     (o.getId(), self.expr), exc_info=sys.exc_info())
 
 
 def factory(f_id, f_type, expr):
     """ factory function for FilteredSets """
-
-    if f_type=='PythonFilteredSet':
+    if f_type == 'PythonFilteredSet':
         return PythonFilteredSet(f_id, expr)
-
     else:
-        raise TypeError,'unknown type for FilteredSets: %s' % f_type
+        raise TypeError('unknown type for FilteredSets: %s' % f_type)

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/TopicIndex/TopicIndex.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/TopicIndex/TopicIndex.py	2012-04-08 01:09:47 UTC (rev 125097)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/TopicIndex/TopicIndex.py	2012-04-08 01:10:32 UTC (rev 125098)
@@ -10,8 +10,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Topic index.
-"""
 
 from logging import getLogger
 
@@ -43,17 +41,17 @@
     """
     implements(ITopicIndex, IPluggableIndex)
 
-    meta_type="TopicIndex"
+    meta_type = "TopicIndex"
     query_options = ('query', 'operator')
 
-    manage_options= (
+    manage_options = (
         {'label': 'FilteredSets', 'action': 'manage_main'},
     )
 
-    def __init__(self,id,caller=None):
+    def __init__(self, id, caller=None):
         self.id = id
-        self.filteredSets  = OOBTree()
-        self.operators = ('or','and')
+        self.filteredSets = OOBTree()
+        self.operators = ('or', 'and')
         self.defaultOperator = 'or'
 
     def getId(self):
@@ -63,15 +61,14 @@
         for fs in self.filteredSets.values():
             fs.clear()
 
-    def index_object(self, docid, obj ,threshold=100):
+    def index_object(self, docid, obj, threshold=100):
         """ hook for (Z)Catalog """
         for fid, filteredSet in self.filteredSets.items():
-            filteredSet.index_object(docid,obj)
+            filteredSet.index_object(docid, obj)
         return 1
 
-    def unindex_object(self,docid):
+    def unindex_object(self, docid):
         """ hook for (Z)Catalog """
-
         for fs in self.filteredSets.values():
             try:
                 fs.unindex_object(docid)
@@ -88,7 +85,7 @@
         """Return the size of the index in terms of distinct values."""
         return "n/a"
 
-    def search(self,filter_id):
+    def search(self, filter_id):
         if self.filteredSets.has_key(filter_id):
             return self.filteredSets[filter_id].getIds()
 
@@ -101,24 +98,26 @@
             return None
 
         operator = record.get('operator', self.defaultOperator).lower()
-        if operator == 'or':  set_func = union
-        else: set_func = intersection
+        if operator == 'or':
+            set_func = union
+        else:
+            set_func = intersection
 
         res = None
         for filter_id in record.keys:
             rows = self.search(filter_id)
-            res = set_func(res,rows)
+            res = set_func(res, rows)
 
         if res:
             return res, (self.id,)
         else:
             return IITreeSet(), (self.id,)
 
-    def uniqueValues(self,name=None, withLength=0):
+    def uniqueValues(self, name=None, withLength=0):
         """ needed to be consistent with the interface """
         return self.filteredSets.keys()
 
-    def getEntryForObject(self,docid, default=_marker):
+    def getEntryForObject(self, docid, default=_marker):
         """ Takes a document ID and returns all the information we have
             on that specific object.
         """
@@ -127,8 +126,8 @@
     def addFilteredSet(self, filter_id, typeFilteredSet, expr):
         # Add a FilteredSet object.
         if self.filteredSets.has_key(filter_id):
-            raise KeyError,\
-                'A FilteredSet with this name already exists: %s' % filter_id
+            raise KeyError(
+                'A FilteredSet with this name already exists: %s' % filter_id)
         self.filteredSets[filter_id] = factory(filter_id,
                                                typeFilteredSet,
                                                expr,
@@ -137,73 +136,75 @@
     def delFilteredSet(self, filter_id):
         # Delete the FilteredSet object specified by 'filter_id'.
         if not self.filteredSets.has_key(filter_id):
-            raise KeyError,\
-                'no such FilteredSet:  %s' % filter_id
+            raise KeyError(
+                'no such FilteredSet:  %s' % filter_id)
         del self.filteredSets[filter_id]
 
     def clearFilteredSet(self, filter_id):
         # Clear the FilteredSet object specified by 'filter_id'.
         if not self.filteredSets.has_key(filter_id):
-            raise KeyError,\
-                'no such FilteredSet:  %s' % filter_id
+            raise KeyError(
+                'no such FilteredSet:  %s' % filter_id)
         self.filteredSets[filter_id].clear()
 
     def manage_addFilteredSet(self, filter_id, typeFilteredSet, expr, URL1, \
-            REQUEST=None,RESPONSE=None):
+            REQUEST=None, RESPONSE=None):
         """ add a new filtered set """
 
-        if len(filter_id) == 0: raise RuntimeError,'Length of ID too short'
-        if len(expr) == 0: raise RuntimeError,'Length of expression too short'
+        if len(filter_id) == 0:
+            raise RuntimeError('Length of ID too short')
+        if len(expr) == 0:
+            raise RuntimeError('Length of expression too short')
 
         self.addFilteredSet(filter_id, typeFilteredSet, expr)
 
         if RESPONSE:
-            RESPONSE.redirect(URL1+'/manage_workspace?'
+            RESPONSE.redirect(URL1 + '/manage_workspace?'
             'manage_tabs_message=FilteredSet%20added')
 
-    def manage_delFilteredSet(self, filter_ids=[], URL1=None, \
-            REQUEST=None,RESPONSE=None):
+    def manage_delFilteredSet(self, filter_ids=[], URL1=None,
+            REQUEST=None, RESPONSE=None):
         """ delete a list of FilteredSets"""
 
         for filter_id in filter_ids:
             self.delFilteredSet(filter_id)
 
         if RESPONSE:
-            RESPONSE.redirect(URL1+'/manage_workspace?'
+            RESPONSE.redirect(URL1 + '/manage_workspace?'
             'manage_tabs_message=FilteredSet(s)%20deleted')
 
-    def manage_saveFilteredSet(self,filter_id, expr, URL1=None,\
-            REQUEST=None,RESPONSE=None):
+    def manage_saveFilteredSet(self, filter_id, expr, URL1=None,
+            REQUEST=None, RESPONSE=None):
         """ save expression for a FilteredSet """
 
         self.filteredSets[filter_id].setExpression(expr)
 
         if RESPONSE:
-            RESPONSE.redirect(URL1+'/manage_workspace?'
+            RESPONSE.redirect(URL1 + '/manage_workspace?'
             'manage_tabs_message=FilteredSet(s)%20updated')
 
     def getIndexSourceNames(self):
         """ return names of indexed attributes """
-        return ('n/a',)
+        return ('n/a', )
 
-    def manage_clearFilteredSet(self, filter_ids=[], URL1=None, \
-            REQUEST=None,RESPONSE=None):
+    def manage_clearFilteredSet(self, filter_ids=[], URL1=None,
+            REQUEST=None, RESPONSE=None):
         """  clear a list of FilteredSets"""
 
         for filter_id in filter_ids:
             self.clearFilteredSet(filter_id)
 
         if RESPONSE:
-            RESPONSE.redirect(URL1+'/manage_workspace?'
+            RESPONSE.redirect(URL1 + '/manage_workspace?'
              'manage_tabs_message=FilteredSet(s)%20cleared')
 
-    manage = manage_main = DTMLFile('dtml/manageTopicIndex',globals())
+    manage = manage_main = DTMLFile('dtml/manageTopicIndex', globals())
     manage_main._setName('manage_main')
-    editFilteredSet = DTMLFile('dtml/editFilteredSet',globals())
+    editFilteredSet = DTMLFile('dtml/editFilteredSet', globals())
 
-
 manage_addTopicIndexForm = DTMLFile('dtml/addTopicIndex', globals())
 
+
 def manage_addTopicIndex(self, id, REQUEST=None, RESPONSE=None, URL3=None):
     """Add a TopicIndex"""
     return self.manage_addIndex(id, 'TopicIndex', extra=None, \

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/UUIDIndex/UUIDIndex.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/UUIDIndex/UUIDIndex.py	2012-04-08 01:09:47 UTC (rev 125097)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/UUIDIndex/UUIDIndex.py	2012-04-08 01:10:32 UTC (rev 125098)
@@ -36,9 +36,9 @@
     For each datum only one documentId can exist.
     """
 
-    meta_type= "UUIDIndex"
+    meta_type = "UUIDIndex"
 
-    manage_options= (
+    manage_options = (
         {'label': 'Settings', 'action': 'manage_main'},
         {'label': 'Browse', 'action': 'manage_browse'},
     )
@@ -108,7 +108,6 @@
             return _marker
         return super(UUIDIndex, self)._get_object_datum(obj, attr)
 
-
 manage_addUUIDIndexForm = DTMLFile('dtml/addUUIDIndex', globals())
 
 

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/common/ResultList.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/common/ResultList.py	2012-04-08 01:09:47 UTC (rev 125097)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/common/ResultList.py	2012-04-08 01:10:32 UTC (rev 125098)
@@ -18,13 +18,14 @@
 from BTrees.OOBTree import OOSet
 from BTrees.OOBTree import union
 
+
 class ResultList:
 
     def __init__(self, d, words, index):
         self._index = index
 
         if type(words) is not OOSet:
-            words=OOSet(words)
+            words = OOSet(words)
         self._words = words
 
         if (type(d) is tuple):
@@ -32,11 +33,13 @@
         elif type(d) is not IIBucket:
             d = IIBucket(d)
 
-        self._dict=d
-        self.__getitem__=d.__getitem__
-        try: self.__nonzero__=d.__nonzero__
-        except: pass
-        self.get=d.get
+        self._dict = d
+        self.__getitem__ = d.__getitem__
+        try:
+            self.__nonzero__ = d.__nonzero__
+        except Exception:
+            pass
+        self.get = d.get
 
     def __nonzero__(self):
         return not not self._dict
@@ -73,7 +76,6 @@
             union(self._words, x._words),
             self._index,
             )
-        # return self.__class__(result, self._words+x._words, self._index)
 
     def near(self, x):
         result = IIBucket()
@@ -82,20 +84,23 @@
         xhas = xdict.has_key
         positions = self._index.positions
         for id, score in dict.items():
-            if not xhas(id): continue
-            p=(map(lambda i: (i,0), positions(id,self._words))+
-               map(lambda i: (i,1), positions(id,x._words)))
+            if not xhas(id):
+                continue
+            p = (map(lambda i: (i, 0), positions(id, self._words)) +
+                 map(lambda i: (i, 1), positions(id, x._words)))
             p.sort()
             d = lp = 9999
             li = None
             lsrc = None
-            for i,src in p:
+            for i, src in p:
                 if i is not li and src is not lsrc and li is not None:
-                    d = min(d,i-li)
+                    d = min(d, i - li)
                 li = i
                 lsrc = src
-            if d==lp: score = min(score,xdict[id]) # synonyms
-            else: score = (score+xdict[id])/d
+            if d == lp:
+                score = min(score, xdict[id])  # synonyms
+            else:
+                score = (score + xdict[id]) / d
             result[id] = score
 
         return self.__class__(

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/common/UnIndex.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/common/UnIndex.py	2012-04-08 01:09:47 UTC (rev 125097)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/common/UnIndex.py	2012-04-08 01:10:32 UTC (rev 125098)
@@ -10,8 +10,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Base for bi-directional indexes.
-"""
 
 from cgi import escape
 from logging import getLogger
@@ -162,10 +160,8 @@
                 if not indexRow:
                     del self._index[entry]
                     self._length.change(-1)
-
             except ConflictError:
                 raise
-
             except AttributeError:
                 # index row is an int
                 try:
@@ -173,13 +169,12 @@
                 except KeyError:
                     # XXX swallow KeyError because it was probably
                     # removed and then _length AttributeError raised
-                    pass 
+                    pass
                 if isinstance(self.__len__, Length):
                     self._length = self.__len__
-                    del self.__len__ 
+                    del self.__len__
                 self._length.change(-1)
-
-            except:
+            except Exception:
                 LOG.error('%s: unindex_object could not remove '
                           'documentId %s from index %s.  This '
                           'should not happen.' % (self.__class__.__name__,
@@ -244,9 +239,9 @@
                         del self._unindex[documentId]
                     except ConflictError:
                         raise
-                    except:
-                        LOG.error('Should not happen: oldDatum was there, now its not,'
-                                  'for document with id %s' % documentId)
+                    except Exception:
+                        LOG.error('Should not happen: oldDatum was there, '
+                            'now its not, for document: %s' % documentId)
 
             if datum is not _marker:
                 self.insertForwardIndexEntry(datum, documentId)
@@ -290,9 +285,9 @@
             del self._unindex[documentId]
         except ConflictError:
             raise
-        except:
+        except Exception:
             LOG.debug('Attempt to unindex nonexistent document'
-                      ' with id %s' % documentId,exc_info=True)
+                      ' with id %s' % documentId, exc_info=True)
 
     def _apply_not(self, not_parm, resultset=None):
         index = self._index

Modified: Products.ZCatalog/trunk/src/Products/PluginIndexes/common/util.py
===================================================================
--- Products.ZCatalog/trunk/src/Products/PluginIndexes/common/util.py	2012-04-08 01:09:47 UTC (rev 125097)
+++ Products.ZCatalog/trunk/src/Products/PluginIndexes/common/util.py	2012-04-08 01:10:32 UTC (rev 125098)
@@ -10,8 +10,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""PluginIndexes utils.
-"""
 
 from types import InstanceType
 



More information about the checkins mailing list