Commits

Marco Lui  committed 9a07392

added support for a progressbar iterator

  • Participants
  • Parent commits 7049e33

Comments (0)

Files changed (3)

File src/consoleLog/__init__.py

         time.sleep(0.5)
         log.log("spam")
 
-    with log.level('Level 0'):
-        pass
     with log.level('Level 1'):
         log.log('message at level 1')
         with log.level('Level 2'):
             log.log('message at level 2')
             eggs()
+        with log.level("progressiter demo"):
+            for i in range(2):
+                with log.progressiter(range(5)) as iterator:
+                    for i in iterator: time.sleep(0.5)
+            with log.progressiter(range(5), "with message") as iterator:
+                for i in iterator: time.sleep(0.5)

File src/consoleLog/consoleLog.py

 import sys
 import codecs
 
+from progressBar import ProgressIterator
+
 _default_encoding = 'utf8'
 
 #----------------------------------------------------------------------------#
     """
     A context manager used to represent a level of nesting in console contexts
     """
-    def __init__(self, logger, start_message):
+    def __init__(self, logger, start_message, nSteps):
         self.logger = logger
         self.start_message = start_message
+        self.nSteps = nSteps
 
     def __enter__(self):
-        self.logger.start(self.start_message)
+        self.logger.start(self.start_message, nSteps=self.nSteps)
 
     def __exit__(self, exc_type, exc_value, traceback):
         self.logger.finish()
         self.logStack = []
         self.trim=trim
 
-    def level(self, message):
+    def level(self, message, nSteps=None):
         """
         Creates a context manager which represents a new level of nesting in
         the logging.
         """
-        return ConsoleLevel(self, message)
+        return ConsoleLevel(self, message, nSteps)
 
     def with_log(self, arg):
         """
         else:
             return lambda decoratee: LoggedFunction(self, decoratee, arg)
 
+    def progressiter(self, seq, msg = None):
+        """
+        Context manager which returns an iterator and displays a progress
+        bar that is updated according to progress in the sequence.
+        """
+        return ProgressIterator(self, seq, msg)
+
     def start(self, message, nSteps=None):
         """
         Starts a new hierarchical logging section. Every call to start() must

File src/consoleLog/progressBar.py

 
 #----------------------------------------------------------------------------#
 
+class ProgressIterator(object):
+    """
+    A context manager which provides an iterator, as well as a progressBar which is
+    updated according to the progress through the given sequence.
+    """
+    def __init__(self, logger, seq, msg):
+        self.logger = logger
+        self.seq = seq
+        self.start_message = msg if msg is not None else ""
+
+    def __enter__(self):
+        self.logger.log(self.start_message, newLine=False)
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        pass
+
+    def __iter__(self):
+        for item in withProgress(self.seq):
+          yield item
+
+#----------------------------------------------------------------------------#
+
 def tick():
     """
     Indicate activity in the current progress bar.