X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=tools%2Fprogressbar.py;fp=tools%2Fprogressbar.py;h=dd38b420adfff9f9e3b93f2ac7e9d330b7a318fd;hb=ea4829e265bd45b9c1b8556463d10ee1e082c6ce;hp=0000000000000000000000000000000000000000;hpb=6ce000e7212d9f5db6e5998c41df15bcad2022c8;p=ipdf%2Fcode.git diff --git a/tools/progressbar.py b/tools/progressbar.py new file mode 100644 index 0000000..dd38b42 --- /dev/null +++ b/tools/progressbar.py @@ -0,0 +1,42 @@ +# From #http://nbviewer.ipython.org/github/ipython/ipython/blob/3607712653c66d63e0d7f13f073bde8c0f209ba8/docs/examples/notebooks/Animations_and_Progress.ipynb + +import sys, time +try: + from IPython.display import clear_output + have_ipython = True +except ImportError: + have_ipython = False + +class ProgressBar: + def __init__(self, iterations): + self.iterations = iterations + self.prog_bar = '[]' + self.fill_char = '*' + self.width = 40 + self.__update_amount(0) + if have_ipython: + self.animate = self.animate_ipython + else: + self.animate = self.animate_noipython + + def animate_ipython(self, iter): + print '\r', self, + 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)