lanto03 / couchdb-python

Automatically exported from code.google.com/p/couchdb-python
Other
0 stars 0 forks source link

Cannot access _id and _doc attributes in a view written in Python #211

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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

Original issue reported on code.google.com by christop...@gmail.com on 15 Apr 2012 at 8:56

GoogleCodeExporter commented 8 years ago
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.

Original comment by kxepal on 15 Apr 2012 at 10:43

GoogleCodeExporter commented 8 years ago
Thank you!

Original comment by christop...@gmail.com on 23 Apr 2012 at 8:17

GoogleCodeExporter commented 8 years ago

Original comment by kxepal on 12 Oct 2012 at 12:18