Anonymous avatar Anonymous committed b25aea0

docs/docstrings: use "with" statement when creating searchers, so they get closed. fixes #75.

note: most of this diff is due to indenting caused by "with" usage.

Comments (0)

Files changed (8)

docs/source/indexing.rst

 
     def incremental_index(dirname)
         ix = index.open_dir(dirname)
-        searcher = ix.searcher()
 
         # The set of all paths in the index
         indexed_paths = set()
         # The set of all paths we need to re-index
         to_index = set()
 
-        writer = ix.writer()
+        with ix.searcher() as searcher:
+          writer = ix.writer()
 
-        # Loop over the stored fields in the index
-        for fields in searcher.all_stored_fields():
-          indexed_path = fields['path']
-          indexed_paths.add(indexed_path)
+          # Loop over the stored fields in the index
+          for fields in searcher.all_stored_fields():
+            indexed_path = fields['path']
+            indexed_paths.add(indexed_path)
 
-          if not os.path.exists(indexed_path):
-            # This file was deleted since it was indexed
-            writer.delete_by_term('path', indexed_path)
+            if not os.path.exists(indexed_path):
+              # This file was deleted since it was indexed
+              writer.delete_by_term('path', indexed_path)
 
-          else:
-            # Check if this file was changed since it
-            # was indexed
-            indexed_time = fields['time']
-            mtime = os.path.getmtime(indexed_path)
-            if mtime > indexed_time:
-              # The file has changed, delete it and add it to the list of
-              # files to reindex
-              writer.delete_by_term('path', indexed_path)
-              to_index.add(indexed_path)
+            else:
+              # Check if this file was changed since it
+              # was indexed
+              indexed_time = fields['time']
+              mtime = os.path.getmtime(indexed_path)
+              if mtime > indexed_time:
+                # The file has changed, delete it and add it to the list of
+                # files to reindex
+                writer.delete_by_term('path', indexed_path)
+                to_index.add(indexed_path)
 
-        # Loop over the files in the filesystem
-        # Assume we have a function that gathers the filenames of the
-        # documents to be indexed
-        for path in my_docs():
-          if path in to_index or path not in indexed_paths:
-            # This is either a file that's changed, or a new file
-            # that wasn't indexed before. So index it!
-            add_doc(writer, path)
+          # Loop over the files in the filesystem
+          # Assume we have a function that gathers the filenames of the
+          # documents to be indexed
+          for path in my_docs():
+            if path in to_index or path not in indexed_paths:
+              # This is either a file that's changed, or a new file
+              # that wasn't indexed before. So index it!
+              add_doc(writer, path)
 
-        writer.commit()
+          writer.commit()
 
 The incremental_index function:
 

docs/source/keywords.rst

   the ``content`` field of emails whose ``emailto`` field contains
   ``matt@whoosh.ca``::
 
-        searcher = email_index.searcher()
-        docnums = searcher.document_numbers(emailto=u"matt@whoosh.ca")
-        keywords = [keyword for keyword, score
-                    in searcher.key_terms(docnums, "body")]
+        with email_index.searcher() as s:
+            docnums = s.document_numbers(emailto=u"matt@whoosh.ca")
+            keywords = [keyword for keyword, score
+                        in s.key_terms(docnums, "body")]
 
 * Extract keywords from arbitrary text not in the index.
 
   Use the :meth:`~whoosh.searching.Searcher.key_terms_from_text` method of a
   :class:`whoosh.searching.Searcher` to extract the keywords, given the text::
 
-        searcher = email_index.searcher()
-        keywords = [keyword for keyword, score
-                    in searcher.key_terms_from_text("body", mytext)]
+        with email_index.searcher() as s:
+            keywords = [keyword for keyword, score
+                        in s.key_terms_from_text("body", mytext)]
 
 
 Expansion models

docs/source/recipes.rst

         return 1.0 / (poses[0] + 1)
 
     pos_weighting = scoring.FunctionWeighting(pos_score_fn)
-    searcher = myindex.searcher(weighting=pos_weighting)
+    with myindex.searcher(weighting=pos_weighting) as s:
+        ...
 
 
 Results

docs/source/schema.rst

     # ...
     w.commit()
 
-    s = ix.searcher()
-    p = qparser.QueryParser("path", schema=schema)
-    s.search(p.parse(u"test_id:alfa"))
+    qp = qparser.QueryParser("path", schema=schema)
+    q = qp.parse(u"test_id:alfa")
+    with ix.searcher() as s:
+        results = s.search(q)
 
 
 Advanced schema setup

docs/source/searching.rst

 
     from whoosh.qparser import QueryParser
 
+    qp = QueryParser("content", schema=myindex.schema)
+    q = qp.parse(u"hello world")
+
     with myindex.searcher() as s:
-        qp = QueryParser("content", schema=myindex.schema)
-        q = qp.parse(u"hello world")
-
         results = s.search(q)
 
 By default the results contains at most the first 10 matching documents. To get
 You can set the scoring object to use when you create the searcher using the
 ``weighting`` keyword argument::
 
-    s = myindex.searcher(weighting=whoosh.scoring.Cosine())
+    with myindex.searcher(weighting=whoosh.scoring.Cosine()) as s:
+        ...
 
 A scoring object is an object with a :meth:`~whoosh.scoring.Weighting.score`
 method that takes information about the term to score and returns a score as a

src/whoosh/query/qcore.py

     def docs(self, searcher):
         """Returns an iterator of docnums matching this query.
 
-        >>> searcher = my_index.searcher()
-        >>> list(my_query.docs(searcher))
+        >>> with my_index.searcher() as searcher:
+        ...     list(my_query.docs(searcher))
         [10, 34, 78, 103]
 
         :param searcher: A :class:`whoosh.searching.Searcher` object.

src/whoosh/scoring.py

             return 1.0 / (poses[0] + 1)
         
         pos_weighting = scoring.FunctionWeighting(pos_score_fn)
-        searcher = myindex.searcher(weighting=pos_weighting)
-        
+        with myindex.searcher(weighting=pos_weighting) as s:
+            results = s.search(q)
+
     Note that the searcher passed to the function may be a per-segment searcher
     for performance reasons. If you want to get global statistics inside the
     function, you should use ``searcher.get_parent()`` to get the top-level

src/whoosh/searching.py

         <query.And ...>
         >>> correction.string
         'mary "little lamb"'
+        >>> mysearcher.close()
 
         You can use the ``Correction`` object's ``format_string`` method to
         format the corrected query string using a
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.