Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple indeterminate progress bar in Python
    primarykey
    data
    text
    <p>Even when I am afraid to be a bit off-topic, but I am not sure where else to ask this, sorry!</p> <p>I wish to build a <code>**simple** indeterminate progress bar</code> in Python</p> <p>there is a really valid p<a href="https://code.google.com/p/python-progressbar/" rel="nofollow noreferrer">rogression bar module</a> in Python, but my target is build a simple personal progress bar to add every time to my code</p> <p>the following my code it's a simple progress bar when you know the <code>maxvalue</code> of your data</p> <pre><code>from __future__ import division import sys 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("\r|%-73s| %d%%" % ('#' * int(self._pct*.73), self._pct)) sys.stdout.flush() import time 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() </code></pre> <p>now i wish to create a new class IndeterminateProgress(object) in order to create a simple indeterminate progress bar when the maxvalue of your data is unknown.</p> <p>the basic idea is print from 0 to 100 and back from 100 to 0 and again until all data are all read or all processed (code update with the help of Ethan Coon, see below)</p> <pre><code> class IndeterminateProgress(object): def __init__(self): self._pct = 0 self.maxval = 100 def update(self,value): abs_pct = value % self.maxval # this gives the percentage change from maxval phase = int(value / self.maxval) % 2 # this gives whether the bar is increasing or decreasing in size if phase == 0: rel_pct = abs_pct / self.maxval * 100 else: rel_pct = (self.maxval - abs_pct) / self.maxval * 100 if (rel_pct != self._pct): self._pct = rel_pct self.display() def start(self): self.update(0) def display(self): sys.stdout.write("\r|%-73s| %d%%" % ('#' * int(self._pct*.73), self._pct)) sys.stdout.flush() data_flush = 30000000 pbar = IndeterminateProgress() for i in xrange(data_flush): time.sleep(0.1) # do real work here pbar.update(i) </code></pre> <p>Testing with the Command Prompt of windows, the progress bar after 100% back to 0%, go to 100% but after this a new under progress bar is created.</p> <p><img src="https://i.stack.imgur.com/AH4lF.jpg" alt="enter image description here"></p> <p>The idea is print only one line of indeterminate progress bar</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload