Calendar Stuff

Dave Angel davea at ieee.org
Tue Nov 10 22:05:31 CET 2009


(Comments inline, and at end)

Victor Subervi wrote:
> On Tue, Nov 10, 2009 at 2:02 PM, MRAB <python at mrabarnett.plus.com> wrote:
>
>   
>> Victor Subervi wrote:
>>
>>     
>>> Hi;
>>> I have the following code:
>>>
>>> import calendar, datetime
>>>
>>> def cal():
>>>  ...
>>>  myCal = calendar.Calendar(calendar.SUNDAY)
>>>  today = datetime.date.today()
>>>  day = today.day
>>>  mo = today.month
>>>  yr = today.year
>>> #  month = myCal.monthdayscalendar(int(time.strftime("%Y"))
>>>  month = myCal.monthdayscalendar(yr, mo)
>>>  print 'hi'
>>>
>>> html headers are included. No matter which one of the last two lines I
>>> comment out, I never get to the point of printing 'hi'. (If I comment them
>>> both out, it does print.) What do?
>>>
>>>  Read the tracebacks?
>>>       
>> The commented line will raise an exception because:
>>
>> 1. There's a final ')' missing.
>>
>> 2. You haven't imported 'time'.
>>
>> 3. .monthdayscalendar() requires the year and month.
>>
>> I don't know why you're writing 'int(time.strftime("%Y"))' because you
>> already have the year in 'yr'.
>>
>> The code above works for me if I comment out the line '...'.
>> --
>>  <http://mail.python.org/mailman/listinfo/python-list>
>>     
>
> It works fine for me in the python interpreter but not in the page from
> which it's called! 
Then why didn't you say so, and in particular mention that this is a 
problem only when running on a webserver as a cgi program?  And give the
error message, and examine the server's log file, and ...

> You say import time, but where do I call time in my
> script? 
On the commented line with the call to  time.strftime(), that looks to 
me like a ref to the time module.  You said in your original message 
that it fails whichever of the last two lines is commented out...    
well one reason it would fail is no import of time.

> Here's the code (abbreviated just to print something out to see it
> work), please advise if you can:
>
> #!/usr/bin/python
>
> import string
> import sys,os
> sys.path.append(os.getcwd())
> import calendar, datetime, time
> import MySQLdb
> import string, re
> from login import login
>
> def calendarPrint():
>   print "Content-Type: text/html"
>   print
>   print """
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "
> http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
> <head xmlns="http://www.w3.org/1999/xhtml">
> <style type='text/css'>
> .text { font-family: Arial, Helvetica, sans-serif; font-size: 16px;
> text-decoration: none; text-align: justify }
> </style>
> <title>NR Electric</title>
> <meta http-equiv="distribution" content="Global" />
> <meta http-equiv="robots" content="index all, follow all" />
> </head>
> <body>
> <script language="JavaScript1.2" src="wz_tooltip.js"
> type="text/javascript"></
> script>
>
>   
You can't break apart the </script>   The slash must appear directly 
before the 'script'
> """
>   myCal = calendar.Calendar(calendar.SUNDAY)
>   today = datetime.date.today()
>   day = today.day
>   mo = today.month
>   yr = today.year
>   month = myCal.monthdayscalendar(yr, mo)
>   print month
>
> calendarPrint()
>
>   
I haven't tried to upload it to a server.  But running it locally, then 
taking the output and feeding it to Firefox, I see a few things wrong.  
It's declared asw HTML, but you don't have a <html> opening tag.  And 
you're missing closing tags for /body and /html   Tidy complains that 
the DOCTYPE url is malformed, but you couldn't prove it by me.

And month is a list of lists, so I don't know why you're printing it.

DaveA



More information about the Python-list mailing list