1
\$\begingroup\$

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()
\$\endgroup\$
1
  • 1
    \$\begingroup\$ CodeReview is to get review on code which is working, not to get some help to add a feature. \$\endgroup\$ Commented Mar 18, 2013 at 2:05

1 Answer 1

2
\$\begingroup\$

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()
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 18, 2013 at 2:19
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Mar 18, 2013 at 6:22
  • \$\begingroup\$ @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 \$\endgroup\$ Commented Mar 18, 2013 at 10:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.