lisa.utils.chain_cm#
- lisa.utils.chain_cm(*fs)[source]#
Chain the context managers returned by the given callables.
This is equivalent to:
@contextlib.contextmanager def combined(fs): fs = list(reversed(fs)) with fs[0](x) as y: with fs[1](y) as z: with fs[2](z) as ...: ... with ... as final: yield final
It is typically used instead of regular function composition when the functions return a context manager
@contextlib.contextmanager def f(a, b): print(f'f a={a} b={b}') yield a + 10 @contextlib.contextmanager def g(x): print(f'g x={x}') yield f'final x={x}' combined = chain_cm(g, f) with combined(a=1, b=2) as res: print(res) # Would print: # f a=1 b=2 # g x=11 # final x=11