wow! (generators) - suggestion (yield saved)

Roman Suzi rnd at onego.ru
Sun Aug 5 16:01:42 CEST 2001


On Sun, 5 Aug 2001, Ken Seehof wrote:

>From: "Roman Suzi" <rnd at onego.ru>

>> What do you think?
>>
>> Sincerely yours, Roman Suzi
>
>This is python, not perl.  Readability therefore has priority over terse
>obfuscation.  I definitely prefer 'yield' over '*'.

'cause I have problems typing 'yiled\h\h\held' ;-)
Seriously, yield is as fundamental as '%' in formatting string
role. There is nothing wrong using '*' instead of yild --
readability doesn't suffer.

>The only argument I can think of in favor of '*' is that it avoids
>introducing a keyword,

I wonder if anybody used yeild as a keyword...

>and therefore is more backward compatible.  Since I don't
>think I have any legacy code with 'yield' in it (knocking on wood), I will
>gladly accept a new keyword to avoid perlization of the python language.

I equally fear it's COBOLization...

>While your use of generators is clever, I'm not sure what, if anything
>generators are actually buying you here.
>
>What's wrong with:
>
>import calendar, string
>def calt(year, month):
>    month_cal = calendar.monthcalendar(year, month)
>    scal = ''
>    scal += "<B>%s.%s</B>" % (month, year)
>    scal += "<TABLE BORDER=1>"
>    for week in month_cal:
>      scal += "\n<TR>"
>      for day in week:
>        scal += "<TD>%s</TD>" % (day or "&nbsp;")
>      scal += "</TR>"
>    scal += "</TABLE>"
>    return scal

Except for long table you get far too many concatenations.
(Well, calendar is not that long).

>Unless, of course you are trying to sell python to a sceptical client
>who, you happen to know, loves generators. :-)
>
>BTW, nice calendar format.

"Red" days are missing, though...
I also have a lambda-version I wrote long time ago

-------------------------------------------------
#!/usr/bin/python1.5
import sys, re, string
from calendar import *

b=1
t="Python Calendar"

try:
 [_,m,_,y]=re.split('&|=',raw_input())
except:
 b="Welcome!"

if b==1:
 try:
  m=int(m)
  y=int(y)
  assert 0<m<13 and 0<y<3000
  c=[day_abbr]+monthcalendar(y,m)
  t="%s for %s, %i"%(t,month_name[m],y)
  ta="<table>\n%s</table>"
  tr="<tr>%s</tr>\n"
  td="<td>%s</td>"
  def jm(f,l): return string.join(map(f,l))
  b=ta%jm(lambda(x):tr%jm(lambda(x):td%[" ",x][x!=0],x),c)
 except:
  b="Oh! Bad values given!"

print """Content-Type: text/html

<html><head><title>%(t)s</title><head><body>
<div align=center><h1>%(t)s</h1>
%(b)s
<hr>
<table><tr><td>
<form action="/cgi-bin/pycal.cgi" method="post">
Please, enter data:<br>
Month: <input type="text" name="Month"><br>
Year: <input type="text" name="Year"><br>
<input type="submit" value="Show calendar">
</form>
</td></tr></table>
</div>
</body></html>
"""%vars()
-------------------------------

(It was so formatted to beat Perl program doing the same ;-)

Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Sunday, August 05, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "By all means, let's not confuse ourselves with the facts!" _/





More information about the Python-list mailing list