Package NotificationFramework
|
|
Package NotificationFramework
The NotificationFramework is a python implementation of the "Observer"
Design Pattern, allowing one-to-many dependency between objects even when
they have no idea of who and where the other objects live.
The complete documentation can found in module NotificationCenter
.
It is distributed under a 3-clause BSD-style license;
see the LICENSE file for details.
Download the last release at sourceforge
Example of use:
>>> from NotificationFramework import NotificationCenter as NC
>>> SUBJECT_CHANGED='Subject changed'
>>> class Subject:
... value=0
... def __init__(self, name):
... self.name=name
... def change_and_notify(self, value):
... self.value=value
... NC.postNotification(SUBJECT_CHANGED, self, info=value)
... def __str__(self):
... return self.name
>>> class Observer:
... def __init__(self, name):
... self.name=name
...
... def handle_notification(self, notification):
... print self.name, 'received notification:', str(notification)
... print ' with info:', notification.userInfo()
...
>>> s1=Subject('s1')
>>> s2=Subject('s2')
>>> observ1, observ2 = Observer('observ1'), Observer('observ2')
>>> generic = Observer('generic')
>>>
... NC.addObserver(observ1, Observer.handle_notification, SUBJECT_CHANGED, s1)
>>>
... NC.addObserver(observ2, Observer.handle_notification, SUBJECT_CHANGED, s2)
>>>
... NC.addObserver(generic, Observer.handle_notification, SUBJECT_CHANGED)
>>> s1.change_and_notify(value='hop')
observ1 received notification: <Notification name:'Subject changed' object:'s1'>
with info: hop
generic received notification: <Notification name:'Subject changed' object:'s1'>
with info: hop
>>> s2.change_and_notify(value=3)
observ2 received notification: <Notification name:'Subject changed' object:'s2'>
with info: 3
generic received notification: <Notification name:'Subject changed' object:'s2'>
with info: 3
Hosted by: