Python: HTMLCalendar 中 formatweek() 方法中的 'theweek' 参数是什么?
Python: What is 'theweek' parameter in the formatweek() method in HTMLCalendar?
我正在 Python/Django 中制作一个网络应用程序,我正在尝试使用日历模块中的 HTMLCalendar class 制作一个显示会议的日历。
我正在覆盖 HTMLCalendar 中的 formatday()
、formatweek()
和 formatmonth()
方法。
我已经为 formatday()
函数编写了新代码。我找到了 formatweek()
函数的两个代码示例,演示了它的用法:
# Sample 1
def formatweek(self, theweek, events):
week = ''
for d, weekday in theweek:
week += self.formatday(d, events)
return f'<tr> {week} </tr>'
# Sample 2
def formatweek(self, theweek, width):
"""
Returns a single week in a string (no newline).
"""
return ' '.join(self.formatday(d, wd, width) for (d, wd) in theweek)
我不确定 theweek
参数到底是什么。我在网上搜索了 formatweek()
的文档,但找不到任何概述 theweek
参数是什么的内容。任何见解表示赞赏。
从 calendar
模块的源代码来看,似乎 formatweek
需要一个 (day number, weekday number)
元组数组作为 theweek
参数的值。
您可以为此使用 monthdatescalendar
API。根据函数文档字符串,
This will Return a matrix representing a month's calendar.Each row
represents a week; week entries are (day number, weekday number)
tuples. Day numbers outside this month are zero.
>>> from calendar import TextCalendar
>>> cal = TextCalendar()
>>> for week in cal.monthdays2calendar(2019, 11):
... print(cal.formatweek(week, 10))
...
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
我正在 Python/Django 中制作一个网络应用程序,我正在尝试使用日历模块中的 HTMLCalendar class 制作一个显示会议的日历。
我正在覆盖 HTMLCalendar 中的 formatday()
、formatweek()
和 formatmonth()
方法。
我已经为 formatday()
函数编写了新代码。我找到了 formatweek()
函数的两个代码示例,演示了它的用法:
# Sample 1
def formatweek(self, theweek, events):
week = ''
for d, weekday in theweek:
week += self.formatday(d, events)
return f'<tr> {week} </tr>'
# Sample 2
def formatweek(self, theweek, width):
"""
Returns a single week in a string (no newline).
"""
return ' '.join(self.formatday(d, wd, width) for (d, wd) in theweek)
我不确定 theweek
参数到底是什么。我在网上搜索了 formatweek()
的文档,但找不到任何概述 theweek
参数是什么的内容。任何见解表示赞赏。
从 calendar
模块的源代码来看,似乎 formatweek
需要一个 (day number, weekday number)
元组数组作为 theweek
参数的值。
您可以为此使用 monthdatescalendar
API。根据函数文档字符串,
This will Return a matrix representing a month's calendar.Each row represents a week; week entries are (day number, weekday number) tuples. Day numbers outside this month are zero.
>>> from calendar import TextCalendar
>>> cal = TextCalendar()
>>> for week in cal.monthdays2calendar(2019, 11):
... print(cal.formatweek(week, 10))
...
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