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

时间:2010-10-02 00:13:52

标签: python calendar

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

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 对我来说,直接思考已经​​为时已晚,而且我已经围绕这个问题跳了一个多小时。

提前致谢,
的Jakub

3 个答案:

答案 0 :(得分:1)

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

像这样:

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")                                                                            

答案 1 :(得分:0)

已更新以按照Aaron在评论中的建议使用types.MethodType

尝试:

import types
c.formatday = types.MethodType(ext_formatday, c, calendar.HTMLCalendar)

请参阅types module文档。要知道它失败的原因:

In [53]: class A(object):
   ....:     def foo(self): pass

In [54]: def bar(self): pass

In [55]: a = A()

In [56]: a.foo
Out[56]: <bound method A.foo of <__main__.A object at 0x030D4770>>

In [57]: a.foo = bar

In [58]: a.foo
Out[58]: <function bar at 0x030C3EB0>

In [59]: aa = A()

In [60]: aa.foo.im_class, aa.foo.im_func, aa.foo.im_self
Out[60]:
(<class '__main__.A'>,
 <function foo at 0x030EE6F0>,
 <__main__.A object at 0x030D4910>)

In [61]: a.foo.im_class
AttributeError: 'function' object has no attribute 'im_class'

答案 2 :(得分:0)

您不想替换这些方法; Alan Hynes所建议的是子类 HTMLCalendar:

class MyCustomCalendar(calendar.HTMLCalendar):

    def formatday(self, day, weekday, *notes):
        ...

    def formatweek(self, theweek, *notes):
        ...

c = MyCustomCalendar(calendar.MONDAY)

这将创建一个新的派生类(MyCustomCalendar),它继承所有HTMLCalendar的方法和属性,但定义了自己的formatdayformatweek版本。

您可以在Python教程或网络上的其他地方阅读有关Inheritance的更多信息。它是Python(以及一般的面向对象编程)中的一个重要工具,并且围绕它设计了许多库。