Writing Lists in Python?

34 views
Skip to first unread message

John Meehan

unread,
Jun 29, 2011, 11:58:11 AM6/29/11
to CouchDB-Python
Can Lists be written in Python?

What would be the Python equivalent of this example from the CouchDB
wiki (http://wiki.apache.org/couchdb/Formatting_with_Show_and_List)?

function(head, req) {
var row;
start({
"headers": {
"Content-Type": "text/html"
}
});
while(row = getRow()) {
send(row.value);
}
}

Kxepal

unread,
Jun 29, 2011, 1:20:33 PM6/29/11
to couchdb...@googlegroups.com
Hi!

Currently this could be done by using new query server from issue 146 (http://code.google.com/p/couchdb-python/issues/detail?id=146)
Equivalent would be :
def fun(head, req):
  start({
    "headers": {"Content-Type": "text/html"}
  })
  for row in get_row():
    send(row["value"])

Notice, that get_row function is generator and any dotted notation accessed attributes should be changed to getitem specification due to this is dict object.
Also notice, that builtn query server function are respect PEP-8, so there is nothing mixedCased nor CamelCase except exceptions(Forbidden, Error, FatalError).

If you would try this query server any feedback would be great(:

--
,,,^..^,,,

John Meehan

unread,
Jun 29, 2011, 2:12:53 PM6/29/11
to couchdb...@googlegroups.com
Thanks!  My next question, however, is how do I install the updated files?

I downloaded query-server.tar.gz from your May 9th post in the issue 146 thread.  

I'm running Ubuntu 10.04, and installed couchdb-python originally with easy-install.  So I have the following in my /usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb dir:

-rwxr-xr-x 1 root staff 37477 2011-06-01 09:58 client.py
-rw-r--r-- 1 root staff 45267 2011-06-01 09:58 client.pyc
-rwxr-xr-x 1 root staff  7649 2011-06-01 09:58 design.py
-rw-r--r-- 1 root staff  8355 2011-06-01 09:58 design.pyc
-rwxr-xr-x 1 root staff 17607 2011-06-01 09:58 http.py
-rw-r--r-- 1 root staff 20156 2011-06-01 09:58 http.pyc
-rwxr-xr-x 1 root staff   534 2011-06-01 09:58 __init__.py
-rw-r--r-- 1 root staff   687 2011-06-01 09:58 __init__.pyc
-rwxr-xr-x 1 root staff  4441 2011-06-01 09:58 json.py
-rw-r--r-- 1 root staff  5859 2011-06-01 09:58 json.pyc
-rwxr-xr-x 1 root staff 22337 2011-06-01 09:58 mapping.py
-rw-r--r-- 1 root staff 34371 2011-06-01 09:58 mapping.pyc
-rwxr-xr-x 1 root staff  8640 2011-06-01 09:58 multipart.py
-rw-r--r-- 1 root staff  9080 2011-06-01 09:58 multipart.pyc
drwxr-sr-x 2 root staff  4096 2011-06-01 09:58 tests
drwxr-sr-x 2 root staff  4096 2011-06-01 09:58 tools
-rwxr-xr-x 1 root staff  7167 2011-06-01 09:58 view.py
-rw-r--r-- 1 root staff  7498 2011-06-01 09:58 view.pyc

(I'm assuming I need to overwrite or add the new files from the archive here.)


--
You received this message because you are subscribed to the Google Groups "CouchDB-Python" group.
To view this discussion on the web visit https://groups.google.com/d/msg/couchdb-python/-/KQWWZ2-nQo0J.

To post to this group, send email to couchdb...@googlegroups.com.
To unsubscribe from this group, send email to couchdb-pytho...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/couchdb-python?hl=en.

Kxepal

unread,
Jun 29, 2011, 3:09:05 PM6/29/11
to couchdb...@googlegroups.com
Just extract couchdb folder from archive over couchdb installed package: where should be added server folder and replaced view.py by new one.
Next thing you have to do is specify version of CouchDB against that query server will be runned (supported since 0.9 till 1.1.0).
This could be done by edition [query_server] config option to made it looks like this (for 1.1.0 version):
python=/path/to/couchpy --couchdb-version=1.1.0 

And it should work(: For more information you may build sphinx docs - there have described all command line options, objects and usage examples.

John Meehan

unread,
Jun 30, 2011, 4:29:36 PM6/30/11
to couchdb...@googlegroups.com
Hi,

Not sure if I've done something wrong with my list code or if I'm running into an issue with the new view server.  The views I'd written worked as expected so I take it that the new view server files were installed OK, but now trying to write a test list against one of those views seems to encounter an issue compiling the list code.  This looks like the relevant portion of the view server log.  Any thoughts?

[2011-06-30 15:17:18,362] [couchdb.server.ddoc] [DEBUG] Processing DDoc command `lists`
[2011-06-30 15:17:18,362] [couchdb.server.compiler] [DEBUG] Compiling code to function:
def fun(head, req):
start({"headers": {"Content-Type": "text/html"}})
c = 0
for row in get_row():
send(str(c) + row['key'][0])
                c+=1
[2011-06-30 15:17:18,363] [couchdb.server.compiler] [ERROR] Failed to compile function
def fun(head, req):
start({"headers": {"Content-Type": "text/html"}})
c = 0
for row in get_row():
send(str(c) + row['key'][0])
                c+=1
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb/server/compiler.py", line 284, in compile_func
    bytecode = compile(funstr, '<string>', 'exec')
  File "<string>", line 1
     def fun(head, req):
                       
^
 SyntaxError: invalid syntax
[2011-06-30 15:17:18,363] [couchdb.server] [ERROR] Error `compilation_error` occurred: invalid syntax (<string>, line 1)
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb/server/__init__.py", line 253, in run
    retval = self.commands[cmd](*args)
  File "/usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb/server/ddoc.py", line 83, in ddoc
    func = compile_func(func, ddoc)
  File "/usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb/server/compiler.py", line 288, in compile_func
    raise Error('compilation_error', err)
Error: ('compilation_error', SyntaxError('invalid syntax', ('<string>', 1, 20, 'def fun(head, req):\r\n')))
[2011-06-30 15:17:18,363] [couchdb.server.stream] [DEBUG] Data to respond:
['error', 'compilation_error', SyntaxError('invalid syntax', ('<string>', 1, 20, 'def fun(head, req):\r\n'))]
[2011-06-30 15:17:18,363] [couchdb.server] [ERROR] TypeError: SyntaxError('invalid syntax', ('<string>', 1, 20, 'def fun(head, req):\r\n')) is not JSON serializable
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb/server/__init__.py", line 255, in run
    retval = self.error_handler(*sys.exc_info())
  File "/usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb/server/__init__.py", line 233, in error_handler
    return self.error_handlers.get(type, default)(type, value, traceback)
  File "/usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb/server/__init__.py", line 176, in handle_qs_error
    stream.respond(retval)
  File "/usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb/server/stream.py", line 43, in respond
    obj = json.encode(obj)
  File "/usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb/json.py", line 65, in encode
    return _encode(obj)
  File "/usr/local/lib/python2.6/dist-packages/CouchDB-0.8-py2.6.egg/couchdb/json.py", line 113, in <lambda>
    dumps(obj, allow_nan=False, ensure_ascii=False)
  File "/usr/lib/pymodules/python2.6/simplejson/__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "/usr/lib/pymodules/python2.6/simplejson/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/pymodules/python2.6/simplejson/encoder.py", line 260, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/pymodules/python2.6/simplejson/encoder.py", line 177, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: SyntaxError('invalid syntax', ('<string>', 1, 20, 'def fun(head, req):\r\n')) is not JSON serializable
[2011-06-30 15:17:18,380] [couchdb.server.stream] [DEBUG] Data to respond:
['error', 'TypeError', "SyntaxError('invalid syntax', ('<string>', 1, 20, 'def fun(head, req):\\r\\n')) is not JSON serializable"]

To view this discussion on the web visit https://groups.google.com/d/msg/couchdb-python/-/sFKh8Qt6PhYJ.

Kxepal

unread,
Jun 30, 2011, 5:38:56 PM6/30/11
to couchdb...@googlegroups.com
Wow. Looks like just SyntaxError, but it shouldn't have to produce double fail. Ok, I'll look why it happened.
Problem is that Python < 2.7 doesn't allows Windows/Mac style newlines within compile function (\r\n chars) [1]. That's my fault especially because I'm using Linux on my dev notebook and I even didn't know about such behavior(: Shortest workaround is to replace all \r\n chars to \n in function source code. I'll attach fixed version on this weekends due to I'm currently rewriting test methods from functional to pure unit tests.

Thanks for report!(:


To post to this group, send email to couch...@googlegroups.com.

To unsubscribe from this group, send email to couchdb-pytho...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/couchdb-python?hl=en.

John Meehan

unread,
Jul 1, 2011, 8:55:44 AM7/1/11
to couchdb...@googlegroups.com
Ah yes.  That's what I get for editing from my Windows box.  I stripped the carriage returns and looks like it's working!  Thanks very much for your help.

To view this discussion on the web visit https://groups.google.com/d/msg/couchdb-python/-/kpeiuvjMCp8J.
Reply all
Reply to author
Forward
0 new messages