进展_棒.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了进展_棒.py相关的知识,希望对你有一定的参考价值。

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
  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

以上是关于进展_棒.py的主要内容,如果未能解决你的问题,请参考以下文章

Discord.py 错误:TypeError:__new__() 得到了一个意外的关键字参数“deny_new”

python中的模块,库,包有啥区别

小棒组合第三周项目总结

找bug的过程

Python的基本库与第三方库

关于经典题目火柴棒等式