The joys of weakrefs... references to instance methods (callbacks)

Mike C. Fletcher mcfletch at geocities.com
Thu Feb 14 02:03:40 EST 2002


Actually, with the work I just finished, I replaced all watcher 
instances with property objects and call dispatcher directly, so I no 
longer need the watchers at all :) .  The version up on the site is the 
old one, the new one is rather trivial (which is why I got rid of it in 
my code):

import dispatcher

class WatchAble:
	"""Mix-in class providing support for "watching" object changes
	
	Not currently thread-safe, should add that some day
	Use of _v_ prefix is to support use with ZODB persistence
	mechanisms.  It indicates that the list is volatile and should
	not be stored in the ZODB.
	"""
	### Customisation Point
	def beforeUpdateWatchers( self, **keywords):
		"""Called before any watchers are updated, lets you update object state 
before the watchers see changes"""

	### Public API
	def addWatcher( self, object, signal = dispatcher.Any ):
		"""Add a new watcher object, must be a callable object with signature:
			object( watchedObject, event )
		Where watchedObject is this instance, and event
		is the opaque parameter passed to updateWatchers.
		"""
		dispatcher.connect( object, signal, self )
	def removeWatcher( self, object, signal = dispatcher.Any ):
		"""Remove a particular watcher from the list of watchers"""
		dispatcher.disconnect( object, signal, self )
	def updateWatchers( self, signal = dispatcher.Any, **keywords ):
		"""Send event to each watcher of the object"""
		self.beforeUpdateWatchers( signal=signal, **keywords)
		dispatcher.send( signal, self, **keywords)

As you can see, no real benefit beyond the interception before the 
message is sent, and conceptually, that should (in my case) be handled 
by the assignment of property values, not by a pre-messaging hack.  Of 
course, the pattern does make the watcher reference local to the object, 
rather than global, but that's just a coding preference, and not 
important to me.

Anywho, need to get back to work. This whole Python 
2.2-on-ZODB-with-automatic-event-generation excursion has sucked up 
almost 4 full days of working time.

Enjoy,
Mike


Patrick K. O'Brien wrote:
> Thanks. Is your new watcher class available? I couldn't tell if this one
> (http://members.rogers.com/mcfletch/programming/watchable.py) was the newly
> rewritten version or not.
> 
> The dispatcher module is part of a project that is very alpha, called
> Bulldozer. You can find the files in CVS on SourceForge:
> 
> http://sourceforge.net/projects/bdoz/
> 
> The license is the same as the Python license. As Magnus mentioned, there
> were several implementations developed as part of the anygui project. We all
> learned from each other, but still came up with different approaches.
> 
> ---
> Patrick K. O'Brien
> Orbtech
...






More information about the Python-list mailing list