Last active
December 20, 2015 20:39
-
-
Save KorbenC/6192307 to your computer and use it in GitHub Desktop.
Progress bar class implemented in Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import with_statement | |
| import threading | |
| import sys | |
| # Implementation of Ticker class | |
| class Ticker(threading.Thread): | |
| def __init__(self, msg): | |
| threading.Thread.__init__(self) | |
| self.msg = msg | |
| self.event = threading.Event() | |
| def __enter__(self): | |
| self.start() | |
| def __exit__(self, ex_type, ex_value, ex_traceback): | |
| self.event.set() | |
| self.join() | |
| def run(self): | |
| sys.stdout.write(self.msg) | |
| while not self.event.isSet(): | |
| sys.stdout.write(".") | |
| sys.stdout.flush() | |
| self.event.wait(1) | |
| # Here's how we use it... | |
| if __name__ == '__main__': | |
| import time | |
| with Ticker("A test"): | |
| time.sleep(10) | |
| with Ticker("Second test"): | |
| time.sleep(5) | |
| raise Exception("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment