Skip to content

Instantly share code, notes, and snippets.

@pgunn
Created January 30, 2019 19:06
Show Gist options
  • Save pgunn/4c7f934a751e0265643753759fbcd32f to your computer and use it in GitHub Desktop.
Save pgunn/4c7f934a751e0265643753759fbcd32f to your computer and use it in GitHub Desktop.
Python contexts
import time
from contextlib import contextmanager
@contextmanager
def do_itmer(operation_name):
pre_time = time.time()
yield
post_time = time.time()
print(f"Operation {operation_name} took {int(post_time - pre_time)} seconds")
with do_timer("Sleep and talk"):
time.sleep(2)
print("Hi")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment