简介(坐在星际办公椅上)
我有一个程序,该程序使用进度条向最终用户显示该程序是否正常运行。这应该告诉最终用户他们必须等待多长时间以及程序是否仍在工作。在阅读了这个有趣的堆栈溢出线程之后,出现了这个问题:Text Progress Bar in the Console
问题/挑战
第一个问题是进度条当前仅在循环中工作,并且仅打印出范围内的数字。这没有帮助。为什么有人会想要一个带有进度条的程序,该进度条仅显示单个循环的进度,而不显示程序其余部分的工作。
第二个问题是,当我还有其他带有进度条的打印语句时,突然在命令提示符下打印了多个进度条,而不是出现在每次进度条更新动画的单个进度条。
如何在进度栏的底部始终显示进度条,并在其上方显示其他打印语句?
另一个挑战是我正在使用Windows 7操作系统(OS)。
代码
请查看我为您准备的以下示例代码:
Unable to locate element: {"method":"xpath","selector":"//div[@id='Tpay_succes']"}
程序打印的内容
import sys
import time
import threading
def progress_bar(progress):
sys.stdout.write('\r[{0}] {1}'.format('#' * int(progress/10 * 10), progress))
def doThis():
for i in range(10):
print("Doing this.")
def doThat():
for i in range(3):
print("Doing that.")
def wrapUp():
total = 2+ 2
print("Total found")
return total
if __name__ == "__main__":
print("Starting in main...")
progress_bar(1)
print("\nOther print statement here.")
print("Nice day, expensive day.")
progress_bar(3)
doThis()
progress_bar(4)
doThat()
progress_bar(5)
doThis()
doThat()
progress_bar(6)
progress_bar(7)
doThat()
doThat()
progress_bar(8)
wrapUp()
progress_bar(9)
progress_bar(10)
答案 0 :(得分:2)
您必须做三件事:
您涵盖了前两个内容,但没有涵盖第三个内容。最好也将控制台的控制封装在一个管理进度条的类中,以便它可以在一个位置处理清除,打印和重新显示所有内容:
import builtins
import math
import sys
import threading
class ProgressConsole:
def __init__(self, size, width=80, output=sys.stdout):
self.size = size
self.width = width
self.current = 0
self.output = output
# [...] and space take 3 characters, plus max width of size (log10 + 1)
self._bar_size = width - 4 - int(math.log10(size))
self._bar_step = self._bar_size / self.size
self._lock = threading.Lock()
def print(self, *message):
with self._lock:
self._clear()
builtins.print(*message, file=self.output)
self._display()
def increment(self, step=1):
with self._lock:
self.current = min(self.current + step, self.size)
self._display()
def _clear(self):
self.output.write('\r')
self.output.write(' ' * self.width)
self.output.write('\r')
def _display(self):
bar = '#' * int(round(self._bar_step * self.current))
blank = ' ' * (self._bar_size - len(bar))
self.output.write(f"\r[{bar}{blank}] {self.current}")
当示例代码导入threading
时,我包括了一个线程锁,所以我假设您希望能够在这样的环境中使用它。
上面的类使用固定宽度的进度条,并通过写出一系列空格将其擦除,然后使用\r
返回最左侧的列。
我也将条形图设置为固定宽度,以使其从左到右填充,而不是在整个屏幕上增长。
然后确保您“打印”到该对象:
if __name__ == "__main__":
progress_bar = ProgressConsole(10)
print = progress_bar.print # replace built-in with our own version
print("Starting in main...")
progress_bar.increment()
print("\nOther print statement here.")
print("Nice day, expensive day.")
progress_bar.increment(2)
doThis()
progress_bar.increment()
doThat()
progress_bar.increment()
doThis()
doThat()
progress_bar.increment(2)
doThat()
doThat()
progress_bar.increment()
wrapUp()
progress_bar.increment(2)
那么上面的最终输出是:
Starting in main...
Other print statement here.
Nice day, expensive day.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing that.
Doing that.
Doing that.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing that.
Doing that.
Doing that.
Doing that.
Doing that.
Doing that.
Doing that.
Doing that.
Doing that.
Total found
[###########################################################################] 10
插入一些随机睡眠后,运行时如下所示: