~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to wxPython/samples/pubsub/basic_v1/main.py

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''
 
2
This example demonstrates how to use the arg1 messaging protocol, in the
 
3
original pubsub version 1.
 
4
 
 
5
There are several listeners created for three topics: topic1, topic2, and 
 
6
"everything". A couple of functions are created that send messages. 
 
7
 
 
8
The main constraints are:
 
9
 
 
10
- only the second argument in the sendMessage() can carry message 
 
11
  data; extra named (i.e. optional) parameters are possible
 
12
  but will never receive data via pubsub. To send more data,
 
13
  you would have to package it into a tuple or a class instance 
 
14
  and pass it as second argument. 
 
15
- all topics along a topic branch get the same data; this doesn't 
 
16
  make much sense in general since typically only subtopics need extra 
 
17
  data, the parent topics wouldn't know what to do with the extra 
 
18
  info. 
 
19
- listeners must be callable with one argument, no more and 
 
20
  no less. The argument name can be anything. 
 
21
 
 
22
:copyright: Copyright 2006-2009 by Oliver Schoenborn, all rights reserved.
 
23
:license: BSD, see LICENSE.txt for details.
 
24
 
 
25
'''
 
26
 
 
27
from pubsub import setupv1
 
28
from pubsub import Publisher as pub
 
29
 
 
30
# ------------ create some listeners --------------
 
31
 
 
32
def listener1(msg, extra=None):
 
33
    print 'Function listener1 received', msg.data
 
34
 
 
35
class Listener:    
 
36
    def onTopic1(self, msg, extra=None):
 
37
        print 'Method Listener.onTopic1 received', `msg.data`
 
38
    def onTopic2(self, msg):
 
39
        print 'Method Listener.onTopic2 received', `msg.data`
 
40
    def __call__(self, msg):
 
41
        print 'Listener() received', `msg.data`
 
42
 
 
43
# ------------ register listeners ------------------
 
44
 
 
45
pub.subscribe(listener1, 'topic1')
 
46
 
 
47
listener2 = Listener()
 
48
pub.subscribe(listener2) # ALL topics!
 
49
pub.subscribe(listener2.onTopic1, 'topic1')
 
50
pub.subscribe(listener2.onTopic2, 'topic2')
 
51
 
 
52
# ------------ create a couple of senders --------------
 
53
 
 
54
def doSomething1():
 
55
    print '--- SENDING topic1 message ---'
 
56
    pub.sendMessage('topic1', 'message1')
 
57
    print '---- SENT topic1 message ----'
 
58
    
 
59
def doSomething2():
 
60
    print '--- SENDING topic2 message ---'
 
61
    pub.sendMessage('topic2', 123)
 
62
    print '---- SENT topic2 message ----'
 
63
    
 
64
# --------- define the main part of application --------
 
65
 
 
66
def run():
 
67
    '''Loop until we get a quit message or user breaks.'''
 
68
    doSomething1()
 
69
    doSomething2()
 
70
    print 'done'
 
71
 
 
72
 
 
73
if __name__ == '__main__':
 
74
    run()
 
75
    
 
 
b'\\ No newline at end of file'