Created
February 12, 2019 15:29
-
-
Save isidentical/cc0bf03d762e47ba2674d4f680926e5a to your computer and use it in GitHub Desktop.
Simple contextlib.contextmanager implementation
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
class ContextManager: | |
def __init__(self, func): | |
self.func = func | |
def __call__(self, *args, **kwargs): | |
self.gen = self.func(*args, **kwargs) | |
return self | |
def __enter__(self): | |
return next(self.gen) | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
if exc_type is None: | |
pass | |
else: | |
self.gen.throw(exc_type, exc_val, exc_tb) | |
def contextmanager(f): | |
return ContextManager(f) | |
@contextmanager | |
def my_func(tot): | |
a_list = [] | |
try: | |
yield a_list | |
finally: | |
print(tot, a_list) | |
with my_func(15) as l: | |
l.append(1) | |
l.append(2) |
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
c=lambda f:(lambda *a,**k:type('H',(),{'__init__': lambda s,f,*a,**k:setattr(s,'g',f(*a, **k)),'__enter__':lambda s:next(s.g),'__exit__':lambda s, t, v, b:t if t is None else s.g.throw(t,v,b)})(f, *a, **k)) | |
@c | |
def my_func(tot): | |
a_list = [] | |
try: | |
yield a_list | |
finally: | |
print(tot, a_list) | |
with my_func(15) as l: | |
l.append(1) | |
l.append(2) |
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
exec("c=%sf:(%s`:type('H',(),{'__init__':%ss,f,`:setattr(s,'g',f(`)),'__enter__':%ss:next(s.g),'__exit__':%ss,t,v,b:t if t is None else s.g.throw(t,v,b)})(f,`))".replace('`','*a,**k')%(("lambda ",)*5)) | |
@c | |
def managed_file(name): | |
try: | |
f = open(name, 'w') | |
yield f | |
finally: | |
f.close() | |
with managed_file('naber.txt') as f: | |
f.write('naber') | |
ghost
commented
Feb 12, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment