Writing views in Python
=======================

The couchdb-python package comes with a view server to allow you to write
views in Python instead of JavaScript. When couchdb-python is installed, it
will install a script called couchpy that runs the view server. To enable
this for your CouchDB server, add the following section to local.ini::

    [query_servers]
    python=/usr/bin/couchpy

After restarting CouchDB, the Futon view editor should show ``python`` in
the language pull-down menu. Here's some sample view code to get you started::

    def fun(doc):
        if doc['date']:
            yield doc['date'], doc

Note that the ``map`` function uses the Python ``yield`` keyword to emit
values, where JavaScript views use an ``emit()`` function.

Writing views in Python
=======================

The couchdb-python package comes with a view server to allow you to write
views in Python instead of JavaScript. When couchdb-python is installed, it
will install a script called couchpy that runs the view server. To enable
this for your CouchDB server, add the following section to local.ini::

    [query_servers]
    python=/usr/bin/couchpy

After restarting CouchDB, the Futon view editor should show ``python`` in
the language pull-down menu. Here's some sample view code to get you started::

    def fun(doc):
        if doc.get('date'):
            yield doc['date'], doc

Note that the ``map`` function uses the Python ``yield`` keyword to emit
values, where JavaScript views use an ``emit()`` function.

Update example python view code to avoid KeyErrors
Update example python view code to avoid KeyErrors

If the reader doesn't happen to have the 'date' key in one of the documents the old code would cause couchdb to throw. If the reader happened to be using futon, it would produce a useless 'no response' error.

By using .get() you'll always at least get None and this code can be used against any database.