Package couchdb :: Module client :: Class ViewResults

Class ViewResults



object --+
         |
        ViewResults

Representation of a parameterized view (either permanent or temporary) and the results it produces.

This class allows the specification of key, startkey, and endkey options using Python slice notation.

>>> server = Server('http://localhost:5984/')
>>> db = server.create('python-tests')
>>> db['johndoe'] = dict(type='Person', name='John Doe')
>>> db['maryjane'] = dict(type='Person', name='Mary Jane')
>>> db['gotham'] = dict(type='City', name='Gotham City')
>>> code = '''function(doc) {
...     map([doc.type, doc.name], doc.name);
... }'''
>>> results = db.query(code)

At this point, the view has not actually been accessed yet. It is accessed as soon as it is iterated over, its length is requested, or one of its rows, total_rows, or offset properties are accessed:

>>> len(results)
3

You can use slices to apply startkey and/or endkey options to the view:

>>> people = results[['Person']:['Person','ZZZZ']]
>>> for person in people:
...     print person.value
John Doe
Mary Jane
>>> people.total_rows, people.offset
(3, 1)

Use plain indexed notation (without a slice) to apply the key option. Note that as CouchDB makes no claim that keys are unique in a view, this can still return multiple rows:

>>> list(results[['City', 'Gotham City']])
[<Row id=u'gotham', key=[u'City', u'Gotham City'], value=u'Gotham City'>]


Instance Methods
 
__init__(self, view, options)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
 
__repr__(self)
repr(x)
 
__getitem__(self, key)
 
__iter__(self)
 
__len__(self)

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __str__

Properties
list rows
The list of rows returned by the view.
int total_rows
The total number of rows in this view.
int offset
The offset of the results from the first row in the view.

Inherited from object: __class__

Method Details

__init__(self, view, options)
(Constructor)

 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__
(inherited documentation)

__repr__(self)
(Representation operator)

 
repr(x)
Overrides: object.__repr__
(inherited documentation)

Property Details

rows

The list of rows returned by the view.
Get Method:
couchdb.client.ViewResults._get_rows(self)
Type:
list

total_rows

The total number of rows in this view.
Get Method:
couchdb.client.ViewResults._get_total_rows(self)
Type:
int

offset

The offset of the results from the first row in the view.
Get Method:
couchdb.client.ViewResults._get_offset(self)
Type:
int