VC里,进度条只需要点击鼠标拖动一个控件就欧了,想要在命令行较长时间执行过程中显示出运行的进度,可以通过如下类完成
#!/usr/bin/env python
import sys
import time
class progressbar(object):
def __init__(self, finalcount, block_char = ‘.’):
self.finalcount = finalcount
self.blockcount = 0
self.block = block_char
self.f = sys.stdout
if not self.finalcount:
return
self.f.write(‘\n——————– % Progress ——————\n’)
self.f.write(‘ 1 2 3 4 5 6 7 8 9 0\n’)
# self.f.write(‘—-0—-0—-0—-0—-0—-0—-0—-0—-0—-0\n’)
def progress(self, count):
count = min(count, self.finalcount)
if self.finalcount:
percentcomplete = int(round(100.0 * count / self.finalcount))
if percentcomplete < 1:
percentcomplete = 1
else:
percentcomplete = 100
blockcount = int(percentcomplete//2)
if blockcount <= self.blockcount:
return
for i in range(self.blockcount, blockcount):
self.f.write(self.block)
self.f.flush()
self.blockcount = blockcount
if percentcomplete == 100:
self.f.write(‘\n’)
if __name__ == ‘__main__’:
pb = progressbar(8, ‘#’)
for count in range(1, 9):
pb.progress(count)
time.sleep(0.2)
# pb = progressbar(100)
# pb.progress(20)
# time.sleep(0.3)
# pb.progress(47)
# time.sleep(0.3)
# pb.progress(90)
# time.sleep(0.3)
# pb.progress(100)
# print ‘testing 1:’
# pb = progressbar(1)
# pb.progress(1)
copy的这个类是以任务最后完成时应该达到的某个数字为基础的,因此用这个类应用程序可以用任何方便的测量单位来跟踪任务的进度和完成度,只要这个单位能够同时应用于最终计数,应用程序周期性传递给process方法的count方法,进度条的显示就是准确的