Snakelets and WSGI

Alan Kennedy alanmk at hotmail.com
Wed Oct 13 08:19:13 EDT 2004


[Alan Kennedy]
 > The choice would between which one to use could even be made at runtime,
 > and could differ for each request, e.g. based on cofiguration, request
 > parameters, etc.
 >
 > The code could look something like this
 >
 > return_html = ['<html>', '<etc>', '</html>']
 > if use_iterator_interface:
 >     yield return_html
 > else:
 >     write("".join(return_html))
 >     yield []

Well, I should have been a little more careful about that code. Here is 
a more complete and correct example

def application_object(wsgi_environ, start_response):
     writer = start_response("200 OK", [ ('context-type', 'text/html') ])
     return_html = ['<html>', '<etc>', '</html>']
     if use_iterator_interface:
         for piece in return_html:
             yield piece
     else:
         writer("".join(return_html))
     # StopIteration automatically raised here

Or you could write it without generators, i.e. for python < 2.2, like so

def application_object(wsgi_environ, start_response):
     writer = start_response("200 OK", [ ('context-type', 'text/html') ])
     return_html = ['<html>', '<etc>', '</html>']
     if use_iterator_interface:
         # The returned list is inherently iterable
         return return_html
     else:
         writer("".join(return_html))
         # Must explicitly return an empty iterable here
         return []

It is illegal to use a combination of iterators and the write callable 
in the same application object.

Lastly, jython development activity seems to be on the cusp of re-igniting.

http://sourceforge.net/mailarchive/forum.php?thread_id=5754593&forum_id=5587

regards,

-- 
alan kennedy
------------------------------------------------------
email alan:              http://xhaus.com/contact/alan



More information about the Python-list mailing list