contextmanager

Context manager as a decorator with access to the yielded object

Context manager as a decorator with access to the yielded object Question: I have a context manager for an object that can be used similar to the open context manager, e.g. with MyContextManager as cm: cm.do_something() I know that a simple context manager can be made into a decorator if using contextlib.ContextDecorator to create it. …

Total answers: 2

Provide contextvars.Context with a ContextManager

Provide contextvars.Context with a ContextManager Question: I’m trying to manage transactions in my DB framework (I use MongoDB with umongo over pymongo). To use transaction, one must pass a session kwarg along the whole call chain. I would like to provide a context manager that would isolate the transaction. Only the function at the end …

Total answers: 1

How to idiomatically open multiple managed resources from an object method in Python

How to idiomatically open multiple managed resources from an object method in Python Question: What is the most Pythonic way of constructing an object to open multiple (context managed) resources and do work with those resources? I have a class which opens several managed resources, which are then operated on in class methods. For example, …

Total answers: 1

Is there a Pythonic way to run async task in background similar to using a contextmanager?

Is there a Pythonic way to run async task in background similar to using a contextmanager? Question: Recently I wanted to run some asynchronous tasks in the background while running other tasks but I didn’t think the code was Pythonic enough: task = asyncio.create_task(long_task()) await short_task() await task So I made it more Pythonic: @asynccontextmanager …

Total answers: 2

How can we "associate" a Python context manager to the variables appearing in its block?

How can we "associate" a Python context manager to the variables appearing in its block? Question: As I understand it, context managers are used in Python for defining initializing and finalizing pieces of code (__enter__ and __exit__) for an object. However, in the tutorial for PyMC3 they show the following context manager example: basic_model = …

Total answers: 3

Pythonic way to compose context managers for objects owned by a class

Pythonic way to compose context managers for objects owned by a class Question: It’s typical to require for some task multiple objects which have resources to be explicitly released – say, two files; this is easily done when the task is local to a function using nested with blocks, or – even better – a …

Total answers: 5

Difference between Context Managers and Decorators in Python

Difference between Context Managers and Decorators in Python Question: What is the main difference between the two? I have been studying Python and came across them. A decorator is essentially a function that wraps another function and you can do anything before and after a particular function executes. def my_decorator(some_function): def wrapper(*args, **kwargs): print(“Do something …

Total answers: 4

How do I write a null (no-op) contextmanager in Python?

How do I write a null (no-op) contextmanager in Python? Question: Sometimes I need a dummy context manager that does nothing. It can then be used as a stand-in for a more useful, but optional, context manager. For example: ctx_mgr = <meaningfulContextManager> if <condition> else <nullContextManager> with ctx_mgr: … How do I define such a …

Total answers: 6

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__ Question: I have searched and I’m unable to come up with any good reason to use python’s __enter__ /__exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, …

Total answers: 3