Show progress bar and remaining time on console (python)

[Addition] 2017/05/10

Regarding the subject of the title, he introduced a package called tqdm. When I tried it, the load was so light that hitting stdout on my own would be stupid, and I think I will use it habitually.

I don't like the overhead of looping stdout, so I felt it was a hassle to thin it out, but it was a shame. Try it.

https://pypi.python.org/pypi/tqdm

_tqdm.py If you look at usage, it's just like that, but for those who want to try it out. 2. For x people, please use xrange.

> python -m pip install tqdm

>>> from tqdm import tqdm
>>>
>>> for _ in tqdm(range(0x100000)):
>>>     pass

The following is a bad sentence except for the comments received.

View progress and time remaining

As the work of molding training sets for learning has become tens of thousands, I have come to want a function that counts down. Even if I searched for a neat implementation, it was different, so I made it up.

> progbar.py
  24% |#######-------------------------|  0:12:06
 100% |################################|  0:00:00

It's a little easier because I was able to visualize the scheduled end time. It also feels good for machine learning training loops.

specification

Implementation

Operation confirmed only under windows environment.

progbar.py


import sys, time

class progbar:
    def __init__(self, period=100, bars=32):
        self._period  = period
        self.bars     = bars
        self.active   = True
        self.start    = time.time()

    def dispose(self):
        if self.active:
            self.active = False
            self.update(self._period)
            sys.stdout.write("\n")

    def __del__(self):
        self.dispose()

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        self.dispose()

    def period(self):
        return self._period

    def update(self, tick):
        rate = tick / self._period

        # progress rate
        str = "{0:7d}% ".format(int(rate*100))

        # progress bar
        bar_prog = int(rate * self.bars)
        str += "|"
        str += "#" * (            bar_prog)
        str += "-" * (self.bars - bar_prog)
        str += "|"

        # calc end
        elapsed = time.time() - self.start
        predict = (elapsed * (1 - rate) / rate) if not rate == 0.0 else 0
        s = int(predict)
        m, s = divmod(s, 60)
        h, m = divmod(m, 60)
        d, h = divmod(h, 24)
        str += "{0:3d}:{1:02d}:{2:02d}\r".format(h,m,s)

        sys.stdout.write(str)

def main():
    # how to use
    with progbar(500) as pb:
        for i in range(pb.period()):
            time.sleep(0.01)
            if i % 10 == 0:
                pb.update(i)
    
if __name__ == '__main__':
    main()

\ # Time display is a force.

Recommended Posts

Show progress bar and remaining time on console (python)
[Python] A progress bar on the terminal
Python on Ruby and angry Ruby on Python
Notes on Python and dictionary types
Python 3.6 on Windows ... and to Xamarin.
Progress bar in pop-up in Python Kivy
Integrate Modelica and Python on Windows
Python and Bash on Cisco Catalyst IOS-XE
Build Python3 and OpenCV environment on Ubuntu 18.04
Python virtual environment and packages on Ubuntu
A memo with Python2.7 and Python3 on CentOS
Notes on building Python and pyenv on Mac
See file and folder information on python
[Python] Progress display by progress bar using tqdm
Understand python lists, dictionaries, and so on.
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
Install and run Python3.5 + NumPy + SciPy on Windows 10
Notes on installing Python3 and using pip on Windows7
[Python] Conversion memo between time data and numerical data
Output python log to both console and file
PHP and Python integration from scratch on Laravel
At the time of python update on ubuntu
The one that displays the progress bar in Python
[Note] Installing Python 3.6 + α on Windows and RHEL
Try importing MLB data on Mac and Python
Install MongoDB on Ubuntu 16.04 and operate via python
To represent date, time, time, and seconds in Python
Install Python and libraries for Python on MacOS Catalina
Install ZIP version Python and pip on Windows 10