Posted By

codeshaman on 01/14/10


Tagged

command python line


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tfoutz99


progress_bar.py


 / Published in: Python
 

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

Originally found on Corey Goldberg's blog

  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

Report this snippet  

You need to login to post a comment.