wow! (generators) - suggestion (yield saved)
Gerhard Häring
gerhard.nospam at bigfoot.de
Sun Aug 5 06:20:02 EDT 2001
On Sun, 5 Aug 2001 11:49:53 +0400 (MSD), Roman Suzi <rnd at onego.ru> wrote:
>
>Hello!
>
>The following code is from CGI script and provides way to have
>nice calendar:
>
>------------------------------------------------------------
>from __future__ import generators
>import calendar, string
>def calt(year, month):
> month_cal = calendar.monthcalendar(year, month)
> yield """<B>%s.%s</B>""" % (month, year)
> yield """<TABLE BORDER=1>"""
> for week in month_cal:
> yield """\n<TR>"""
> for day in week:
> yield """<TD>%s</TD>""" % (day or " ")
> yield """</TR>"""
> yield """</TABLE>"""
>
>print string.join(calt(2001, 8), "")
>--------------------------------------------------------
>
Like in most of the cases I played with generators, you could have replaced
them with a simple s = "" at the beginning, replace every yield with s +=
<whatever> and a return s at the end. I think you can always do this if all you
want is a list.
>I like generators :-)
I like them, too :-)
>(Probably Python should use "black bullet" or "*" instead of "yield":
I prefer "yield".
Gerhard
--
mail: gerhard <at> bigfoot <dot> de registered Linux user #64239
web: http://highqualdev.com public key at homepage
public key fingerprint: DEC1 1D02 5743 1159 CD20 A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y: x+y, [chr(ord(x)^42) for x in list('zS^BED\nX_FOY\x0b')])
More information about the Python-list
mailing list