Skip to content

Commit

Permalink
Document Name and parse_query
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Rossi committed Mar 18, 2011
1 parent fccd7e2 commit b8ed36d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ Boolean Operators

.. autoclass:: Not

Other Helpers
~~~~~~~~~~~~~

.. autoclass:: Name

.. autofunction:: parse_query

.. _api_fieldindex_section:

:mod:`repoze.catalog.indexes.field`
Expand Down
26 changes: 24 additions & 2 deletions repoze/catalog/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,28 @@ def _optimize(self):

class Name(object):
"""
A variable name in an expression, evaluated at query time.
A variable name in an expression, evaluated at query time. Can be used
to defer evaluation of variables used inside of expressions until query
time.
Example::
from repoze.catalog.query import Eq
from repoze.catalog.query import Name
# Define query at module scope
find_cats = Eq('color', Name('color')) & Eq('sex', Name('sex'))
# Use query in a search function, evaluating color and sex at the
# time of the query
def search_cats(catalog, resolver, color='tabby', sex='female'):
# Let resolver be some function which can retrieve a cat object
# from your application given a docid.
params = dict(color=color, sex=sex)
count, docids = catalog.query(find_cats, params)
for docid in docids:
yield resolver(docid)
"""
def __init__(self, name):
self.name = name
Expand Down Expand Up @@ -881,7 +902,8 @@ def optimize(query):

def parse_query(expr, optimize_query=True):
"""
Parses the given expression string into a catalog query.
Parses the given expression string and returns a query object. Requires
Python >= 2.6.
"""
if not ast_support:
raise NotImplementedError("Parsing of CQEs requires Python >= 2.6")
Expand Down

0 comments on commit b8ed36d

Please sign in to comment.