Commits

Anonymous committed 1b98eb5

timer for progress bar

Comments (0)

Files changed (1)

scripts/progressbar.py

 # Text progress bar library, like curl or scp.
 
-import sys
+import sys, time
 
 class ProgressBar:
     def __init__(self, label, limit):
         self.label = label
         self.limit = limit
         self.cur = 0
+        self.t0 = time.clock()
 
     def update(self, value):
         self.cur = value
         pct = int(100.0 * self.cur / self.limit)
         barlen = int(50.0 * self.cur / self.limit) - 1
         bar = '='*barlen + '>'
-        sys.stdout.write('\r%-12s %3d%% %-50s|'%(self.label, pct, bar))
+        dt = time.clock() - self.t0
+        sys.stdout.write('\r%-12s %3d%% %-50s| %6.1fs'%(self.label, pct, bar, dt))
         sys.stdout.flush()
 
     def finish(self):
         sys.stdout.write('\n')
 
 if __name__ == '__main__':
-    from time import sleep
     pb = ProgressBar('test', 12)
     for i in range(12):
         pb.update(i)
-        sleep(0.5)
+        time.sleep(0.5)
     pb.finish()