import time import sys class Duration: def __init__(self, description): self.description = description def __enter__(self): self.start = time.time() return self def __exit__(self, exc_type, exc_val, exc_tb): self.end = time.time() self.duration = self.end - self.start print(self.description, end=" ", file=sys.stderr) print("took {} seconds.".format(self.duration), file=sys.stderr)