Simple textual calendar

Mensanator mensanator at aol.com
Mon Nov 3 21:02:19 EST 2008


On Nov 3, 7:31 am, ostra pikula <ostra.pik... at gmail.com> wrote:
> On Mon, 03 Nov 2008 14:14:00 +0100, ostra pikula
>
>
>
>
>
> <ostra.pik... at gmail.com> wrote:
> >Hello everyone,
>
> >please, I need your help. I'm a beginner in python and this is
> >probably elemental to you, but represents quote a goggle for me.
>
> >I'm trying to write out a calendar for a year, in this form (imaginary
> >month below):
> >    2008, February
> >    Mon     Tue     Wed     Thu     Fri     Sat     Sun
> >            01      02      03      04      05      06
> >    07      08      09      10      11 ... et cetera
> >    2008, March
> >    ...
> >    ...
> >    ... (same as above, only for march)
>
> My mistake here (well, whose else would it be ?!); it's supposed to go
> 2008, February  2008, March
> ....                    ....
>
> Months are supposed to go, one after the other in a row, not in a
> column.

Well, you COULD put them in a column. One nice thing about the
calendar module is you can take it's output and re-format it
to match your needs. Personally, I find the tradtional format
worthless. I want to see the year represented as a single block
of 52 weeks so I can tell at a glance how many weeks seperate
Mar 1 and Sep 13.

Here's my example of how to use calendar to get what you want:

import calendar

month_list = []
c = calendar.month(2009,1)

# split multi-line string to individual lines
cw = c.split('\n')

# save name of monthe for later printing
month_list.append(cw[0])

# January is special, we don't want to trim
# the leading spaces so that Jan 1 lands on
# correct day of week. All others will fall
# into place automatically.
all_days = ' ' + ' '.join(cw[2:]) + ' '

print ' ' + cw[1] #extra space needed for DOW

# all subsequent month strings get concatenated
# so that every day is exactly 3 characters
for i in xrange(2,13):
  c = calendar.month(2009,i)
  cw = c.split('\n')
  month_list.append(cw[0])
  all_days +=  ' '.join(cw[2:]).strip() + '  '

# at this point, whole year is one long string

m = 0
# each week (except last) is 21 characters
for i in xrange(0,1092,21):
  current_week = all_days[i:i+21]
  # when we see '  1' in current_week, we need
  # to label the new month
  if '  1' in current_week:
    print current_week,month_list[m]
    m += 1
  else:
    print current_week

# print whatever's left in the year
print all_days[1092:]

## program output
## Mo Tu We Th Fr Sa Su
##           1  2  3  4     January 2009
##  5  6  7  8  9 10 11
## 12 13 14 15 16 17 18
## 19 20 21 22 23 24 25
## 26 27 28 29 30 31  1    February 2009
##  2  3  4  5  6  7  8
##  9 10 11 12 13 14 15
## 16 17 18 19 20 21 22
## 23 24 25 26 27 28  1      March 2009
##  2  3  4  5  6  7  8
##  9 10 11 12 13 14 15
## 16 17 18 19 20 21 22
## 23 24 25 26 27 28 29
## 30 31  1  2  3  4  5      April 2009
##  6  7  8  9 10 11 12
## 13 14 15 16 17 18 19
## 20 21 22 23 24 25 26
## 27 28 29 30  1  2  3       May 2009
##  4  5  6  7  8  9 10
## 11 12 13 14 15 16 17
## 18 19 20 21 22 23 24
## 25 26 27 28 29 30 31
##  1  2  3  4  5  6  7      June 2009
##  8  9 10 11 12 13 14
## 15 16 17 18 19 20 21
## 22 23 24 25 26 27 28
## 29 30  1  2  3  4  5      July 2009
##  6  7  8  9 10 11 12
## 13 14 15 16 17 18 19
## 20 21 22 23 24 25 26
## 27 28 29 30 31  1  2     August 2009
##  3  4  5  6  7  8  9
## 10 11 12 13 14 15 16
## 17 18 19 20 21 22 23
## 24 25 26 27 28 29 30
## 31  1  2  3  4  5  6    September 2009
##  7  8  9 10 11 12 13
## 14 15 16 17 18 19 20
## 21 22 23 24 25 26 27
## 28 29 30  1  2  3  4     October 2009
##  5  6  7  8  9 10 11
## 12 13 14 15 16 17 18
## 19 20 21 22 23 24 25
## 26 27 28 29 30 31  1    November 2009
##  2  3  4  5  6  7  8
##  9 10 11 12 13 14 15
## 16 17 18 19 20 21 22
## 23 24 25 26 27 28 29
## 30  1  2  3  4  5  6    December 2009
##  7  8  9 10 11 12 13
## 14 15 16 17 18 19 20
## 21 22 23 24 25 26 27
## 28 29 30 31


>
> Ostra




More information about the Python-list mailing list