calendar.py

Tesla Coil tescoil at rtpro.net
Mon Apr 3 16:40:27 EDT 2000


Looking at calendar.py and wondering if there's any reason
mdays, day_name, day_abbr, month_name, and month_abbr
wouldn't be better off tuples than lists.  I don't see that they
are anywhere in the module the objects of operations specific
to lists, and there doesn't seem to be any advantage having
them mutable sequences.

Who maintains calendar.py?  I'd like to propose inclusion
of functions to facilitate such calculations as "what will the
date be in 45 days," or "print dates for twelve 28 day cycles
beginning from 3/1/2000."   Excuse the undercooked code,
but something like the following:

mdays = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

def isleap(year):
    return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)

# lacks handling targets in excess of 365 (or 366) days
# eventually return, not print, results.
def day2date(target, year):
    for month in range(1, 13, 1):
        if isleap(year) and month==2 and target<=29:
            print month, target
            break
        elif target <= mdays[month]:
            print month, target
            break
        elif isleap(year) and month==2:
            target=target-29
        else:
            target=target-mdays[month]

# lacks handling of nonexistent dates, e.g., 2,29,2001
# eventually return, not print, results.
def date2day(month, day, year):
    def add(x,y): return x+y
    total=reduce(add, mdays[:month])
    total=total+day
    if isleap(year) and month > 2:
        total=total+1
    print total




More information about the Python-list mailing list