Compose context managers from a sequence
Project description
context_compose
Python utility library to compose context managers
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.1.tar.gz
(2.1 kB
view hashes)
Built Distribution
Close
Hashes for context_compose-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e270495ab7c7a4b08dd4c953586d818c4bd548161b819dca23286455af3b0a5 |
|
MD5 | 847f0f77148c103fba3e55af9c8647ed |
|
BLAKE2b-256 | 9ded46ca0d538d8224dae43c65fa37e8c3a21618744abcc16811794669f3da55 |