Commits

James Taylor committed 8c9f568

Helper for displaying progress bar while iterating

  • Participants
  • Parent commits 2b8119f

Comments (0)

Files changed (1)

File lib/bx/cookbook/progress_bar.py

 import sys
 
 class ProgressBar:
-    def __init__(self, minValue = 0, maxValue = 10, totalWidth=12):
+    def __init__(self, minValue = 0, maxValue = 10, totalWidth=72):
         self.progBar = "[]"   # This holds the progress bar string
         self.min = minValue
         self.max = maxValue
     def __str__(self):
         return str(self.progBar)
 
+def iterprogress( sized_iterable ):
+    """
+    Iterate something printing progress bar to stdout
+    """
+    pb = ProgressBar( 0, len( sized_iterable ) )
+    for i, value in enumerate( sized_iterable ):
+        yield value
+        pb.update_and_print( i, sys.stderr )
+
 if __name__ == "__main__":
     import time