Guest User

Untitled

a guest
Jul 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. from sys import stdout
  2. class progress_range:
  3. def __init__(self, total, progress_unit_str="#", bar_width=50):
  4. self.total = total
  5. self.bar_width = int(bar_width)
  6. self.step = bar_width/total
  7. self.current = -1
  8. self.progress_unit_str = progress_unit_str
  9. self.msg_format = "\r[{:" + str(int(bar_width)) + "}] {}/{}"
  10.  
  11. def __iter__(self):
  12. return self
  13.  
  14. def __next__(self):
  15. if self.current == (self.total - 1):
  16. raise StopIteration
  17. else:
  18. self.current += 1
  19. count = int(self.step*(self.current+1))
  20. bar = self.progress_unit_str * (count if count <= self.bar_width else self.bar_width)
  21. self.message = self.msg_format.format(bar, self.current+1, self.total)
  22. stdout.write(self.message)
  23. stdout.flush()
  24. return self.current
  25.  
  26. def update(self, msg):
  27. update = "{} {}".format(self.message, msg)
  28. stdout.write(update)
  29. stdout.flush()
Add Comment
Please, Sign In to add comment