473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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,observed,args,kwargs):
for reaction in self.get(observed,()):
reaction(*args,**kwargs)

def emit(self,method,observed=None):
''' A decorator for functions to signal their calling.Post hook
cablated'''
def wrapper(*args,**kwargs):
if observed:
if type(observed) is types.MethodType:
event=observed.im_func
else:
event=observed
else:
event=wrapper
result=method(*args,**kwargs)
self(wrapper,args,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(method,observed)
return wrapEmit

def reactOn(self,*observeds):
''' 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.MethodType:
observed=observed.im_func
self.setdefault(observed,set()).add(method)
return method
return reaction
if __name__=='__main__':
observer=Observer()
class base(object):
@observer.emit
def method(self,*args,**kwargs):
print '%s.method'%str(self),args,kwargs

class extensionA(object):
@observer.reactOn(base.method)
def __methodReaction(self,*args,**kwargs):
print 'A,%s.methodReaction'%str(self),args,kwargs

class extensionB(object):
@observer.reactOn(base.method)
def __methodReaction(self,*args,**kwargs):
print 'B,%s.methodReaction'%str(self),args,kwargs

class applicable(base,extensionA,extensionB):
@observer.reactOn(base.method)
def __methodReaction(self,*args,**kwargs):
print 'applicable,%s.methodReaction'%str(self),args,kwar gs

pass

applicable().method('cucu')
___________________________________
Yahoo! Messenger: chiamate gratuite in tutto il mondo
http://it.beta.messenger.yahoo.com
Aug 18 '05 #1
0 1348

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

Similar topics

0
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
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...
4
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....
0
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...
22
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....
4
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)...
1
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...
4
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...
5
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
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,...

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.