gpt4 book ai didi

Python:替换日历模块中的方法

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:10 24 4
gpt4 key购买 nike

我正在尝试替换日历模块中的两种方法:

import calendar


c = calendar.HTMLCalendar(calendar.MONDAY)
def ext_formatday(self, day, weekday, *notes):
if day == 0:
return '<td class="noday">&nbsp;</td>'
if len(notes) == 0:
return '<td class="%s">%d<br /></td>' % (self.cssclasses[weekday], day)
else:
return '<td class="%s">%d<br />%s</td>' % (self.cssclasses[weekday], day, notes)

def ext_formatweek(self, theweek, *notes):
if len(notes) == 0:
s = ''.join(self.formatday(d, wd) for (d, wd) in theweek)
else:
s = ''.join(self.formatday(d, wd, notes) for (d, wd) in theweek)
return '<tr>%s</tr>' % s

c.formatday = ext_formatday
c.formatweek = ext_formatweek

print c.formatmonth(2012,1,"foobar")

这行不通 - 有人可以指点我相关文献或指出我做错了什么吗?我正在尝试从以下线程中实现 Alan Hynes 的建议:thread现在对我来说已经太晚了,我已经围绕这个问题思考了一个多小时。

提前致谢,
雅各布

最佳答案

尝试替换类中的方法而不是实例。

像这样:

import calendar                                                                                                 


def ext_formatday(self, day, weekday, *notes):
if day == 0:
return '<td class="noday">&nbsp;</td>'
if len(notes) == 0:
return '<td class="%s">%d<br /></td>' % (self.cssclasses[weekday], day)
else:
return '<td class="%s">%d<br />%s</td>' % (self.cssclasses[weekday], day, notes)

def ext_formatweek(self, theweek, *notes):
if len(notes) == 0:
s = ''.join(self.formatday(d, wd) for (d, wd) in theweek)
else:
s = ''.join(self.formatday(d, wd, notes) for (d, wd) in theweek)
return '<tr>%s</tr>' % s

calendar.HTMLCalendar.formatday = ext_formatday
calendar.HTMLCalendar.formatweek = ext_formatweek

c = calendar.HTMLCalendar(calendar.MONDAY)
print c.formatmonth(2012,1,"foobar")

关于Python:替换日历模块中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3843800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com