title: Python Observer Pattern: Examples, Tips?
body: <p>Are there any exemplary examples of the GoF Observer implemented in Python?  I have a bit code which currently has bits of debugging code laced through the key class (currently generating messages to stderr if a magic env is set). Additionally, the class has an interface for incrementally return results as well as storing them (in memory) for post processing.  (The class itself is a job manager for concurrently executing commands on remote machines over ssh).</p>
  
  <p>Currently the usage of the class looks something like:</p>
  
  <pre><code>job = SSHJobMan(hostlist, cmd)
  job.start()
  while not job.done():
      for each in job.poll():
          incrementally_process(job.results[each])
          time.sleep(0.2) # or other more useful work
  post_process(job.results)
  </code></pre>
  
  <p>An alernative usage model is:</p>
  
  <pre><code>job = SSHJobMan(hostlist, cmd)
  job.wait()  # implicitly performs a start()
  process(job.results)
  </code></pre>
  
  <p>This all works fine for the current utility.  However it does lack flexibility.  For example I currently support a brief output format or a progress bar as incremental results, I also support
  brief, complete and "merged message" outputs for the <code>post_process()</code> function.</p>
  
  <p>However, I'd like to support multiple results/output streams (progress bar to the terminal, debugging and warnings to a log file, outputs from successful jobs to one file/directory, error messages and other results from non-successful jobs to another, etc).</p>
  
  <p>This sounds like a situation that calls for Observer ... have instances of my class accept registration from other objects and call them back with specific types of events as they occur.</p>
  
  <p>I'm looking at <a href="http://pypi.python.org/pypi/PyPubSub/" rel="noreferrer">PyPubSub</a> since I saw several references to that in SO related questions.  I'm not sure I'm ready to add the external dependency to my utility but I could see value in using their interface as a model for mine if that's going to make it easier for others to use.  (The project is intended as both a standalone command line utility and a class for writing other scripts/utilities).</p>
  
  <p>In short I know how to do what I want ... but there are numerous ways to accomplish it.  I want suggestions on what's most likely to work for other users of the code in the long run.</p>
  
  <p>The code itself is at: <a href="http://bitbucket.org/jimd/classh" rel="noreferrer">classh</a>.</p>