Notifications? 
Author Message
 Notifications?

There's something I'd like to do with my classes but don't know how to go
about doing this -- please forgive my wording here, I don't know how else to
phrase the question.

Basically, I want to one class to "register" itself with another for
notifications on certain events (example the calling of a specific method,
or a change in a class variable). When the source class executes the method
I want the sink class to have some method called ... giving it a
notification -- maybe even passing in any arguments passed into that method.
In some sense, I'd like to do this generically and invisibly  -- without
having to alter the source class to support the notfications, but for my
purposes it's fine if that I have to add code to support it.

What are some strategies I can use to do this? Nothing in the standard set
of python source seems to apply to this case ... am I missing something.

Thanks,
-Saveen

--
The opinions expressed in this message are my own personal views
and do not reflect the official views of the Microsoft Corporation.



Sat, 01 Apr 2000 03:00:00 GMT  
 Notifications?

Quote:
>Basically, I want to one class to "register" itself with another for
>notifications on certain events (example the calling of a specific method,
>or a change in a class variable). When the source class executes the method
>I want the sink class to have some method called ... giving it a
>notification -- maybe even passing in any arguments passed into that method.
>In some sense, I'd like to do this generically and invisibly  -- without
>having to alter the source class to support the notfications, but for my
>purposes it's fine if that I have to add code to support it.

>What are some strategies I can use to do this? Nothing in the standard set
>of python source seems to apply to this case ... am I missing something.

There's been some recent discussions on this topic; try searching
for "observer" via the newsgroup search (*).

Cheers /F

*) at http://www.python.org/locator



Sun, 02 Apr 2000 03:00:00 GMT  
 Notifications?

Saveen,

Try Observer/Observable classes based on "Observer Pattern".
My implementation is found at:
http://www.skpl.com/employee/jun/python/

All the best,

Jun,

Quote:
>There's something I'd like to do with my classes but don't know how to go
>about doing this -- please forgive my wording here, I don't know how else to
>phrase the question.

>Basically, I want to one class to "register" itself with another for
>notifications on certain events (example the calling of a specific method,
>or a change in a class variable). When the source class executes the method
>I want the sink class to have some method called ... giving it a
>notification -- maybe even passing in any arguments passed into that method.
>In some sense, I'd like to do this generically and invisibly  -- without
>having to alter the source class to support the notfications, but for my
>purposes it's fine if that I have to add code to support it.

>What are some strategies I can use to do this? Nothing in the standard set
>of python source seems to apply to this case ... am I missing something.

>Thanks,
>-Saveen

//----------------------------------------------------------
// public final class Jun extends JunichiSuzuki {
//   public static final String
//   company = "Soken Planning Co., Ltd.",
//   title   = "Technical Director of Consulting Group,
//              Technical Assistant to OMG Representative in Japan",

//   web     = "http://www.skpl.com/,
//              http://www.omg.org/";
// }


Sun, 02 Apr 2000 03:00:00 GMT  
 Notifications?

  This is something called the Observer pattern in ubiquitous book
"Design Patterns" which keeps getting referenced here. Get it. Read it.
Love it. (page 293)

  The quick definition in the cover of the book states:

"Observer (p.293) Define a one-to-many dependency between objects so that
when one object changes state, all its dependents are notified and updated
automatically."

  Here are simple subject and observer base classes in python:

class Observer:
  def __init__(self):
    pass

  def Update(self, updated_subj):
    pass

class Subject:
  """Something that can be observed; contains a list of the attached observers"""
  def __init__(self):
    self.observers = []

  def Attach(self, attach_obs):
    self.observers.append(attach_obs)

  def Detach(self, detach_obs):
    for i in range(len(self.observers)-1,-1,-1):
      if self.observers[i] == detach_obs:
        del self.observers[i]

# this will delete multiple copies of the observer if there are any

  def Notify(self):
    for observer in self.observers:
      observer.Update(self)

  You'll have to subclass from these to do anything useful with them.
Something I did was to implement a webserver access log watcher,
for which each observer had a regex that matched an "interesting"
line in a log. A logfile class was derived from Subject, because it's
what the observers are interested in. When the logfile class is instantiated,
it runs a "tail -f" on the particular logfile, then each time a new
line is written to the file by the server process, it  calls Notify
on itself. This, in turn, calls Update() on each of the observers
that are subscribed to the logfile. In Update(), the new logfile entry
is matched against the observer's pattern. If it matches, the observer
does whatever it wants, like increment a hit count. Finally, any time
you're interested in the current stats, a summarize() method is
provided on the subject, which similarly to the Notify() method,
calls a summarize() on each observer, which prints out each one's
current stats.

  Got it? Maybe? I'd be glad to further expound on the subject if
you have further questions.

                                                yours,
                                                dave mitchell

Quote:

> There's something I'd like to do with my classes but don't know how to go
> about doing this -- please forgive my wording here, I don't know how else to
> phrase the question.

> Basically, I want to one class to "register" itself with another for
> notifications on certain events (example the calling of a specific method,
> or a change in a class variable). When the source class executes the method
> I want the sink class to have some method called ... giving it a
> notification -- maybe even passing in any arguments passed into that method.
> In some sense, I'd like to do this generically and invisibly  -- without
> having to alter the source class to support the notfications, but for my
> purposes it's fine if that I have to add code to support it.

> What are some strategies I can use to do this? Nothing in the standard set
> of python source seems to apply to this case ... am I missing something.

> Thanks,
> -Saveen

> --
> The opinions expressed in this message are my own personal views
> and do not reflect the official views of the Microsoft Corporation.



Sun, 02 Apr 2000 03:00:00 GMT  
 Notifications?

Quote:

> Basically, I want to one class to "register" itself with another for
> notifications on certain events (example the calling of a specific
method,
> or a change in a class variable). When the source class executes the
method
> I want the sink class to have some method called ... giving it a
> notification -- maybe even passing in any arguments passed into that
method.
> In some sense, I'd like to do this generically and invisibly  -- without
> having to alter the source class to support the notfications, but for my
> purposes it's fine if that I have to add code to support it.

You already have some responses, but let me point out that you might
use getattr and the argument catch-alls to implement this.

Roughly, here is an untested wrapper that passes on all method calls
except __init__ and __gettattr__

class wrapper:
   def __init__(self, contained):
        self.contained = contained
   def __getattr__(self, name):
        return attrwrap(self.contained, name)

class attrwrap:
   def __init__(self, thing, name):
        self.thing, self.name = thing, name
   def __call__(self, *args, **kw):
        print "method", self.name, "called", args, kw
        return apply(getattr(self.thing, self.name), args, kw)

Note that by modifying these wrappers you can do a lot of
tricks, maybe like the ones you have in mind.  It's easiest
with "pure method call" interfaces.  -- Aaron watters

===
http://starship.skyport.net/crew/aaron_watters/bplustree/term.cgi?352...



Tue, 04 Apr 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. image start/shutdown notification

2. DST Problems (Event Notification)

3. Date change notification in VW?

4. VM Change Notification

5. OS X Bouncing Error Notification

6. Notification Manager under OS X?

7. Notification Manager?

8. Can RealBasic post a Notification Popup?

9. Notification by sleep?

10. Notification of drop event?

11. notification of change of a class's method dictionary

12. Automatic notification by email

 

 
Powered by phpBB® Forum Software