File: console_listeners.py

package info (click to toggle)
python-pypubsub 4.0.3-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 928 kB
  • sloc: python: 4,884; makefile: 156; xml: 16; php: 2
file content (39 lines) | stat: -rw-r--r-- 1,008 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""

:copyright: Copyright since 2006 by Oliver Schoenborn, all rights reserved.
:license: BSD, see LICENSE.txt for details.

"""

from pubsub import pub


# ------------ create some listeners --------------

class Listener:
    def onTopic11(self, msg, extra=None):
        print('Method Listener.onTopic11 received: ', repr(msg), repr(extra))

    def onTopic1(self, msg, topic=pub.AUTO_TOPIC):
        info = 'Method Listener.onTopic1 received "%s" message: %s'
        print(info % (topic.getName(), repr(msg)))

    def __call__(self, **kwargs):
        print('Listener instance received: ', kwargs)


listenerObj = Listener()


def listenerFn(msg, extra=None):
    print('Function listenerFn received: ', repr(msg), repr(extra))


# ------------ subscribe listeners ------------------

pub.subscribe(listenerObj, pub.ALL_TOPICS)  # via its __call__

pub.subscribe(listenerFn, 'topic1.subtopic11')
pub.subscribe(listenerObj.onTopic11, 'topic1.subtopic11')

pub.subscribe(listenerObj.onTopic1, 'topic1')