Simple textual calendar

O

ostra pikula

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)


(don't know how this will come up in your newsreaders, but you get the
general idea).

I discovered datetime module, but I'm not sure how to do the actual
calendar writeout (formatting). Please, if you could throw a tip or
two, I'd be very grateful.

Ostra

p.s. This is not homework or anything in that direction. It's just I'm
not a programmer, and so this and everything is still new to me.
 
A

Arnaud Delobelle

ostra pikula said:
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)


(don't know how this will come up in your newsreaders, but you get the
general idea).

I discovered datetime module, but I'm not sure how to do the actual
calendar writeout (formatting). Please, if you could throw a tip or
two, I'd be very grateful.

Ostra

p.s. This is not homework or anything in that direction. It's just I'm
not a programmer, and so this and everything is still new to me.

Have your looked at the calendar module?

http://www.python.org/doc/2.5.2/lib/module-calendar.html

HTH
 
O

ostra pikula

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.

Ostra
 
S

skip

ostra> It is an example which I've setted myself to try to solve to see
ostra> how it would go.

An even simpler solution is the cal command on Unix systems:

% cal 2009
2009

Jan Feb Mar
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7
4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14
11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21
18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28
25 26 27 28 29 30 31 29 30 31

Apr May Jun
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 1 2 1 2 3 4 5 6
5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
31
Jul Aug Sep
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 4 1 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
30 31
Oct Nov Dec
S M Tu W Th F S S M Tu W Th F S S M Tu W Th F S
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
25 26 27 28 29 30 31 29 30 27 28 29 30 31

:)

If you really want to write something yourself take a look at the datetime
module. Here's a trivial (untested) example:

import datetime
date = datetime.datetime(2009, 1, 1)
end = datetime.datetime(2010, 1, 1)
oneday = datetime.timedelta(days=1)
while date < end:
print date.date()
date += oneday

Correctness, formatting and efficiency are left as an exercise for the
reader. ;-)
 
T

Tim Chase

Yes, I saw the calendar module, but, as I said, this isn't
homework or something like that. It is an example which I've
setted myself to try to solve to see how it would go. Calendar
just gives me the solution :-(

Part of the answer to a problem is knowing when not to duplicate
effort that's already been exerted & tested... :)

However, yes, several of my mini apps have wanted to print
decorated calendars on the command-line so I've had to rehash
this logic. The code in the calendar.py can be instructive, and
it's not to complex to use the "calendar.monthcalendar(year,
month)" function to draw these calendars:

from calendar import monthcalendar, month_name
year = 2008
for month in range(1,13):
print year, month_name[month]
for i,week in enumerate(monthcalendar(year, month)):
print "Week %i" % (i+1),
for day in week:
print " %02i" % day,
print

(adjust for special handling of "day=0")

-tkc
 
J

Jorgen Grahn

Part of the answer to a problem is knowing when not to duplicate
effort that's already been exerted & tested... :)

But this one is a good intellectual exercise which combines puzzle
elements and outside requirements (i.e. what a calendar looks like).

And besides, someone has to write those modules, you know ;-)

I'd start with just one month, and a piece of paper. Assuming Sunday
starts the week:

s m t w t f s
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .

It's obvious that worst-case you need six rows. Then I'd pretend that
the day-of-week of the 1st, and the number of days in the month are
inputs to the problem so I don't have to worry about them. And I'd
pretend that the output is just a list of 7*6 numbers, with 0 for the
unused days (or None, or '').

def month(weekday1, days):
# -> list of 42 numbers
pass

Then I'd pick an example: a month with the 1st on Tuesday (2) and 30
days. That would be [0]*2 + [1 .. 30] + [0]*10. And that more or less
makes the solution (of this sub-problem) obvious. I'd implement it,
write unit tests, and move on to the next part of the problem with a
bit more confidence.

/Jorgen
 
M

Mensanator

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
 
J

Jorgen Grahn

....
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
^^^^^^^^
Or 53 weeks.
Mar 1 and Sep 13.
....
## 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
....

This is completely unrelated to Python, but damn, you are right!
Now that I see it, it's obviously better than the traditional format
you mention, the one that the Unix utility "cal -y" has used since
the 1980s or something.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top