Приведенные выше предложения довольно хороши, но я думаю, что большинство людей просто хотят получить готовое решение, не зависящее от внешних пакетов, но также пригодное для повторного использования.
Я получил лучшие из всех вышеперечисленных пунктов и превратил их в функцию вместе с контрольными примерами.
Чтобы использовать его, просто скопируйте строки в "def update_progress (progress)"но не тестовый скрипт.Не забудьте импортировать sys.Вызывайте это всякий раз, когда вам нужно отобразить или обновить индикатор выполнения.
Это работает путем прямой отправки символа "\ r" на консоль, чтобы переместить курсор назад к началу.«print» в python не соответствует вышеуказанному символу для этой цели, поэтому нам нужно 'sys'
import time, sys
# update_progress() : Displays or updates a console progress bar
## Accepts a float between 0 and 1. Any int will be converted to a float.
## A value under 0 represents a 'halt'.
## A value at 1 or bigger represents 100%
def update_progress(progress):
barLength = 10 # Modify this to change the length of the progress bar
status = ""
if isinstance(progress, int):
progress = float(progress)
if not isinstance(progress, float):
progress = 0
status = "error: progress var must be float\r\n"
if progress < 0:
progress = 0
status = "Halt...\r\n"
if progress >= 1:
progress = 1
status = "Done...\r\n"
block = int(round(barLength*progress))
text = "\rPercent: [{0}] {1}% {2}".format( "#"*block + "-"*(barLength-block), progress*100, status)
sys.stdout.write(text)
sys.stdout.flush()
# update_progress test script
print "progress : 'hello'"
update_progress("hello")
time.sleep(1)
print "progress : 3"
update_progress(3)
time.sleep(1)
print "progress : [23]"
update_progress([23])
time.sleep(1)
print ""
print "progress : -10"
update_progress(-10)
time.sleep(2)
print ""
print "progress : 10"
update_progress(10)
time.sleep(2)
print ""
print "progress : 0->1"
for i in range(100):
time.sleep(0.1)
update_progress(i/100.0)
print ""
print "Test completed"
time.sleep(10)
Это то, что показывает результат тестового сценария (анимация последнего индикатора выполнения):
progress : 'hello'
Percent: [----------] 0% error: progress var must be float
progress : 3
Percent: [##########] 100% Done...
progress : [23]
Percent: [----------] 0% error: progress var must be float
progress : -10
Percent: [----------] 0% Halt...
progress : 10
Percent: [##########] 100% Done...
progress : 0->1
Percent: [##########] 99.0%
Test completed