Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

i wish to improve my Progressbar in Python

from __future__ import division
import sys
import time

class Progress(object):
    def __init__(self, maxval):
        self._seen = 0.0
        self._pct = 0
        self.maxval = maxval

    def update(self, value):
        self._seen = value
        pct = int((self._seen / self.maxval) * 100.0)
        if self._pct != pct:
            sys.stderr.write("|%-100s| %d%%" % (u"\u2588"*pct, pct) + '\n')
            sys.stdout.flush()
        self._pct = pct

    def start(self):
        pct = int((0.0 / self.maxval) * 100.0)
        sys.stdout.write("|%-100s| %d%%" % (u"\u2588"*pct, pct) + '\n')
        sys.stdout.flush()

    def finish(self):
        pct = int((self.maxval / self.maxval) * 100.0)
        sys.stdout.write("|%-100s| %d%%" % (u"\u2588"*pct, pct) + '\n')
        sys.stdout.flush()

toolbar_width = 300
pbar = Progress(toolbar_width)
pbar.start()
for i in xrange(toolbar_width):
    time.sleep(0.1) # do real work here
    pbar.update(i)
pbar.finish()
share|improve this question
1  
CodeReview is to get review on code which is working, not to get some help to add a feature. – Josay Mar 18 at 2:05

1 Answer

up vote 1 down vote accepted

As per my comments, I've not sure SE.CodeReview is the right place to get the help you are looking for.

However, as I didn't read your question properly at first, I had a look at your code and changed a few things (please note that it's not exactly the same behavior as it used to be):

  • extract the display logic in a method on its own
  • reuse update in start and finish (little changes here : this method now updates the object instead of just doing some display)
  • removed self._seen because it didn't seem useful

Here the code :

class Progress(object):
    def __init__(self, maxval):
        self._pct = 0
        self.maxval = maxval

    def update(self, value):
        pct = int((value / self.maxval) * 100.0)
        if self._pct != pct:
            self._pct = pct
            self.display()

    def start(self):
        self.update(0)

    def finish(self):
        self.update(self.maxval)

    def display(self):
        sys.stdout.write("|%-100s| %d%%" % (u"\u2588"*self._pct, self._pct) + '\n')
        sys.stdout.flush()
share|improve this answer
Thank Josay for the review. What i wish to do is print the percentage (1%, 2%, 3%, ..., 100%) in the middle of the progress bar. – Gianni Mar 18 at 2:19
@Gianni unfortunately, that's off-topic for Code Review. You should edit your question to be a valid code review request as defined in the faq, otherwise it will be closed. – codesparkle Mar 18 at 6:22
@codesparkle it's a fuzzy definition about SO (= stackoverflow) or CR (code review). Normally people use this approach: code works CR, code doesn't work SO, no code Programmer. In my case the code works and i want to improve the quality. With a with a working code (no error) i think (maybe i wrong) this question is more appropriate in CD – Gianni Mar 18 at 10:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.