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.
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.
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