[PEAK] Using peak.model as an observer subject

Doug Quale quale1 at charter.net
Wed Sep 1 22:11:12 EDT 2004


"Phillip J. Eby" <pje at telecommunity.com> writes:

> Looks pretty good to me.  Personally, I'd use peak.events "event
> source" objects such as
> 'events.Broadcaster' instances rather than creating a new callback
> facility.  This would eliminate the need for the _callbacks dictionary
> and a fair bit of logic; i.e. something like:
> 
>      class ObservableAttr(model.Attribute):
>          newVerbs = [('get_event', '%(singularName)sEvent')]
> 
>          def get_event(feature, element):
>              key = feature.attrName+'__event'
>              try:
>                  return feature.__dict__[key]
>              except KeyError:
>                  return feature.__dict__.setdefault(key,events.Broadcaster())
> 
>          def _onLink(feature, element, item, posn=None):
>              feature.get_event(element).send(...)
> 
> Then, to subscribe to a feature, you'd do:
> 
>      anElement.someFeatureNameEvent().addCallback(callback)
> 
> Of course, this only calls 'callback' once; you'd probably use:
> 
>      source = anElement.someFeatureNameEvent()
>      unsubscribe = events.subscribe(source, callback)
> 
> which handles weak referencing for you, and automatically re-adds the
> callback each time the event fires.

Thanks.  This is very nice.  I'll use it.

I have a Python 101 type question:  What is the advantage of using
key = feature.attrName+'__event' instead of key = '__event'?



More information about the PEAK mailing list