Created
February 27, 2015 04:17
-
-
Save kwatch/aed2b9b52ce2d3b18e81 to your computer and use it in GitHub Desktop.
1ヶ月分のカレンダーを表示する(その1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
try: | |
xrange | |
except NameError: | |
xrange = range | |
def build_month(ndays, wday): | |
""" | |
1ヶ月分のデータを返す。 | |
たとえば build_month(30, 2) なら | |
[ | |
[None, None, 1, 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, None, None, None], | |
] | |
を返す。 | |
""" | |
month = [] | |
week = [] | |
for _ in xrange(wday): | |
week.append(None) | |
for i in xrange(1, ndays+1): | |
week.append(i) | |
if len(week) == 7: | |
month.append(week) | |
week = [] | |
if week: | |
for _ in xrange(7 - len(week)): | |
week.append(None) | |
month.append(week) | |
return month | |
def render_month(month): | |
""" | |
1ヶ月分のデータを受け取り、HTMLテーブルを返す。 | |
""" | |
buf = [] | |
buf.append('''<table> | |
<thead> | |
<th>Mon</th> | |
<th>Tue</th> | |
<th>Wed</th> | |
<th>Thu</th> | |
<th>Fri</th> | |
<th>Sat</th> | |
<th>Sun</th> | |
</thead> | |
<tbody> | |
''') | |
for week in month: | |
buf.append( ' <tr>\n') | |
for day in week: | |
s = '' if day is None else str(day) | |
buf.extend((' <td>', s, '</td>\n')) | |
buf.append( ' </tr>\n') | |
buf.append( ''' </tbody> | |
</table> | |
''') | |
return "".join(buf) | |
def print_calendar_month(ndays, wday): | |
""" | |
1ヶ月分のカレンダーを出力する。 | |
""" | |
month = build_month(ndays, wday) | |
html = render_month(month) | |
print(html) | |
if __name__ == '__main__': | |
print_calendar_month(30, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment