2013-03-22 70 views
2

即使我害怕有點偏離主題,但我不知道還有什麼地方可以問這個問題,對不起!Python中簡單不確定的進度條

我想在Python

建設 **simple** indeterminate progress bar

有Python中的真正有效的P rogression bar module,但我的目標是建立一個簡單的個人的進度欄每次添加到我的代碼

下面我代碼這是一個簡單的進度條,當你知道你的數據的maxvalue

from __future__ import division 
import sys 


class Progress(object): 
    def __init__(self, maxval): 
     self._pct = 0 
     self.maxval = maxval 

    def update(self, value): 
     pct = int((value/self.maxval) * 100.0) 
     if self._pct != pct: 
      self._pct = pct 
      self.display() 

    def start(self): 
     self.update(0) 

    def finish(self): 
     self.update(self.maxval) 

    def display(self): 
     sys.stdout.write("\r|%-73s| %d%%" % ('#' * int(self._pct*.73), self._pct)) 
     sys.stdout.flush() 


import time 

toolbar_width = 300 
pbar = Progress(toolbar_width) 
pbar.start() 
for i in xrange(toolbar_width): 
    time.sleep(0.1) # do real work here 
    pbar.update(i) 
pbar.finish() 

現在我想,以創建一個簡單的indeterminat創建一個新的類IndeterminateProgress(對象)當您的數據的最大值未知時,您可以使用進度條。

的基本思路是打印從0到100,後從100到0,再次直到所有數據全部讀取或全部處理(伊森庫恩的幫助代碼更新,見下文)

class IndeterminateProgress(object): 
    def __init__(self): 
     self._pct = 0 
     self.maxval = 100 

    def update(self,value): 
     abs_pct = value % self.maxval # this gives the percentage change from maxval 
     phase = int(value/self.maxval) % 2 # this gives whether the bar is increasing or decreasing in size 
     if phase == 0: 
      rel_pct = abs_pct/self.maxval * 100 
     else: 
      rel_pct = (self.maxval - abs_pct)/self.maxval * 100 
     if (rel_pct != self._pct): 
      self._pct = rel_pct 
      self.display() 

    def start(self): 
     self.update(0) 

    def display(self): 
     sys.stdout.write("\r|%-73s| %d%%" % ('#' * int(self._pct*.73), self._pct)) 
     sys.stdout.flush() 


data_flush = 30000000 
pbar = IndeterminateProgress() 
for i in xrange(data_flush): 
    time.sleep(0.1) # do real work here 
    pbar.update(i) 

使用命令窗口提示進行測試,100%後的進度條返回到0%,達到100%,但在此之後,將創建一個新的進度條。

enter image description here

的想法是打印只有一行不確定的進度欄的

+1

如果進度不確定,打印數字的意義是什麼?如何展示一些有趣的動畫ASCII藝術呢? :) – 2013-03-22 15:28:49

+0

最初的想法是一個不確定的進度條,但我是一個開放的傢伙= D! – 2013-03-22 15:31:17

+1

嗯,這不會發生在我的一臺Linux機器上......酒吧會像預期的那樣向右,向左,向右,只剩一條線。這個數字對我來說也是47%,因爲它可能應該讀取247%,這不難解決,但我不確定爲什麼你會獲得第二條線。 – 2013-03-22 19:27:47

回答

1

基本上你只想要的一切模的MAXVAL。在Python中,modulo是使用%運算符完成的。

def update(self,value): 
    abs_pct = value % self.maxval # this gives the percentage change from maxval 
    phase = int(value/self.maxval) % 2 # this gives whether the bar is increasing or decreasing in size 
    if phase == 0: 
     rel_pct = abs_pct/self.maxval * 100 
    else: 
     rel_pct = (self.maxval - abs_pct)/self.maxval * 100 

    if (rel_pct != self._pct): 
     self._pct = rel_pct 
     self.display() 

請注意,這裏沒有要求maxval是100 ...您可以將它設置爲適合您的數據的「合理增量大小」。如果你有10億個數據要讀取並以每秒1000次的速度讀取,也許你不希望你的增量大小爲100;)

+0

謝謝@Ethan我用命令提示符測試了你的代碼。問題是100%後,代碼打印一個新的進度條。進度條返回,並創建一個新的進度條 – 2013-03-22 17:17:48

+0

Ethan查看答案已更新 – 2013-03-22 17:23:07