Even when I am afraid to be a bit off-topic, but I am not sure where else to ask this, sorry!
I wish to build a **simple** indeterminate progress bar in Python
there is a really valid progression bar module in Python, but my target is build a simple personal progress bar to add every time to my code
the following my code it's a simple progress bar when you know the maxvalue of your data
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()
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.
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)
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)
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.

The idea is print only one line of indeterminate progress bar
Basically you just want everything modulo the maxval. In python, modulo is done with the % operator.
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()
Note there is no requirement here that maxval is 100... you could set it to whatever a "reasonable increment size" is for your data. If you have 1billion data to read and do it at 1000 per second, maybe you don't want your increment size to be 100 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With