Created
January 6, 2016 14:20
-
-
Save icio/c0d3f7efd415071f725b to your computer and use it in GitHub Desktop.
Python enter_context
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
"""Imperative enter/exit control for python contextmanagers. | |
Where you'd prefer to use `with`: | |
with my_context(123) as f: | |
do_something() | |
but only have access to before and after hook-functions (as with Flask): | |
def before_hook(): | |
pass # Enter the context. | |
def after_hook(): | |
pass # Exit the context. | |
def handler(): | |
return do_something() | |
you can use the `enter_context` function as an open/close pair of functions: | |
ctxt = threading.local() | |
def before_hook(): | |
f, ctxt.close_context = enter_context(my_context, 123) | |
def after_hook(): | |
ctxt.close_context() | |
def handler(): | |
return do_something() | |
""" | |
from contextlib import contextmanager | |
from functools import partial | |
def main(): | |
with context(123) as mgr: | |
print "middle: %r" % (mgr,) | |
print "---" | |
mgr, exit_context = enter_context(context, 456) | |
print "middle: %r" % (mgr,) | |
exit_context() | |
@contextmanager | |
def context(*args, **kwargs): | |
print "in" | |
yield {'args': args, 'kwargs': kwargs} | |
print "out" | |
def enter_context(func, *args, **kwargs): | |
def enter(): | |
with func(*args, **kwargs) as f: | |
yield f | |
e = enter() | |
def exit(): | |
try: | |
next(e) | |
except StopIteration: | |
pass | |
return next(e), exit | |
if __name__ == "__main__": | |
main() |
Author
icio
commented
Jan 6, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment