2013-03-30 12 views
0

我有一個從之前的post派生的類ProcessingFlush。我編寫了這個類,以便打印一系列點(由索引給出)來測試我的軟件是否在數據大小未知時處理我的數據at priori修復用於打印進度條處理的ProcessingFlush類的閃爍效果

class ProcessingFlush(object): 
    def __init__(self, index): 
     if index > 70: 
      raise ValueError("Index not valid") 
     self.index = index 

    def update(self, n): 
     self._n = n 
     self._display() 

    def finish(self): 
     sys.stdout.flush() 

    def start(self): 
     sys.stdout.write("Processing") 

    def _display(self): 
     sys.stdout.write("Processing %s%s\r" % ((n % index)* ".", (index - 1 - (n % index))* " ")) 
     sys.stdout.flush() 

pr = ProcessingFlush(5) 
pr.start() 
for n in xrange(5000): 
    pr.update(n) 
pr.finish() 

此類的限制是「處理」的閃爍效果當處理是非常快。我試圖發展這個想法來解決這個問題,但沒有結果。我希望創建一個模塊開始

def start(self): 
    sys.stdout.write("Processing") 

其中只打印處理。

def _display(self): 
     sys.stdout.write("%s%s%s\r" % ((" " * 10),(self._n % self.index)* ".", (self.index - 1 - (self._n % self.index))* " ")) 
     sys.stdout.flush() 

僅顯示打印處理的要點。

回答

1
import sys 

class progressBar: 
    ''' 
    http://code.activestate.com/recipes/168639/ 
    Randy Pargman 
    Improvements by Kelvie Wong 

    Creates a text-based progress bar. Call the object with the `print' 
    command to see the progress bar, which looks something like this: 

    [=======>  22%     ] 

    You may specify the progress bar's width, min and max values on init. 
    ''' 

    def __init__(self, minValue = 0, maxValue = 100, totalWidth=80): 
     self.progBar = '[]' # This holds the progress bar string 
     self.min = minValue 
     self.max = maxValue 
     self.span = maxValue - minValue 
     self.width = totalWidth 
     self.amount = 0  # When amount == max, we are 100% done 
     self.updateAmount(0) # Build progress bar string 

    def updateAmount(self, newAmount = 0): 
     ''' Update the progress bar with the new amount (with min and max 
      values set at initialization; if it is over or under, it takes the 
      min or max value as a default. ''' 
     if newAmount < self.min: newAmount = self.min 
     if newAmount > self.max: newAmount = self.max 
     self.amount = newAmount 

     # Figure out the new percent done, round to an integer 
     diffFromMin = float(self.amount - self.min) 
     percentDone = (diffFromMin/float(self.span)) * 100.0 
     percentDone = int(round(percentDone)) 

     # Figure out how many hash bars the percentage should be 
     allFull = self.width - 2 
     numHashes = (percentDone/100.0) * allFull 
     numHashes = int(round(numHashes)) 

     # Build a progress bar with an arrow of equal signs; special cases for 
     # empty and full 
     if numHashes == 0: 
      self.progBar = '[>%s]' % (' '*(allFull-1)) 
     elif numHashes == allFull: 
      self.progBar = '[%s]' % ('='*allFull) 
     else: 
      self.progBar = '[%s>%s]' % ('='*(numHashes-1), 
             ' '*(allFull-numHashes)) 

     # figure out where to put the percentage, roughly centered 
     percentPlace = (len(self.progBar)/2) - len(str(percentDone)) 
     percentString = str(percentDone) + '%' 

     # slice the percentage into the bar 
     self.progBar = ''.join([self.progBar[0:percentPlace], percentString, 
           self.progBar[percentPlace+len(percentString):] 
           ]) 

    def __str__(self): 
     return str(self.progBar) 

    def __call__(self, value): 
     ''' Updates the amount, and writes to stdout. Prints a carriage return 
      first, so it will overwrite the current line in stdout.''' 
     print '\r', 
     self.updateAmount(value) 
     sys.stdout.write(str(self)) 
     sys.stdout.flush() 

if __name__=='__main__': 
    import time 
    prog = progressBar(0, 100, 80) 
    for i in xrange(101): 
     prog.updateAmount(i) 
     sys.stdout.write('{p}\r'.format(p=prog)) 
     sys.stdout.flush() 
     time.sleep(.025) 
    print