Created
April 15, 2021 14:29
-
-
Save cvanelteren/979a34d5bca150a4cb85a4d4dcab7daf to your computer and use it in GitHub Desktop.
Creating a simple progression bar
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, time | |
class progbar: | |
def __init__(self, x, stream = sys.stdout, | |
char = "#"): | |
self.x = x | |
self.stream = stream | |
self.char = char | |
def __iter__(self): | |
self.n = len(self.x) | |
self.count = 0 | |
self.start = time.time() | |
for xi in self.x: | |
yield xi | |
self.update() | |
self.count += 1 | |
def update(self): | |
self.stream.write(self.__str__()) | |
self.stream.flush() | |
def __repr__(self): | |
self.stream.flush() | |
s = f"\r[{self.char * self.count}{' ' * (self.n - self.count)}]" | |
s+= f"{time.time() - self.start : .2f}" | |
return s | |
def __str__(self): | |
return self.__repr__() | |
for i in progbar(range(10), char = "#"): | |
time.sleep(0.1) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment