支持多频道订阅者的Python可观察实现

时间:2011-10-03 17:56:14

标签: python

在扭曲的应用程序中,我有一系列资源控制器/管理器类,它们通过Observable模式进行交互。通常大多数观察者都会订阅一个特定的频道(例如“foo.bar.entity2”),但在某些情况下,我想知道特定频道中的所有事件(例如“foo。*”),所以我写了类似下面的内容:

    from collections import defaultdict

    class SimplePubSub(object):

        def __init__(self):
            self.subjects = defaultdict(list)


        def subscribe(self, subject, callbackstr):
            """
                for brevity, callbackstr would be a valid Python function or bound method but here is just a string
            """
            self.subjects[subject].append(callbackstr)

        def fire(self, subject):
            """
                Again for brevity, fire would have *args, **kwargs or some other additional message arguments but not here            
            """

            if subject in self.subjects:
                print "Firing callback %s" % subject
                for callback in self.subjects[subject]:
                    print "callback %s" % callback



    pubSub = SimplePubSub()
    pubSub.subscribe('foo.bar', "foo.bar1")
    pubSub.subscribe('foo.foo', "foo.foo1")

    pubSub.subscribe('foo.ich.tier1', "foo.ich.tier3_1")
    pubSub.subscribe('foo.ich.tier2', "foo.ich.tier2_1")
    pubSub.subscribe('foo.ich.tier3', "foo.ich.tier2_1")

    #Find everything that starts with foo
    #say foo.bar is fired
    firedSubject = "foo.bar"
    pubSub.fire(firedSubject)
    #outputs 
    #>>Firing callback foo.bar
    #>>callback foo.bar1

    #but let's say I want to add a callback for everything undr foo.ich

    class GlobalPubSub(SimplePubSub):

        def __init__(self):
            self.globals = defaultdict(list)
            super(GlobalPubSub, self).__init__()


        def subscribe(self, subject, callback):
            if subject.find("*") > -1:
                #assumes global suscriptions would be like subject.foo.* and we want to catch all subject.foo's 
                self.globals[subject[:-2]].append(callback)
            else:
                super(GlobalPubSub, self).subscribe(subject, callback)

        def fire(self, subject):
            super(GlobalPubSub, self).fire(subject)
            if self.globals:
                for key in self.globals.iterkeys():
                    if subject.startswith(key):
                        for callback in self.globals[key]:
                            print "global callback says", callback

    print "Now with global subscriptions"
    print
    pubSub = GlobalPubSub()
    pubSub.subscribe('foo.bar', "foo.bar1")
    pubSub.subscribe('foo.foo', "foo.foo1")

    pubSub.subscribe('foo.ich.tier1', "foo.ich.tier3_1")
    pubSub.subscribe('foo.ich.tier2', "foo.ich.tier2_1")
    pubSub.subscribe('foo.ich.tier3', "foo.ich.tier2_1")
    pubSub.subscribe("foo.ich.*", "All the ichs, all the time!")

    #Find everything that starts with foo.ich
    firedSubject = "foo.ich.tier2"
    pubSub.fire(firedSubject)
    #outputs 
    #>>Firing callback foo.bar
    #>>callback foo.bar1
    #>>Now with global subscriptions
    #
    #>>Firing callback foo.ich.tier2
    #>>callback foo.ich.tier2_1
    #>>global callback says All the ichs, all the time!

如果没有诉诸某种异国情调的构造(例如尝试),这是不是很好?我正在寻找一个肯定,我在正确的轨道或更好的替代建议的全球订阅处理程序是纯python(没有外部库或服务)。

1 个答案:

答案 0 :(得分:1)

这看起来就像你走在正确的轨道上。我正在使用PyPubSub和wxPython应用程序,然后最终实现了我自己的“更简单”版本,从根本上看,它看起来与你在这里所做的非常相似,除了更多的铃声当你填写你的要求时,你可能最终会实施的口哨声。

给出here的答案也和你所做的一样。

这个answer进入的示例有点不同。

除了PyPubSub之外还有一些现有的库,例如pydispatchblinker,可能值得参考或参考。

相关问题