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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file context-compose-0.0.2.tar.gz.
File metadata
- Download URL: context-compose-0.0.2.tar.gz
- Upload date:
- Size: 2.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9dcc38edbab23b0fe4c64d9a6d6310116e463cdb1ab6695939787650bccf312
|
|
| MD5 |
a880087e89a2b1017fb5f1e019eaccb3
|
|
| BLAKE2b-256 |
20bc9ab766b568acb8fe7dd8aa1b2ae17fd258f0491e7bfc9b84d2cf0607647b
|
File details
Details for the file context_compose-0.0.2-py3-none-any.whl.
File metadata
- Download URL: context_compose-0.0.2-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50d12f1f3e49549ecb7009e7e26a787ea31a5fefd346e067b3cf25a21a32ed15
|
|
| MD5 |
4e54c976b5b2c44382a5e8952ccd21ce
|
|
| BLAKE2b-256 |
3d39312158895db224ea3859777d724b36686cf72d7931ef60786221b0dd6fe1
|