Created
March 20, 2014 10:29
-
-
Save rmartinjak/9660973 to your computer and use it in GitHub Desktop.
This file contains 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
import functools | |
import contextlib | |
def cmwrap(cm, *cmargs, **cmkwargs): | |
"""Decorator wrapping a context manager around a function call.""" | |
def wrapper(f): | |
@functools.wraps(f) | |
def wrapped_f(*args, **kwargs): | |
with cm(*cmargs, **cmkwargs): | |
return f(*args, **kwargs) | |
return wrapped_f | |
return wrapper | |
@contextlib.contextmanager | |
def verbose(before, after): | |
print(before) | |
yield | |
print(after) | |
@cmwrap(verbose, "hello", "bye") | |
def foo(bar): | |
print(bar) | |
if __name__ == '__main__': | |
foo("doing something") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment