個人的にはIDLE上で短いPythonスクリプトを動かすことが多いのですが、vistaでちょっと大きめのプログラムを動かすと 「(反応なし)」って表示されちゃうこと、ありがち、ですよね?
というわけで、コマンドライン用のプログレスバーもどきを作ってみました。
# -*- coding: utf8 -*-
# pbar.py
#lisence : as same as Python it self
from __future__ import print_function
class PBar(object):
"progress bar for CUI"
def __init__(self,n,title=""):
self.n=n
self.title=title
self.ntic=n//20
self.counter=0
def tic(self):
"call once per loop"
c=self.counter
if c==0:
print ()
print (self.title)
print ("- - - - + "*4)
if not c % self.ntic:
#print ".",
print (". ",end="")
if c>=self.n-1:
print ()
self.counter+=1
if __name__=="__main__":
import time
#class
bar=PBar(100,"Hello World. Please wait...")
for i in range(100):
time.sleep(0.01)
bar.tic()
print ("done")
テスト実行結果
please wait…
– – – – + – – – – + – – – – + – – – – +
. . . . . . . . . . . . . . . . . . . .
done
水木さんのマンガのように点々が増えていきます。
ジェネレーター版はこちら
Recipe 576986: Progress Bar for Console Programs as Iterator (Python)