Skip to content

Instantly share code, notes, and snippets.

@kowey
Created July 18, 2014 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kowey/97c6aa7646cc9bfce971 to your computer and use it in GitHub Desktop.
Save kowey/97c6aa7646cc9bfce971 to your computer and use it in GitHub Desktop.
# pylint: disable=too-few-public-methods, redefined-builtin, invalid-name
class Torpor(object):
"""
Announce that we're about to do something, then do it,
then say we're done.
Usage: ::
with Torpor("doing a slow thing"):
some_slow_thing
Output (1): ::
doing a slow thing...
Output (2a): ::
doing a slow thing... done
Output (2b): ::
doing a slow thing... ERROR
<stack trace>
"""
def __init__(self, msg, sameline=True, file=sys.stderr):
self._msg = msg
self._file = file
self._sameline = sameline
def __enter__(self):
if self._sameline:
print(self._msg, end="... ", file=self._file)
else:
print("[start]", self._msg, file=self._file)
def __exit__(self, type, value, tb):
if tb is None:
done = "done" if self._sameline else "[end] " + self._msg
print(done, file=self._file)
else:
oops = "ERROR!" if self._sameline else "ERROR! " + self._msg
print(oops, file=self._file)
traceback.print_exception(type, value, tb)
sys.exit(1)
# pylint: enable=too-few-public-methods, redefined-builtin, invalid-name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment