Compose context managers from a sequence
Project description
context_compose
Python utility library to compose context managers (3.6+)
Motivation
When you want to use a dynamic combination of context managers together, and the builtin ways are too ugly or inflexible.
Turn this:
import os
if __name__ == "__main__":
if os.environ.get("DEBUG") and os.environ.get("PROFILE":
with debugger(), profile():
main()
elif os.environ.get("DEBUG"):
with debugger():
main()
elif os.environ.get("DEBUG"):
with debugger():
main()
Into this:
import os
from context_compose import compose, impotent_manager
if __name__ == "__main__":
managers = [
debugger() if os.environ.get("DEBUG") else impotent_manager(),
profile() if os.environ.get("PROFILE") else impotent_manager(),
]
with compose(managers):
main()
impotent_manager
is a useful substitute when your code has a with
block, you'd rather keep to a single pattern so a context manager must be provided to it.
How it works
Under the hood it uses contextlib.ExitStack to layer context managers in list order.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
context-compose-0.0.2.tar.gz
(2.1 kB
view hashes)
Built Distribution
Close
Hashes for context_compose-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50d12f1f3e49549ecb7009e7e26a787ea31a5fefd346e067b3cf25a21a32ed15 |
|
MD5 | 4e54c976b5b2c44382a5e8952ccd21ce |
|
BLAKE2b-256 | 3d39312158895db224ea3859777d724b36686cf72d7931ef60786221b0dd6fe1 |