[Zope-Checkins] CVS: Zope2 - UnTextIndex.py:1.33.2.17

fred@digicool.com fred@digicool.com
Tue, 5 Jun 2001 17:24:26 -0400 (EDT)


Update of /cvs-repository/Zope2/lib/python/SearchIndex
In directory korak.digicool.com:/tmp/cvs-serv9343/lib/python/SearchIndex

Modified Files:
      Tag: zope-2_3-branch
	UnTextIndex.py 
Log Message:

Import the "re" module -- this module could not be fully imported without
this, indicating that someone did not test the last change to this on the
2.3.x branch.  The existing tests were sufficient to turn this up!

Reduce the number of lookups for builtins and calls to type() by converting
to use isinstance().



--- Updated File UnTextIndex.py in package Zope2 --
--- UnTextIndex.py	2001/06/05 13:02:24	1.33.2.16
+++ UnTextIndex.py	2001/06/05 21:24:26	1.33.2.17
@@ -96,6 +96,7 @@
 
 import string, regex, regsub, ts_regex
 import operator
+import re
 
 from Globals import Persistent
 from Acquisition import Implicit
@@ -180,7 +181,7 @@
         Zope.  I don't think indexes were ever intended to participate
         in this way, but I don't see too much of a problem with it."""
 
-        if type(vocab_id) is not StringType:
+        if not isinstance(vocab_id, StringType):
             vocab = vocab_id # we already havd the lexicon
             return vocab
         else:
@@ -203,11 +204,11 @@
 
     def _convertBTrees(self, threshold=200):
 
-        if type(self._lexicon) is type(''):
+        if isinstance(self._lexicon, StringType):
             # Turn the name reference into a hard reference. 
             self._lexicon=self.getLexicon(self._lexicon)
 
-        if type(self._index) is IOBTree: return
+        if isinstance(self._index, IOBTree): return
 
         from BTrees.convert import convert
 
@@ -215,9 +216,11 @@
         self._index=IOBTree()
 
         def convertScores(scores,
-                          type=type, TupleType=TupleType, IIBTree=IIBTree
+                          isinstance=isinstance,
+                          TupleType=TupleType, IIBTree=IIBTree
                           ):
-            if type(scores) is not TupleType and type(scores) is not IIBTree():
+            if not (isinstance(scores, TupleType)
+                    or isinstance(scores, IIBTree)):
                 scores=IIBTree(scores)
             return scores
                 
@@ -228,13 +231,13 @@
         self._unindex=IOBTree()
         convert(_unindex, self._unindex, threshold)
 
-    def histogram(self, type=type, TupleType=type(())):
+    def histogram(self, isinstance=isinstance, TupleType=TupleType):
         """Return a mapping which provides a histogram of the number of
         elements found at each point in the index."""
 
         histogram = IIBucket()
         for (key, value) in self._index.items():
-            if type(value) is TupleType: entry=1
+            if isinstance(value, TupleType): entry=1
             else: entry = len(value)
             histogram[entry] = histogram.get(entry, 0) + 1
 
@@ -271,7 +274,7 @@
         indexRow = index.get(entry, None)
 
         if indexRow is not None:
-            if type(indexRow) is TupleType:
+            if isinstance(indexRow, TupleType):
                 # Tuples are only used for rows which have only
                 # a single entry.  Since we now need more, we'll
                 # promote it to a mapping object (dictionary).
@@ -291,8 +294,8 @@
             else:
                 if indexRow.get(documentId, -1) != score:
                     # score changed (or new entry)
-                    
-                    if type(indexRow) is DictType:
+
+                    if isinstance(indexRow, DictType):
                         indexRow[documentId] = score
                         if len(indexRow) > 3:
                             # Big enough to give it's own database record
@@ -403,13 +406,13 @@
                     'unindex_object tried to unindex nonexistent'
                     ' document, wid  %s, %s' % (i,wid))
                 continue
-            if type(widScores) is TupleType:
+            if isinstance(widScores, TupleType):
                 del index[wid]
             else:
                 try:
                     del widScores[i]
                     if widScores:
-                        if type(widScores) is DictType:
+                        if isinstance(widScores, DictType):
                             if len(widScores) == 1:
                                 # convert to tuple
                                 widScores = widScores.items()[0]
@@ -427,8 +430,8 @@
         Note that this differentiates between being passed an Integer
         and a String.  Strings are looked up in the lexicon, whereas
         Integers are assumed to be resolved word ids. """
-        
-        if type(word) is IntType:
+
+        if isinstance(word, IntType):
             # We have a word ID
             result = self._index.get(word, {})
             return ResultList(result, (word,), self)
@@ -498,7 +501,7 @@
             op=string.lower(str(request['textindex_operator']))
             query_operator = operators.get(op, query_operator)
 
-        if type(keys) is StringType:
+        if isinstance(keys, StringType):
             if not keys or not string.strip(keys):
                 return None
             keys = [keys]
@@ -610,7 +613,7 @@
         # we'll recursively evaluate, other wise it's nothing for us
         # to evaluate, and we just get the results and return them.
         if (len(query) == 1):
-            if (type(query[0]) is ListType):
+            if isinstance(query[0], ListType):
                 return self.evaluate(query[0])
 
             return self[query[0]]       # __getitem__
@@ -684,14 +687,14 @@
     i = 0
     isop = operator_dict.has_key
     while (i < len(q)):
-        if (type(q[i]) is ListType): q[i] = parse2(q[i], default_operator)
+        if isinstance(q[i], ListType): q[i] = parse2(q[i], default_operator)
 
         # every other item, starting with the first, should be an operand
         if ((i % 2) != 0):
             # This word should be an operator; if it is not, splice in
             # the default operator.
             
-            if type(q[i]) is not ListType and isop(q[i]):
+            if not isinstance(q[i], ListType) and isop(q[i]):
                 q[i] = operator_dict[q[i]]
             else: q[i : i] = [ default_operator ]