python-observable library lets Python developers build loosely coupled object meshes with a complete Observer/Observable implementation.
In software development literature, Observer/Observable is a software
pattern. From an Observer/Observable perspective, there are two kinds
- objects which experience events or state changes (Observables)
- objects which need to know about other objects' events (Observers)
If you have any experience with event-based programming, such as with
toolkits like GTK+ or Qt, you'll be familiar with the Observer/Observable
pattern. Observer objects register themselves with the Observable object
of their interest, and when Observables experience events, these events
are relayed to the Observers.
Observer/Observable lets software developers build loosely coupled object meshes. What this means to you: your software will be more stable and easier to refactor.
This module has a complete Observer/Observable implementation. To take advantage of this module, all you have to do is:
- Make your Observable classes inherit from Observable (don't forget to call Observable.__init__(self) in the constructor)
- Make your Observer classes inherit from Observer (don't forget to call Observer.__init__(self) in the constructor)
- At runtime, for each object you want to observe, call self.observe(observable_object) in a method of your Observer object.
- In your Observable object, every time an event happens, call:
- In your Observer object, implement a method named: def processEvent(self,notifierObject,eventName,*args): which will receive the notifier object, an event name, and a variable number of arguments, every time broadcastEvent is called in any of the Observable objects that have been observe'd()
That's it. Keep in mind that processEvent() will be invoked in the same thread context as the code which called broadcastEvent() in the Observable object. Thus, processEvent() methods should return quickly and never do blocking operations (suggested technique for coping with blocking operations: run them in a separate thread, which sleeps until a flag is raised, and raise the flag in the processEvent() method as appropriate).
· Python 2.2 or later
· Python 2.2 or later