Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import stdout
- class progress_range:
- def __init__(self, total, progress_unit_str="#", bar_width=50):
- self.total = total
- self.bar_width = int(bar_width)
- self.step = bar_width/total
- self.current = -1
- self.progress_unit_str = progress_unit_str
- self.msg_format = "\r[{:" + str(int(bar_width)) + "}] {}/{}"
- def __iter__(self):
- return self
- def __next__(self):
- if self.current == (self.total - 1):
- raise StopIteration
- else:
- self.current += 1
- count = int(self.step*(self.current+1))
- bar = self.progress_unit_str * (count if count <= self.bar_width else self.bar_width)
- self.message = self.msg_format.format(bar, self.current+1, self.total)
- stdout.write(self.message)
- stdout.flush()
- return self.current
- def update(self, msg):
- update = "{} {}".format(self.message, msg)
- stdout.write(update)
- stdout.flush()
Add Comment
Please, Sign In to add comment