Re: [Python-ideas] Conditional context manager

2016-10-01 Thread Chris Angelico
On Sun, Oct 2, 2016 at 5:07 AM, Neil Girdhar wrote: > I suggest a more compact notation: > > with cm_function() as cm if condition: > suite() > The simplest way would be to make a conditional version of the context manager. @contextlib.contextmanager def maybe_cm(state): if state

Re: [Python-ideas] Conditional context manager

2016-10-01 Thread Paul Moore
Resending, because Google Groups messes up replying to the list :-( On 1 October 2016 at 21:09, Paul Moore wrote: > On 1 October 2016 at 19:07, Neil Girdhar wrote: >> Sometimes, I want to conditionally enter a context manager. This simplest >> (afaik) way of doing that is: >> >> with ExitSt

Re: [Python-ideas] Conditional context manager

2016-10-01 Thread Neil Girdhar
FYI: There is a null context manager: ExitStack(). On Sat, Oct 1, 2016 at 3:43 PM MRAB wrote: > On 2016-10-01 19:07, Neil Girdhar wrote: > > I'm just throwing this idea out there to get feedback. > > > > Sometimes, I want to conditionally enter a context manager. This > > simplest (afaik) way o

Re: [Python-ideas] Conditional context manager

2016-10-01 Thread MRAB
On 2016-10-01 19:07, Neil Girdhar wrote: I'm just throwing this idea out there to get feedback. Sometimes, I want to conditionally enter a context manager. This simplest (afaik) way of doing that is: with ExitStack() as stack: if condition: cm = stack.enter_context(cm_f

[Python-ideas] Conditional context manager

2016-10-01 Thread Neil Girdhar
I'm just throwing this idea out there to get feedback. Sometimes, I want to conditionally enter a context manager. This simplest (afaik) way of doing that is: with ExitStack() as stack: if condition: cm = stack.enter_context(cm_function()) suite() I suggest a mo