Simple indeterminate progress bar in Python
Question: 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 p[rogression bar
module](https://code.google.com/p/python-progressbar/) 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.

![enter image description here](http://i.stack.imgur.com/AH4lF.jpg)

The idea is print only one line of indeterminate progress bar


Answer: 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 ;)

