-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathgenerators_and_yield.py
42 lines (29 loc) · 1.27 KB
/
generators_and_yield.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
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. ;-)
"""
# SOURCE: https://github.com/cherrypy/cherrypy/blob/master/cherrypy/tutorial/tut08_generators_and_yield.py
import cherrypy
class GeneratorDemo:
def header(self):
return "<html><body><h2>Generators rule!</h2>"
def footer(self):
return "</body></html>"
@cherrypy.expose
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()
if __name__ == "__main__":
# CherryPy always starts with app.root when trying to map request URIs
# to objects, so we need to mount a request handler root. A request
# to '/' will be mapped to HelloWorld().index().
cherrypy.quickstart(GeneratorDemo())