[Edu-sig] Teaching Python with the Calendar

Kirby Urner urnerk at qwest.net
Sat Nov 13 20:38:58 CET 2004


Since starting with Python tutoring, I've already learned a lot.  For
example, I'm a new convert to this technique of programming around the
Gregorian calendar (that's the familiar one, to us ISO-Latin types).  

It's a good mix of real world and abstraction, in that figuring the holidays
is kinda messy (given the calendar is -- leap year and all that), yet coded
solutions exist (most easily within the epoch -- I've not ventured outside
it).

For example, Columbus Day is the Monday nearest to October 12th.  Here's my
code for that:

def getcolday(y):
    """
    Get Columbus Day, return daynum
    Monday closest to Oct 12
    """
    i = j = 12
    while calendar.weekday(y, 10, i) <> 0:
        i -= 1
    while calendar.weekday(y, 10, j) <> 0:
        j += 1
    if abs(12-i) > abs(12-j):
        closest = j
    else:
        closest = i
    return getdaynum(y, 10, closest)

def getdaynum(y, m, d):
    """
    Return day number
    """
    return time.localtime(time.mktime((y, m, d, 0, 0, 0, 0, 0, 0)))[7]

What gets returned is an integer between 1 and 365 (or 366 on leap years),
which I then use against a dictionary that looks like this (my student
compiled it -- I credit him in the comments, but redact for here):

def alldates(year):
    """
    Returns all the dates of holidays in a year in a dictionary
    Compiled by xxx
    """
    leap    = calendar.isleap(year)
    easter  = geteaster(year)
    easter  = apply(getdaynum, easter)    
    thedays = {304 + leap    : 'Halloween',
               185 + leap    : 'July 4th',
               359 + leap    : 'Christmas',
               305 + leap    : 'All Saints Day',
               306 + leap    : 'All Souls Day',
               1             : 'New Year\'s Day',
               365 + leap    : 'New Year\'s Eve',
               315 + leap    : 'Veterans Day',
               165 + leap    : 'Flag Day',
               easter        : 'Easter',
               easter - 1    : 'Holy Saturday',
               easter - 2    : 'Good Friday',
               easter - 7    : 'Palm Sunday',
               easter - 40   : 'Ash Wednesday',
               getnthday(year,11,3,4) : 'Thanks- giving',
               getnthday(year,1,0,3)  : "MLK B'day",
               getnthday(year,2,0,3)  : "Presidents Day",
               getmemday(year)        : "Memorial Day",
               getnthday(year,9,0,1)  : "Labor Day",
               getcolday(year)        : "Columbus Day"
              }
    return thedays

Beyond just coding the holidays (I focused on some Xtian plus a few secular,
in part because my student is interested in the Roman Catholic calendar --
alternative dictionaries suggest themselves) there's the CGI angle.  

Of course this is not a new idea, I found many websites implementing it, but
to roll one of one's own, in Python, provides a useful exercise:  in cgi,
HTML, even CSS.

Here's my latest example:  http://www.4dsolutions.net/cgi-bin/calendar2.cgi

Kirby




More information about the Edu-sig mailing list