473,560 Members | 3,045 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An observer pattern application.

Lately I was needing to use multiple inheritance to split behaviour of a
class and modularize it.
But the problem raises when the need is to add operations to a method
already present in one of them from another.
I think the technical solution is the use of 'super'.
Then I tried to write a decorator for automatize the 'super' call,but I
failed.

Next solution is implementing the observer pattern on methods call.
I'm pretty sure there are bugs and ideas to be corrected in the next
code,any help and comment appreciated.

Regards Paolino

############### ############### ############### ############### ############### #############

''' A module collecting classes for known patterns

'''

import types

class Observer(dict):
''' A class defining some decorators for function/methods to
chain/link them when called .To do:implement pre event execution hooking'''

def __call__(self,o bserved,args,kw args):
for reaction in self.get(observ ed,()):
reaction(*args, **kwargs)

def emit(self,metho d,observed=None ):
''' A decorator for functions to signal their calling.Post hook
cablated'''
def wrapper(*args,* *kwargs):
if observed:
if type(observed) is types.MethodTyp e:
event=observed. im_func
else:
event=observed
else:
event=wrapper
result=method(* args,**kwargs)
self(wrapper,ar gs,kwargs)
return result
return wrapper

def emitOther(self, observed):
''' A decorator facility to let the function/method emit another
event (not itself)'''
def wrapEmit(method ):
return self.emit(metho d,observed)
return wrapEmit

def reactOn(self,*o bserveds):
''' a decorator to set the function/method as a reaction to
*observeds event.
Remember to use __ name mangling when working on methods to be
able to use
same reaction name on multiple class inheritances'''
def reaction(method ):
for observed in observeds:
if type(observed) is types.MethodTyp e:
observed=observ ed.im_func
self.setdefault (observed,set() ).add(method)
return method
return reaction
if __name__=='__ma in__':
observer=Observ er()
class base(object):
@observer.emit
def method(self,*ar gs,**kwargs):
print '%s.method'%str (self),args,kwa rgs

class extensionA(obje ct):
@observer.react On(base.method)
def __methodReactio n(self,*args,** kwargs):
print 'A,%s.methodRea ction'%str(self ),args,kwargs

class extensionB(obje ct):
@observer.react On(base.method)
def __methodReactio n(self,*args,** kwargs):
print 'B,%s.methodRea ction'%str(self ),args,kwargs

class applicable(base ,extensionA,ext ensionB):
@observer.react On(base.method)
def __methodReactio n(self,*args,** kwargs):
print 'applicable,%s. methodReaction' %str(self),args ,kwargs

pass

applicable().me thod('cucu')
_______________ _______________ _____
Yahoo! Messenger: chiamate gratuite in tutto il mondo
http://it.beta.messenger.yahoo.com
Aug 18 '05 #1
0 1355

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7024
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
3
2618
by: Michael Schneider | last post by:
Hello All, I am comming back to python after being away for several years. I would like to use weak refs in an observer pattern implementation. The problme that I have seems to be that weakrefs can't manage functions. ------------------- from docs: http://www.python.org/doc/current/lib/module-weakref.html
4
1923
by: decrypted | last post by:
Since I couldn't find a OO design/architext forum, I thought I would post here... I have a win app with forms management. forms are grouped by category (pertains to company, pertains to project. etc). This is represented, for example, by a company window containing additional windows such as company information, document history, yada yada....
0
1615
by: FluffyCat | last post by:
Last week I continued my series of design patterns examples using PHP 5. Here now is my 14th example, the Observer pattern. http://www.fluffycat.com/PHP-Design-Patterns-Observer/ In the Observer Pattern, a Subject object notifies an Observer object if it's the Subject object's state changes. Pretty simple and fairly useful.
22
4698
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer. Also describes implementation via ChangeManager (Mediator + Singleton) 2) Pattern hatching by John Vlissides Describes Observer's implementation via...
4
4664
by: Gianni Mariani | last post by:
Two issues here: a) What is the accepted definition of "observer pattern". While I can't point to anything specific, I do remember having issues with inconsistency in the definition. b) Generic observer design in C++. I have been pushing the Austria "Twin" interface for a while and more recently the multi threaded version of it...
1
1866
by: Christopher | last post by:
The observer pattern itself is easy enough. I've implemented it using a Event that contains data for any Event type I forsee my application using. My problem is I don't want one and only one general purpose type of Event. I want to write my design in such a way that more Event Types can be created and used down the road as needed. How can you...
4
2106
by: Mohamed Mansour | last post by:
Hello, What is the purpose of implementing the Observer Pattern if we can trigger an event easily? For example (from books), You have a "Forecaster" which notifies "Observable" when a prediction is ready, then there is a "WeatherViewer" which calls methods from the "Observer Interface".
5
1469
by: Alan Isaac | last post by:
I have two questions about the "observer pattern" in Python. This is question #1. (I'll put the other is a separate post.) Here is a standard example of the observer pattern in Python:
0
7841
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8066
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7920
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6191
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3605
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2047
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1169
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.