Last active
March 5, 2023 08:09
-
-
Save spava/becabf22df0ccb985db0e68e5cad0280 to your computer and use it in GitHub Desktop.
a simple and useful progress bar in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
def progress(iterable, length=33): | |
count = avg = 0 | |
total = len(iterable) | |
then = time.time() | |
for it in iter(iterable): | |
yield it | |
count += 1 | |
avg += (time.time() - then - avg) / count | |
then = time.time() | |
percent = count / total | |
filled_len = round(length * percent) | |
bar = '█' * filled_len + ' ' * (length - filled_len) | |
sys.stdout.write(f'▕{bar}▏ {round(100 * percent)}% {round((total - count) * avg)}s \r') | |
sys.stdout.flush() | |
sys.stdout.write(f'▕{"█" * length}▏ 100% {round(avg * total)}s \n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment