覆盖_open方法

时间:2017-09-15 15:34:19

标签: python python-3.x logging

这是一个例子,这里是内置python Logging的代码片段:

from skpy import Skype

username = '<your username>'
password = '<your password>'

sk = Skype(username, password) # connect to Skype

# to go from id to url
chat_id = '<skype chat id>'
ch = sk.chats.chat(chat_id)
join_url = ch.joinUrl

# to go from url to id
join_url = '<skype url to get id>'
chat_id = sk.chats.urlToIds(join_url)

我试图覆盖class StreamHandler(Handler) def __init__(self, stream=None): Handler.__init__(self) self.stream = stream class FileHandler(StreamHandler): def __init__(self, filename, mode='a'): StreamHandler.__init__(self, self._open()) def _open(self): stream = open(self.name, self.mode) return stream class MyFileHandler(FileHandler) def _open(self): stream = open(self.name, self.mode, 0) return stream 函数。所以我在上面做了,但没有工作。在上述情况下如何覆盖_open

2 个答案:

答案 0 :(得分:0)

我不知道你从哪里获得这些课程。在Filehandler模块中使用的logginglogging.__init__中定义,在其__init__中设置了您应在_open中使用的几个属性呼叫。具体为self.baseFilename,即要使用的文件的名称。

您的班级_open是唯一实施的内容,应如下所示:

from logging import FileHandler

class MyFileHandler(FileHandler):
    def _open(self):
        stream = open(self.baseFilename, self.mode, 0)

并且由于您将缓冲设置为0,因此您应该提供一种以二进制方式打开它的模式。

答案 1 :(得分:0)

感谢。 对不起代码是片段。我从日志记录模块导入。

class FileHandler(StreamHandler):
    def __init__(self, filename, mode='a'):
        StreamHandler.__init__(self, self._open())    <-- 

我想覆盖传递 init

的流(第二个参数)