Issue 211 in couchdb-python: Cannot access _id and _doc attributes in a view written in Python

31 views
Skip to first unread message

couchdb...@googlecode.com

unread,
Apr 15, 2012, 4:56:30 AM4/15/12
to couchdb...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 211 by christop...@gmail.com: Cannot access _id and _doc
attributes in a view written in Python
http://code.google.com/p/couchdb-python/issues/detail?id=211

What steps will reproduce the problem?
- Create a view like this:
mapfunc="""
def foo(doc):
yield(doc._id, 42)
"""
- Create a query object like:
q=some_database.query(map_fun=mapfunc)
- The query object gets created properly:
<ViewResults <TemporaryView 'def foo(doc):\n yield(doc._id,42)\n'
None> {}>
- However getting the rows or the number of rows fails:
/home/chaas/projekte/debshots/<ipython-input-82-f16a381fffa4> in <module>()
----> 1 q.total_rows

/usr/lib/pymodules/python2.7/couchdb/client.pyc in total_rows(self)
1013 """
1014 if self._rows is None:
-> 1015 self._fetch()
1016 return self._total_rows
1017

/usr/lib/pymodules/python2.7/couchdb/client.pyc in _fetch(self)
988
989 def _fetch(self):
--> 990 data = self.view._exec(self.options)
991 wrapper = self.view.wrapper or Row
992 self._rows = [wrapper(row) for row in data['rows']]

/usr/lib/pymodules/python2.7/couchdb/client.pyc in _exec(self, options)
912 _, _, data = self.resource.post_json(body=content, headers={
913 'Content-Type': 'application/json'
--> 914 }, **self._encode_options(options))
915 return data
916

/usr/lib/pymodules/python2.7/couchdb/http.pyc in post_json(self, *a, **k)
397
398 def post_json(self, *a, **k):
--> 399 status, headers, data = self.post(*a, **k)
400 if 'application/json' in headers.get('content-type'):
401 data = json.decode(data.read())

/usr/lib/pymodules/python2.7/couchdb/http.pyc in post(self, path, body,
headers, **params)
379 def post(self, path=None, body=None, headers=None, **params):
380 return self._request('POST', path, body=body,
headers=headers,
--> 381 **params)
382
383 def put(self, path=None, body=None, headers=None, **params):

/usr/lib/pymodules/python2.7/couchdb/http.pyc in _request(self, method,
path, body, headers, **params)
417 return self.session.request(method, url, body=body,
418 headers=all_headers,
--> 419 credentials=self.credentials)
420
421

/usr/lib/pymodules/python2.7/couchdb/http.pyc in request(self, method, url,
body, headers, credentials, num_redirects)
237 raise
238
--> 239 resp = _try_request_with_retries(iter(self.retry_delays))
240 status = resp.status
241

/usr/lib/pymodules/python2.7/couchdb/http.pyc in
_try_request_with_retries(retries)
203 except StopIteration:
204 # No more retries, raise last socket error.

--> 205 raise e
206 time.sleep(delay)
207 conn.close()

error: 104

In the /var/log/couchdb/couchdb.log I see a lot of log output. The most
interesting line here probably was:

<<"Traceback (most recent call last):\n File
\"/usr/lib/pymodules/python2.7/couchdb/view.py\", line 78, in map_doc\n
results.append([[key, value] for key, value in function(doc)])\n File
\"<string>\", line 2, in foo\nAttributeError: 'dict' object has no
attribute '_id'\n">>}]}}}]},


It appears as if the attributes starting with "_" are not getting passed
through.

---

What is the expected output? What do you see instead?
- I expected to get a view of all document IDs (_id) as keys.
- Instead I received an "error: 104".

---

What version of the product are you using? On what operating system?
- CouchDB 1.1.1
- python-couchdb 0.8-1
- Python 2.7.3


couchdb...@googlecode.com

unread,
Apr 15, 2012, 6:43:39 AM4/15/12
to couchdb...@googlegroups.com

Comment #1 on issue 211 by kxepal: Cannot access _id and _doc attributes in

Hi,
This is not a problem: documents are represented as pure dicts in CouchDB
Python query functions and do not have support of object notation access to
attributes as Javascript one provides. Your map function should be
def foo(doc):
yield doc['_id'], 42
to work properly.
Reason that error had raised on any attribute access is a specific of lazy
initialization when temporary view had executed only when any information
from it was requested.

P.S. q=some_database.query(map_fun=mapfunc) should also has language
argument because default expected language of queries is javascript, not
python.

couchdb...@googlecode.com

unread,
Apr 23, 2012, 4:17:43 AM4/23/12
to couchdb...@googlegroups.com

Comment #2 on issue 211 by christop...@gmail.com: Cannot access _id and
_doc attributes in a view written in Python
http://code.google.com/p/couchdb-python/issues/detail?id=211

Thank you!

couchdb...@googlecode.com

unread,
Oct 11, 2012, 8:18:11 PM10/11/12
to couchdb...@googlegroups.com
Updates:
Status: WorksForMe

Comment #3 on issue 211 by kxepal: Cannot access _id and _doc attributes in
(No comment was entered for this change.)

Reply all
Reply to author
Forward
0 new messages