Logo F2FInterview

Python Advanced Interview Questions

Q   |   QA

Now we need to create the actual calendar. To get the actual calendar data, we need the calendar module's monthcalendar() method. This method takes two arguments: the year and the month of the desired calendar (both in integer form). It returns a list which contains lists of the dates of the month by week. So if we count the number of items in the returned value, we have the number of weeks in the given month.

month = calendar.monthcalendar(current_yr, current_no)
nweeks = len(month)  

Knowing the number of weeks in the month, we can create a for loop which counts through a range() from 0 to the number of weeks. As it does, it will print out the rest of the calendar.

for w in range(0,nweeks):
     week = month[w]
     print "<tr>"
     for x in xrange(0,7):
          day = week[x]
          if x == 5 or x == 6:
               classtype = 'weekend'
               classtype = 'day'

           if day == 0:
                classtype = 'previous'
                print '<td class="%s"></td>' %(classtype)
           elif day == current_day:
                print '<td class="%s"><strong>%s</strong></span><div class="%s"></div></td>' %(classtype, day, classtype)
                print '<td class="%s">%s</span><div class="%s"></div></td>' %(classtype, day, classtype)
                print "</tr>"

           print ''' </tbody>

We will discuss this code line-by-line on the next page. 

After this range has been started, the dates of the week are culled from month according to the value of the counter and assigned to week. Then, a tabular row is created to hold the calendar dates.

A for loop then walks through the days of the week so they can be analyzed. The calendar module prints a '0' for every date in the table that does not have a valid value. A blank value would work better for our purposes so we print the bookends of tabular data without a value for those dates.

Next, if the day is the current one, we should highlight it somehow. Based on the td class today, the CSS of this page will cause the current date to be rendered against a dark background instead of the light background of the other dates.

Finally, if the date is a valid value and is not the current date, it is printed as tabular data. The exact color combinations for these are held in the CSS style preamble.

The last line of the first for loop closes the row. With the calendar printed our task is finished and we can close the HTML document.

print "</tbody></table></body></html>"

As all of this code is in the main() function, do not forget to call it.

if __name__ == "__main__":

Just this simple calendar can be used in any way that needs a calendar representation. By hyperlinking the dates in the HTML, one can easily create a diary functionality. Alternatively, one can check against a diary file and then reflect which dates are taken by their color. Or, if one converts this program into a CGI script, one can have it generated on the fly.

For many people, network programming is a black art, with network programmers being the witch-doctors of the Web. As with most things that relate to computers, it really is easy once you know a little bit about it. This tutorial will help you grasp the basics of client operations by building a simple web client.

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: