473,390 Members | 1,329 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,390 software developers and data experts.

weakrefs to functions for observer pattern

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/li...e-weakref.html

Not all objects can be weakly referenced; those objects which can
include class instances, functions written in Python (but not in C),
methods (both bound and unbound), sets, frozensets, file objects,
generators, type objects, DBcursor objects from the bsddb module,
sockets, arrays, deques, and regular expression pattern objects. Changed
in version 2.4: Added support for files, sockets, arrays, and patterns.

-------------------------------------------------------

Is there a technique that I can use to leverage weak references to I
don't have to unregister my observers?

Thanks
Mike

PS. here is the code that I have been working with (Note: I commendout
out the weak ref creation.

--------------------------------------------------------------------
import types
class Observable(object):

def addObserver(self, observer, events=None):
if not hasattr(self, '_observers'):
#self._observers = weakref.WeakKeyDictionary()
self._observers = {}

if observer is None:
return

if events is not None and type(events) not in (types.TupleType,
types.ListType):
events = (events,)
self._observers[observer] = events

def removeObserver(self, callable):
if not hasattr(self, '_observers'):
return

if self._observers.has_key(callable):
del self._observers[callable]

##
# Notify all currently-registered Observers.
#
# This observer will be called if the event is one that the
# Observer is interested in, or if event is 'None'
#
# @param event The event to notify the Observers about. None
# means no specific event.
#
# *args - standard arguments - passed through to observer
# **kw - keyword arguments - passed through to observer
def notifyObservers(self, event=None, *args, **kw):

if not hasattr(self, '_observers'):
return

for cb, events in self._observers.items():
if events is None or event is None or event in events:
if cb is not None:
cb(self, event, *args, **kw)
Nov 3 '05 #1
3 2606
Michael Schneider <mi**************@fuse.net> wrote:
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.
They can manage just fine functions written in *Python*, just not
"builtin functions*, i.e., ones written in *C*. Just wrap any builtin
function you need to register as observer into a tiny Python-coded
wrapper and live happily ever after.
... Not all objects can be weakly referenced; those objects which can
include class instances, functions written in Python (but not in C),

Alex
Nov 3 '05 #2
Alex Martelli wrote:
Michael Schneider <mi**************@fuse.net> wrote:

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.

They can manage just fine functions written in *Python*, just not
"builtin functions*, i.e., ones written in *C*. Just wrap any builtin
function you need to register as observer into a tiny Python-coded
wrapper and live happily ever after.
...
Not all objects can be weakly referenced; those objects which can
include class instances, functions written in Python (but not in C),


Alex

Alex,

Thank you, I mis-read the docs.

The mistake I made was having was using a weak reference as a key in
the dictionary.

Weak references will be very useful for me.

I really enjoy python. So many good things have been added to the
language without taking the fun away :-)

Mike

--
The greatest performance improvement occurs on the transition of from
the non-working state to the working state.
Nov 3 '05 #3
Michael Schneider <mi**************@fuse.net> wrote:
...
Thank you, I mis-read the docs.

The mistake I made was having was using a weak reference as a key in
the dictionary.
Rather than using a weakref.WeakKeyDictionary , I guess?

Weak references will be very useful for me.

I really enjoy python. So many good things have been added to the
language without taking the fun away :-)


Glad to hear this!
Alex
Nov 3 '05 #4

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
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....
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
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)...
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:
5
by: Alan Isaac | last post by:
I have two questions about the "observer pattern" in Python. This is question #2. (I'll put the other is a separate post.) Consider this standard example of the observer pattern in Python:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...

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.