Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Simple cli based progress bar module for Python 2/3
- Example:
- import time
- import progressbar
- bar = progressbar.ProgressBar(range(10))
- for x in bar:
- time.sleep(0.5)
- """
- __author__ = '@laszlokuehl'
- import sys
- class ProgressBar:
- def __init__(self, data, length=50):
- self.data = data
- self.length = float(length)
- self.multip = 100 / self.length
- self.used = 0
- self.current = 0
- def __str__(self):
- return str(self.data)
- def __repr__(self):
- return self.__str__()
- def __iter__(self):
- for val in self.data:
- self.printBar()
- yield val
- self.printBar(); print('')
- def printBar(self):
- bar = '\r[%s%s | %s/%s | %d%% ] ' % (
- '=' * int(self.current), '-' * int(self.length - self.current),
- self.used, len(self.data), int(self.current * self.multip))
- sys.stdout.write(bar)
- sys.stdout.flush()
- self.current += self.length / len(self.data)
- self.used += 1
Add Comment
Please, Sign In to add comment