LoginSignup
5
7

More than 5 years have passed since last update.

スクリプト言語でプログレスバーを仮実装する方法

Last updated at Posted at 2017-01-16

はじめに

スクリプトを実行していて途中経過がよく分からない時、Print文を多用するのですが、これが結構かっこ悪いので、「#」を使ったプログレスバーを実装する。

Ruby

test.rb
(0..10).each do |i|
  sleep(1)
  print "\r" + "%3d" % (100.0 * i.to_f / 10.0) + "% " + "#" * i
end

puts "\nEND"

Python

test.py
import time

for i in range(11):
    time.sleep(1)
    print("\r{0:3d}%".format(int(100.0 * i / 10)), "#" * i , end='')

print('\nEND')

結果

コマンドを実行。

$ ruby test.rb
100% ##########
END
$ python test.py
100% ##########
END

結果だけ見てもわかりませんが、時間の経過とともに「#」が増えていきました(^_^;)
結構、便利かも...

ちなみに、Jupyter notebookでも使えますよ。
ただし、Pythonの場合は専用のライブラリが用意されているから使うことは無いかもしれません(汗)

使い方は以下の記事にてご確認下さい。
http://qiita.com/mix_dvd/items/e613c2714c7ea0e81be9

5
7
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7