Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 501105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:09:20+00:00 2026-05-13T06:09:20+00:00

Are there any exemplary examples of the GoF Observer implemented in Python? I have

  • 0

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).

Currently the usage of the class looks something like:

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)

An alernative usage model is:

job = SSHJobMan(hostlist, cmd)
job.wait()  # implicitly performs a start()
process(job.results)

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 post_process() function.

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).

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.

I’m looking at PyPubSub 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).

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.

The code itself is at: classh.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:09:20+00:00Added an answer on May 13, 2026 at 6:09 am

    However it does lack flexibility.

    Well… actually, this looks like a good design to me if an asynchronous API is what you want. It usually is. Maybe all you need is to switch from stderr to Python’s logging module, which has a sort of publish/subscribe model of its own, what with Logger.addHandler() and so on.

    If you do want to support observers, my advice is to keep it simple. You really only need a few lines of code.

    class Event(object):
        pass
    
    class Observable(object):
        def __init__(self):
            self.callbacks = []
        def subscribe(self, callback):
            self.callbacks.append(callback)
        def fire(self, **attrs):
            e = Event()
            e.source = self
            for k, v in attrs.items():
                setattr(e, k, v)
            for fn in self.callbacks:
                fn(e)
    

    Your Job class can subclass Observable. When something of interest happens, call self.fire(type="progress", percent=50) or the like.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to change the speed at which the value is updated
Is there any built-in solution when a TabHost have a high number of tabs
There is any solution to add namespaces without prefix(i mean these ns0, ns1) which
Is there any means within MonoDevelop to create your own snippets, i.e. reusable bits
Are there any out of the box solutions to have conditional formatting of HTML
Is there any query which can return me the number of revisions made to
Is there any build-in function in Python that merges two lists into a dict?
Is there any way to have a custom recipe add some text and image
Is there any way to update cookies across folders in PHP? For example, consider
Is there any way to trigger a function that's called when the user types

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.