ralsei.contextmanagers
¶
Module Contents¶
Classes¶
Protocol describing any context manager class |
|
Makes a dictionary of context managers act as a single context manager |
Functions¶
like |
|
Like |
API¶
- class ralsei.contextmanagers.ContextManager¶
Bases:
typing.Protocol
Protocol describing any context manager class
- __enter__() T ¶
- __exit__(__exc_type: type[BaseException] | None, __exc_value: BaseException | None, __traceback: types.TracebackType | None) bool | None ¶
- class ralsei.contextmanagers.MultiContextManager(context_managers: dict[str, ralsei.contextmanagers.ContextManager[T]])¶
Makes a dictionary of context managers act as a single context manager
>>> with MultiContextManager( ... {"sess": requests.Session(), "file": open("file.txt")} ... ) as context: ... print(context) { 'sess': <requests.sessions.Session object at 0x742d72897680>, 'file': <_io.TextIOWrapper name='file.txt' mode='r' encoding='UTF-8'> }
Initialization
- __exit__(__exc_type, __exc_value, __traceback)¶
- ralsei.contextmanagers.reusable_contextmanager(func: collections.abc.Callable[ralsei.contextmanagers.reusable_contextmanager.P, collections.abc.Generator[ralsei.contextmanagers.reusable_contextmanager.T, None, None]]) collections.abc.Callable[ralsei.contextmanagers.reusable_contextmanager.P, ralsei.contextmanagers._ReusableGeneratorContextManager[ralsei.contextmanagers.reusable_contextmanager.T, ralsei.contextmanagers.reusable_contextmanager.P]] ¶
like
contextlib.contextmanager()
, but can be entered multiple times@reusable_contextmanager def foo(value: int): yield value ctx = foo(1) with ctx as value: print(value) # Prints 1 with ctx as value: print(value) # Also prints 1
- ralsei.contextmanagers.reusable_contextmanager_const(func: collections.abc.Callable[[], collections.abc.Generator[ralsei.contextmanagers.reusable_contextmanager_const.T, None, None]]) ralsei.contextmanagers._ReusableGeneratorContextManager[ralsei.contextmanagers.reusable_contextmanager_const.T, []] ¶
Like
ralsei.contextmanagers.reusable_contextmanager()
, but used without invocationOnly for functions with no arguments.
@reusable_contextmanager_const def foo(): yield 1 with foo as value: print(value)