Package ivs :: Package aux :: Module progressMeter
[hide private]
[frames] | no frames]

Source Code for Module ivs.aux.progressMeter

  1  # -*- coding: utf-8 -*- 
  2  """ 
  3  Show progress of an arbitrary function. 
  4   
  5  Here is a silly example of its usage: 
  6      >>> import progress 
  7      >>> import time 
  8      >>> import random 
  9      >>> p = progress.ProgressMeter(total=1000) 
 10      >>> while total > 0: 
 11      ... cnt = random.randint(1, 25) 
 12      ... p.update(cnt) 
 13      ... total -= cnt 
 14      ... time.sleep(random.random()) 
 15   
 16  Here is an example of its output: 
 17   
 18  [------------------------->                                   ] 41%  821.2/sec 
 19  """ 
 20  import time, sys, math 
 21   
22 -class ProgressMeter(object):
23 ESC = chr(27)
24 - def __init__(self, **kw):
25 # What time do we start tracking our progress from? 26 self.timestamp = kw.get('timestamp', time.time()) 27 # What kind of unit are we tracking? 28 self.unit = str(kw.get('unit', '')) 29 # Number of units to process 30 self.total = int(kw.get('total', 100)) 31 # Number of units already processed 32 self.count = int(kw.get('count', 0)) 33 # Refresh rate in seconds 34 self.rate_refresh = float(kw.get('rate_refresh', .5)) 35 # Number of ticks in meter 36 self.meter_ticks = int(kw.get('ticks', 60)) 37 # Does the bar has to disappear afterwards? 38 self.disappear = bool(kw.get('disappear', False)) 39 self.meter_division = float(self.total) / self.meter_ticks 40 self.meter_value = int(self.count / self.meter_division) 41 self.last_update = None 42 self.rate_history_idx = 0 43 self.rate_history_len = 10 44 self.rate_history = [None] * self.rate_history_len 45 self.rate_current = 0.0 46 self.last_refresh = 0 47 self._cursor = False 48 self.reset_cursor()
49
50 - def reset_cursor(self, first=False):
51 if self._cursor: 52 sys.stdout.write(self.ESC + '[u') 53 self._cursor = True 54 sys.stdout.write(self.ESC + '[s')
55
56 - def update(self, count, **kw):
57 now = time.time() 58 # Caclulate rate of progress 59 rate = 0.0 60 # Add count to Total 61 self.count += count 62 self.count = min(self.count, self.total) 63 if self.last_update: 64 delta = now - float(self.last_update) 65 if delta: 66 rate = count / delta 67 else: 68 rate = count 69 self.rate_history[self.rate_history_idx] = rate 70 self.rate_history_idx += 1 71 self.rate_history_idx %= self.rate_history_len 72 cnt = 0 73 total = 0.0 74 # Average rate history 75 for rate in self.rate_history: 76 if rate == None: 77 continue 78 cnt += 1 79 total += rate 80 rate = total / cnt 81 self.rate_current = rate 82 self.last_update = now 83 # Device Total by meter division 84 value = int(self.count / self.meter_division) 85 if value > self.meter_value: 86 self.meter_value = value 87 if self.last_refresh: 88 if (now - self.last_refresh) > self.rate_refresh or \ 89 (self.count >= self.total): 90 self.refresh() 91 else: 92 self.refresh()
93
94 - def get_meter(self, **kw):
95 bar = '-' * self.meter_value 96 pad = ' ' * (self.meter_ticks - self.meter_value) 97 perc = (float(self.count) / self.total) * 100 98 return '[%s>%s] %d%% %.1f/sec' % (bar, pad, perc, self.rate_current)
99
100 - def refresh(self, **kw):
101 # Clear line 102 sys.stdout.write(self.ESC + '[2K') 103 self.reset_cursor() 104 sys.stdout.write(self.get_meter(**kw)) 105 # Are we finished? 106 if self.count >= self.total and not self.disappear: 107 sys.stdout.write('\n') 108 if self.count >= self.total and self.disappear: 109 sys.stdout.write("Kieken") 110 sys.stdout.write(self.ESC + '[2K') 111 sys.stdout.write(self.ESC + '[u') 112 sys.stdout.flush() 113 # Timestamp 114 self.last_refresh = time.time()
115