This is one of those things which are so easy to implement which makes you think it is probably not worth adding to the stdlib, but then again, this is something I've ended up doing and rewriting pretty often over the years. Real world example:
class DummyLock(object): def __init__(self, *args, **kwargs): pass def __enter__(self, *args, **kwargs): return self def __exit__(self, *args, **kwargs): pass
def get_lock(name, bypass_lock=False): lock_cls = DummyLock if bypass_lock else RedisLock return lock
with get_lock('foo', bypass_lock=True): ...
Similarly to contextlib.closing and contextlib.suppress, perhaps it would be nice to have contextlib.DummyContext just because it's something which is done (I think) fairly often.
On the bikeshedding front, in order to be consistent with closing(), redirect_stderr(), redirect_stdout() and suppress(), a better name for this would probably be contextlib.dummy_context.
Extra: the same thing can be achieved by using mock.MagicMock, which probably makes this proposal useless and kills it entirely. The additional value is that it would be more explicit/clear/immediate to have this in contextlib itself as opposed to unittest module, which is kinda weird. But then again, "there should be (possibly) only one way to do it" so I'm not sure. OK, I should stop talking with myself. =)
You can use an ExitStack for this
On Wed, May 18, 2016 at 1:29 AM, Giampaolo Rodola' g.rodola@gmail.com wrote:
This is one of those things which are so easy to implement which makes you think it is probably not worth adding to the stdlib, but then again, this is something I've ended up doing and rewriting pretty often over the years. Real world example:
class DummyLock(object): def __init__(self, *args, **kwargs): pass def __enter__(self, *args, **kwargs): return self def __exit__(self, *args, **kwargs): pass
def get_lock(name, bypass_lock=False): lock_cls = DummyLock if bypass_lock else RedisLock return lock
with get_lock('foo', bypass_lock=True): ...
Similarly to contextlib.closing and contextlib.suppress, perhaps it would be nice to have contextlib.DummyContext just because it's something which is done (I think) fairly often.
On the bikeshedding front, in order to be consistent with closing(), redirect_stderr(), redirect_stdout() and suppress(), a better name for this would probably be contextlib.dummy_context.
Extra: the same thing can be achieved by using mock.MagicMock, which probably makes this proposal useless and kills it entirely. The additional value is that it would be more explicit/clear/immediate to have this in contextlib itself as opposed to unittest module, which is kinda weird. But then again, "there should be (possibly) only one way to do it" so I'm not sure. OK, I should stop talking with myself. =)
-- Giampaolo - http://grodola.blogspot.com
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
This is one of those things which are so easy to implement which makes you think it is probably not worth adding to the stdlib, but then again, this is something I've ended up doing and rewriting pretty often over the years. Real world example:
class DummyLock(object): def __init__(self, *args, **kwargs): pass def __enter__(self, *args, **kwargs): return self def __exit__(self, *args, **kwargs): pass
def get_lock(name, bypass_lock=False): lock_cls = DummyLock if bypass_lock else RedisLock return lock
with get_lock('foo', bypass_lock=True): ...
with contextlib.ExitStack() as exit_stack: if not bypass_lock: exit_stack.enter_context(RedisLock())
And similar.
In fact, ExitStack can itself be used as a no-op context manager, if you want.
-- Devin
On Wed, May 18, 2016 at 1:36 AM, Devin Jeanpierre jeanpierreda@gmail.com wrote:
This is one of those things which are so easy to implement which makes
you
think it is probably not worth adding to the stdlib, but then again,
this is
something I've ended up doing and rewriting pretty often over the years. Real world example:
class DummyLock(object): def __init__(self, *args, **kwargs): pass def __enter__(self, *args, **kwargs): return self def __exit__(self, *args, **kwargs): pass
def get_lock(name, bypass_lock=False): lock_cls = DummyLock if bypass_lock else RedisLock return lock
with get_lock('foo', bypass_lock=True): ...
with contextlib.ExitStack() as exit_stack: if not bypass_lock: exit_stack.enter_context(RedisLock())
And similar.
In fact, ExitStack can itself be used as a no-op context manager, if you want.
-- Devin
I didn't think about this. So yeah, my proposal is useless. Nevermind.
On 05/18/2016 06:16 AM, Giampaolo Rodola' wrote:
On Wed, May 18, 2016 at 1:36 AM, Devin Jeanpierre wrote:
with contextlib.ExitStack() as exit_stack: if not bypass_lock: exit_stack.enter_context(RedisLock())
And similar.
In fact, ExitStack can itself be used as a no-op context manager, if you want.
I didn't think about this. So yeah, my proposal is useless. Nevermind.
Not at all. It's a good use-case, and it helped introduce/remind us about ExitStack (which I suspect is a hidden gem).
So thank you all! :)
-- ~Ethan~
On Wed, May 18, 2016, 10:12 Ethan Furman ethan@stoneleaf.us wrote:
On 05/18/2016 06:16 AM, Giampaolo Rodola' wrote:
On Wed, May 18, 2016 at 1:36 AM, Devin Jeanpierre wrote:
with contextlib.ExitStack() as exit_stack: if not bypass_lock: exit_stack.enter_context(RedisLock())
And similar.
In fact, ExitStack can itself be used as a no-op context manager, if you want.
I didn't think about this. So yeah, my proposal is useless. Nevermind.
Not at all. It's a good use-case, and it helped introduce/remind us about ExitStack (which I suspect is a hidden gem).
So thank you all! :)
Python 3.6 also has a context manager ABC so you could also use that in 2 lines for a dummy context manager.
-brett
-- ~Ethan~
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On May 18, 2016, at 08:11 AM, Ethan Furman wrote:
Not at all. It's a good use-case, and it helped introduce/remind us about ExitStack (which I suspect is a hidden gem).
A beautiful one.
http://www.wefearchange.org/2013/05/resource-management-in-python-33-or.html
Cheers, -Barry