在Python中代理一个类

2024-06-26 00:20:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python-mpd2模块来控制GUI应用程序中Raspberry Pi上的媒体播放器。因此,我希望在后台优雅地处理连接错误和超时(问题中的播放器在60秒后丢弃MPD连接)。但是,MPD模块没有一个入口点,通过它可以发送所有命令或检索我可以修补的信息。在

我想要一个类,它允许访问与mpd.mpd客户机,但让我添加我自己的错误处理。换句话说,如果我这么做了:

client.play()

然后抛出一个connectione错误,我想捕捉它并重新发送相同的命令。除了必须重新连接到服务器造成的小延迟之外,用户不应该注意到有任何问题。在

到目前为止,这是我想出的解决办法。它在我的应用程序中起作用了,但并不能真正实现我的目标。在

^{pr2}$

我可以为每个MPD命令向这个类添加一个方法,例如:

^{3}$

但这似乎远不是实现这一目标的最佳方式。在


Tags: 模块命令信息应用程序目标错误pigui
1条回答
网友
1楼 · 发布于 2024-06-26 00:20:09

如果您不介意创建一个包含所有91个字符串的列表,这些字符串构成了命令名,那么您可以按照this answer的行进行操作。我相信这种方法有很多优点,因为它所涉及的魔法较少。在

奥托,91岁确实很多。下面是一个自动解决方案,使用自定义的__getattr__,它返回一个包装:

from functools import partial
import types

class DummyClient(object):
    def connect(self, *a, **kw): print 'connecting %r %r' % (a, kw)
    def play(self): print 'playing'
    def stop(self): print 'stopping'

class PersistentMPDClient(object):
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.client = DummyClient()
        self.client.connect(self.host, self.port)

    def __getattr__(self, attr, *args):
        cmd = getattr(self.client, attr, *args)
        if isinstance(cmd, types.MethodType):
            # a method   wrap
            return lambda *a, **kw: self.command(attr, *a, **kw)
        else:
            # anything else   return unchanged
            return cmd

    def command(self, cmd, *args, **kwargs):
        command_callable = partial(self.client.__getattribute__(cmd), *args, **kwargs)
        try:
            return command_callable()
        except ConnectionError:
            # Mopidy drops our connection after a while, so reconnect to send the command
            self.client._soc = None
            self.client.connect(self.host, self.port)
            return command_callable()

c = PersistentMPDClient(hostname, port)
c.play()
c.stop()

在我写这篇文章的时候,我注意到@MatToufoutu也发布了一个类似的解决方案(尽管有一些不同)。我不知道为什么他/她删除了它。。。如果这个答案没有被删除,我很乐意给予它应有的信任。

相关问题 更多 >