<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 18, 2016 at 1:36 AM, Devin Jeanpierre <span dir="ltr"><<a href="mailto:jeanpierreda@gmail.com" target="_blank">jeanpierreda@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span class="">> This is one of those things which are so easy to implement which makes you<br>
> think it is probably not worth adding to the stdlib, but then again, this is<br>
> something I've ended up doing and rewriting pretty often over the years.<br>
> Real world example:<br>
><br>
> class DummyLock(object):<br>
> def __init__(self, *args, **kwargs):<br>
> pass<br>
> def __enter__(self, *args, **kwargs):<br>
> return self<br>
> def __exit__(self, *args, **kwargs):<br>
> pass<br>
><br>
> def get_lock(name, bypass_lock=False):<br>
> lock_cls = DummyLock if bypass_lock else RedisLock<br>
> return lock<br>
><br>
> with get_lock('foo', bypass_lock=True):<br>
> ...<br>
<br>
</span>with contextlib.ExitStack() as exit_stack:<br>
if not bypass_lock:<br>
exit_stack.enter_context(RedisLock())<br>
<br>
And similar.<br>
<br>
In fact, ExitStack can itself be used as a no-op context manager, if you want.<br>
<span class=""><font color="#888888"><br>
-- Devin<br></font></span></blockquote><div><br></div><div>I didn't think about this. So yeah, my proposal is useless. Nevermind. </div></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>Giampaolo - <a href="http://grodola.blogspot.com" target="_blank">http://grodola.blogspot.com</a></div><div><br></div></div></div>
</div></div>