bundled/cherrypy/cherrypy/tutorial/tut08_generators_and_yield.py @ 22de90ef33ed
Add identifiers to ReviewChangeset's and ReviewSignoff's.
The identifier of a rcset/rsignoff is the filename it was saved as, which is
the hash of its contents.
This will be useful when we want to add replies to comments/signoffs.
CLI support for this feature has also been added (using --verbose and --debug
flags with 'hg review [-r REV]'), as well as some simple unit tests.
author |
Steve Losh <steve@stevelosh.com> |
date |
Sat, 27 Mar 2010 11:10:12 -0400 |
parents |
4e1fb853d9d2 |
children |
(none) |
"""
Bonus Tutorial: Using generators to return result bodies
Instead of returning a complete result string, you can use the yield
statement to return one result part after another. This may be convenient
in situations where using a template package like CherryPy or Cheetah
would be overkill, and messy string concatenation too uncool. ;-)
"""
import cherrypy
class GeneratorDemo:
def header(self):
return "<html><body><h2>Generators rule!</h2>"
def footer(self):
return "</body></html>"
def index(self):
# Let's make up a list of users for presentation purposes
users = ['Remi', 'Carlos', 'Hendrik', 'Lorenzo Lamas']
# Every yield line adds one part to the total result body.
yield self.header()
yield "<h3>List of users:</h3>"
for user in users:
yield "%s<br/>" % user
yield self.footer()
index.exposed = True
cherrypy.tree.mount(GeneratorDemo())
if __name__ == '__main__':
import os.path
thisdir = os.path.dirname(__file__)
cherrypy.quickstart(config=os.path.join(thisdir, 'tutorial.conf'))