In our calendar, it would be nice to have a header for the calendar that reads something like "A Python-Generated Calendar For..." and have the current month and year. In order to do this, we need to get the month and year from the system. This functionality is something that calendar provides, Python can retrieve the month and year. But we still have a problem. As all system dates are numeric and do not contain unabbreviated or non-numeric forms of the months, we need a list of those months. Enter the list year.
year = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
Now when we get the number of a month, we can access that number (minus one) in the list and get the full month name.
Starting the main() function, let's ask datetime for the time.
def main(): today = datetime.datetime.date(datetime.datetime.now())
Curiously, the datetime module has a datetime class. It is from this class that we call two objects: now() and date(). The method datetime.datetime.now() returns an object containing the following information: year, month, date, hour, minute, second, and microseconds. Of course, we have no need for the time information. To cull out the date information alone, we pass the results of now() to datetime.datetime.date() as an argument. The result is that today now contains the year, month, and date separated by em-dashes.
To break this bit of data into more managable pieces, we must split it. We can then assign the parts to the variables current_yr, current_month, and current_day respectively.
current = re.split('-', str(today)) current_no = int(current) current_month = year[current_no-1] current_day = int(re.sub('\A0', '', current)) current_yr = int(current)
To understand the first line of this code, work from the right to the left and from the inside outward. First, we stringify the object today in order to operate on it as a string. Then, we split it using the em-dash as a delimiter, or token. Finally, we assign those three values as a list to 'current'.
In order to deal with these values more distinctly and to call the long name of the current month out of year, we assign the number of the month to current_no. We can then do a bit of subtraction in the subscript of year and assign the month name to current_month.
In the next line, a bit of substitution is needed. The date which is returned from datetime is a two-digit value even for the first nine days of the month. A zero functions as a place holder, but we would rather our calendar have just the single digit. So we substitute no value for every zero that begins a string (hence '\A'). Finally, we assign the year to current_yr, converting it to an integer along the way.
Methods that we will call later will require input in integer format. Therefore, it is important to ensure that all of the date data is saved in integer, not string, form.
Before we print the calendar, we need to print the HTML preamble and CSS layout for our calendar. Go to this page for the code to print the CSS and HTML preamble for the calendar. and copy the code into your program file. The CSS in the HTML of this file follows the template offered by Jennifer Kyrnin, About's Guide to Web Design. If you do not understand this part of the code, you may want to consult her helps for learning CSS and HTML. Finally, to customise the month name, we need the following line:
print '<h1> %s %s </h1 >' %(current_month, current_yr)
Now that the basic layout is output, we can set up the calendar itself. A calendar, at its most basic point, is a table. So let's make a table in our HTML:
print ''' <table id="month" > <thead > <tr > <th class="weekend" >Sunday</th > <th >Monday</th > <th >Tuesday</th > <th >Wednesday</th > <th >Thursday</th > <th >Friday</th > <th class="weekend" >Saturday</th > </tr > </thead > <tbody > '''
Now our program will print our desired header with the current month and year. If you have used the command-line option mentioned earlier, here you should insert an if-else statement as follows:
if firstday == '0': print ''' <table id="month" > <thead > <tr > <th >Monday</th > <th >Tuesday</th > <th >Wednesday</th > <th >Thursday</th > <th >Friday</th > <th class="weekend" >Saturday</th > <th class="weekend" >Sunday</th > </tr > </thead > <tbody > ''' else: ## Here we assume a binary switch, a decision between '0' or not '0'; therefore, any non-zero argument will cause the calendar to start on Sunday. print ''' <table id="month" > <thead > <tr > <th class="weekend" >Sunday</th > <th >Monday</th > <th >Tuesday</th > <th >Wednesday</th > <th >Thursday</th > <th >Friday</th > <th class="weekend" >Saturday</th > </tr > </thead > <tbody > '''