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

  • Home
  • SEARCH
  • 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 4054778
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:37:07+00:00 2026-05-20T14:37:07+00:00

Suppose i want to track the progress of a loop using the progress bar

  • 0

Suppose i want to track the progress of a loop using the progress bar printer ProgressMeter (as described in this recipe).

def bigIteration(collection):
    for element in collection:
        doWork(element)

I would like to be able to switch the progress bar on and off. I also want to update it only every x steps for performance reasons. My naive way to do this is

def bigIteration(collection, progressbar=True):
    if progressBar:
        pm = progress.ProgressMeter(total=len(collection))
        pc = 0
    for element in collection:
        if progressBar:
            pc += 1
            if pc % 100 = 0:
                pm.update(pc)
        doWork(element)

However, I am not satisfied. From an “aesthetic” point of view, the functional code of the loop is now “contaminated” with generic progress-tracking code.

Can you think of a way to cleanly separate progress-tracking code and functional code? (Can there be a progress-tracking decorator or something?)

  • 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-20T14:37:07+00:00Added an answer on May 20, 2026 at 2:37 pm

    It seems like this code would benefit from the null object pattern.

    # a progress bar that uses ProgressMeter
    class RealProgressBar:
         pm = Nothing
         def setMaximum(self, max):
             pm = progress.ProgressMeter(total=max)
             pc = 0
         def progress(self):
            pc += 1
            if pc % 100 = 0:
                pm.update(pc)
    
    # a fake progress bar that does nothing
    class NoProgressBar:
        def setMaximum(self, max):
             pass 
        def progress(self):
             pass
    
    # Iterate with a given progress bar
    def bigIteration(collection, progressBar=NoProgressBar()):
        progressBar.setMaximum(len(collection))
        for element in collection:
            progressBar.progress()
            doWork(element)
    
    bigIteration(collection, RealProgressBar())
    

    (Pardon my French, er, Python, it’s not my native language 😉 Hope you get the idea, though.)

    This lets you move the progress update logic from the loop, but you still have some progress related calls in there.

    You can remove this part if you create a generator from the collection that automatically tracks progress as you iterate it.

     # turn a collection into one that shows progress when iterated
     def withProgress(collection, progressBar=NoProgressBar()):
          progressBar.setMaximum(len(collection))
          for element in collection:
               progressBar.progress();
               yield element
    
     # simple iteration function
     def bigIteration(collection):
        for element in collection:
            doWork(element)
    
     # let's iterate with progress reports
     bigIteration(withProgress(collection, RealProgressBar()))
    

    This approach leaves your bigIteration function as is and is highly composable. For example, let’s say you also want to add cancellation this big iteration of yours. Just create another generator that happens to be cancellable.

    # highly simplified cancellation token
    # probably needs synchronization
    class CancellationToken:
         cancelled = False
         def isCancelled(self):
             return cancelled
         def cancel(self):
             cancelled = True
    
    # iterates a collection with cancellation support
    def withCancellation(collection, cancelToken):
         for element in collection:
             if cancelToken.isCancelled():
                 break
             yield element
    
    progressCollection = withProgress(collection, RealProgressBar())
    cancellableCollection = withCancellation(progressCollection, cancelToken)
    bigIteration(cancellableCollection)
    
    # meanwhile, on another thread...
    cancelToken.cancel()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to learn to track a moving ball using Kalman filter. Although many
I want to track an object in a video. So i suppose that I
Suppose I need to track my clicks using google analytics scripts. I know that
Suppose I want to reflect exception in the log. Should I pass stack trace
Suppose I want to add two buffers and store the result. Both buffers are
Suppose I want to store N samples (each sample takes up a significant portion
Suppose I want to have a blog with Rails 3 on my website and
Suppose I want to put objects that identify a server into a stl set
Suppose I want to execute code, for example value += 5 inside a namespace
Suppose I want to make a Web application which uses a fixed width and

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.