[Zope-CVS] CVS: Products/ZCTextIndex - QueryParser.py:1.3 ZCTextIndex.py:1.15

Guido van Rossum guido@python.org
Sun, 19 May 2002 08:57:08 -0400


Update of /cvs-repository/Products/ZCTextIndex
In directory cvs.zope.org:/tmp/cvs-serv22007

Modified Files:
	QueryParser.py ZCTextIndex.py 
Log Message:
QueryParser refactoring step 1: add the lexicon to the constructor args.

=== Products/ZCTextIndex/QueryParser.py 1.2 => 1.3 ===
 class QueryParser:
 
-    def __init__(self):
-        pass # This parser has no persistent state
+    def __init__(self, lexicon):
+        self._lexicon = lexicon
 
     def parseQuery(self, query):
         # Lexical analysis.


=== Products/ZCTextIndex/ZCTextIndex.py 1.14 => 1.15 ===
     __implements__ = PluggableIndexInterface
 
+    ## Magic class attributes ##
+
     meta_type = 'ZCTextIndex'
 
-    manage_options= (
+    manage_options = (
         {'label': 'Settings', 'action': 'manage_main'},
     )
 
     query_options = ['query']
 
+    ## Constructor ##
+
     def __init__(self, id, extra, caller, index_factory=OkapiIndex):
         self.id = id
         self._fieldname = extra.doc_attr
@@ -64,13 +68,15 @@
         self._index_factory = index_factory
         self.clear()
 
+    ## External methods not in the Pluggable Index API ##
+
     def query(self, query, nbest=10):
         """Return pair (mapping from docids to scores, num results).
 
         The num results is the total number of results before trimming
         to the nbest results.
         """
-        tree = QueryParser().parseQuery(query)
+        tree = QueryParser(self.lexicon).parseQuery(query)
         results = tree.executeQuery(self.index)
         if results is None:
             return [], 0
@@ -107,7 +113,7 @@
         if record.keys is None:
             return None
         query_str = ' '.join(record.keys)
-        tree = QueryParser().parseQuery(query_str)
+        tree = QueryParser(self.lexicon).parseQuery(query_str)
         results = tree.executeQuery(self.index)
         return  results, (self._fieldname,)
 
@@ -120,9 +126,13 @@
         get_word = self.lexicon.get_word
         return [get_word(wid) for wid in word_ids]
 
+    ## XXX To which API does this conform? ##
+
     def clear(self):
         """reinitialize the index"""
         self.index = self._index_factory(self.lexicon)
+
+    ## Helper ##
 
     def _get_object_text(self, obj):
         x = getattr(obj, self._fieldname)