Posted By


ctmiller on 01/14/10

Tagged


Statistics


Viewed 386 times
Favorited by 0 user(s)

Related snippets


progress_bar.py


/ Published in: Python
Save to your folder(s)

Found on [Corey Goldberg's blog](http://coreygoldberg.blogspot.com/2010/01/python-command-line-progress-bar-with.html). Usage:

from progress_bar import ProgressBar
p = ProgressBar(60)
p.update_time(15)
print p
p.fill_char = '='
p.update_time(40)
print p


Copy this code and paste it in your HTML
  1. #!/usr/bin/env python
  2. #
  3. # ascii command-line progress bar with percentage and elapsed time display
  4. #
  5. # adapted from Pylot source code (original by Vasil Vangelovski)
  6. # modified by Corey Goldberg - 2010
  7.  
  8.  
  9.  
  10. class ProgressBar:
  11. def __init__(self, duration):
  12. self.duration = duration
  13. self.prog_bar = '[]'
  14. self.fill_char = '#'
  15. self.width = 40
  16. self.__update_amount(0)
  17.  
  18. def __update_amount(self, new_amount):
  19. percent_done = int(round((new_amount / 100.0) * 100.0))
  20. all_full = self.width - 2
  21. num_hashes = int(round((percent_done / 100.0) * all_full))
  22. self.prog_bar = '[' + self.fill_char * num_hashes + ' ' * (all_full - num_hashes) + ']'
  23. pct_place = (len(self.prog_bar) / 2) - len(str(percent_done))
  24. pct_string = '%i%%' % percent_done
  25. self.prog_bar = self.prog_bar[0:pct_place] + \
  26. (pct_string + self.prog_bar[pct_place + len(pct_string):])
  27.  
  28. def update_time(self, elapsed_secs):
  29. self.__update_amount((elapsed_secs / float(self.duration)) * 100.0)
  30. self.prog_bar += ' %ds/%ss' % (elapsed_secs, self.duration)
  31.  
  32. def __str__(self):
  33. return str(self.prog_bar)
  34.  
  35.  
  36.  
  37. if __name__ == '__main__':
  38. p = ProgressBar(60)
  39.  
  40. p.update_time(15)
  41. print p
  42.  
  43. p.fill_char = '='
  44. p.update_time(40)
  45. print p

URL: http://code.google.com/p/corey-projects/source/browse/trunk/python2/progress_bar.py

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.