Skip to content

Instantly share code, notes, and snippets.

@cvanelteren
Created April 15, 2021 14:29
Show Gist options
  • Save cvanelteren/979a34d5bca150a4cb85a4d4dcab7daf to your computer and use it in GitHub Desktop.
Save cvanelteren/979a34d5bca150a4cb85a4d4dcab7daf to your computer and use it in GitHub Desktop.
Creating a simple progression bar
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