comments please...

Ron Forrester (rjf@aurora.pcg.com)
Mon, 18 Jul 1994 11:40:52 -0700

This is really my first python program, as small as it is. Please
comment on any and all pertinent (or non-pertinent) issues...

Thanks!
rjf

---------------

#
# Simple 'cal' type clone.
#
# Usage:
#
# cal [month] [['year'] [year]]
#
# With no parameters, prints current month/year calendar.
#
# cal jan print january of current year
# cal dec 1990 print december of year 1990
# cal year 2000 print full calendar for year 2000
# cal year print full calendar for current year
#

from sys import argv, exit
from string import lower
import calendar
import time

#
# Some constants...
#
MAXYEAR = 2037
MINYEAR = 1970

#
# Global's
#
months = [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ]
month_num = {}

####################################################################
# A bad month abbrev. was entered, let user know and get out...
#
def badMonth():
print "Bad month (at least first 3 letters are significant)..."
print "Use:",
for month in months:
print `month`,
exit()

####################################################################
# A bad year was entered, let user know and get out...
#
def badYear():
print "Please enter a year between " + str(MINYEAR) + " and " + str(MAXYEAR)
exit()

####################################################################
# Make sure year falls within safe range for time module
#
def validateYear(year):
if ((year < MINYEAR) or (year > MAXYEAR)):
badYear()

####################################################################
# Make sure user entered valid month (or abbrev.)
#
def validateMonth(month):
if not month_num.has_key(month):
badMonth()

####################################################################
# It all starts here...
#
def main():

argc = len(argv)
year = 0

print

#
# Initialize the month number dictionary
#
for month in months:
month_num[month] = len(month_num)+1

#
# No arguments, just print current month
#
if argc == 1:
calendar.prmonth(eval(time.ctime(time.time())[-4:]),
month_num[lower(time.ctime(time.time())[4:7])])
else:
#
# One argument, could be asking for this whole year, or just a month
#
if argc == 2:
year = eval(time.ctime(time.time())[-4:])
if lower(argv[1]) == 'year':
calendar.prcal(year)
else:
month = lower(argv[1][:3])
validateMonth(month)
calendar.prmonth(year, month_num[month])
#
# Two parameters were passed in. Either a month and year, or just a year
#
else:
year = eval(argv[2])
validateYear(year)
if lower(argv[1]) == 'year':
calendar.prcal(year)
else:
month = lower(argv[1][:3])
validateMonth(month)
calendar.prmonth(year,month_num[month])

main()

-- 
| "I don't like being bluffed -- makes me doubt     |    rjf@aurora.pcg.com |
| my perception of reality..."                      |            71722,3175 |
|                      Chris in the morning on KBHR |                       |
+---------------------------------------------------+-----------------------+