Created
May 27, 2014 15:39
-
-
Save bryan-lott/a75c93bc4dd3b402a53f to your computer and use it in GitHub Desktop.
Text Progress 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
"""I believe that this snippet of code | |
came from somewhere on stackoverflow.""" | |
from __future__ import print_function | |
import sys, time | |
class ProgressBar: | |
def __init__(self, iterations): | |
self.iterations = iterations | |
self.prog_bar = '[]' | |
self.fill_char = '|' | |
self.width = 50 | |
self.__update_amount(0) | |
def animate(self, iter): | |
print('\r', self, end='') | |
sys.stdout.flush() | |
self.update_iteration(iter + 1) | |
def update_iteration(self, elapsed_iter): | |
self.__update_amount((elapsed_iter / float(self.iterations)) * 100.0) | |
self.prog_bar += ' %d of %s complete' % (elapsed_iter, self.iterations) | |
def __update_amount(self, new_amount): | |
percent_done = int(round((new_amount / 100.0) * 100.0)) | |
all_full = self.width - 2 | |
num_hashes = int(round((percent_done / 100.0) * all_full)) | |
self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']' | |
pct_place = (len(self.prog_bar) // 2) - len(str(percent_done)) | |
pct_string = '%d%%' % percent_done | |
self.prog_bar = self.prog_bar[0:pct_place] + \ | |
(pct_string + self.prog_bar[pct_place + len(pct_string):]) | |
def __str__(self): | |
return str(self.prog_bar) | |
p = ProgressBar(1000) | |
for i in range(1001): | |
time.sleep(0.002) | |
p.animate(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment