On 4 October 2016 at 14:32, Ken Kundert <admin(a)shalmirane.com> wrote:
> For example, it was suggested that one could simplify a multi-level loop by
> moving the multiple levels of for loop into a separate function that acts as
> generator. And that is a nice idea, but when writing it, the writing of the
> generator function represents a speed bump. Whereas writing something like the
> following is simple, compact, quick, and obvious. There is no reason why it
> should not be allowed even though it might not always be the best approach to
> use:
>
> for i in range(5) for j in range(5) for k in range(5):
> ...
>
> And I would really like to be able to write loops of the form:
>
> for item in items if item is not None:
> ...
>
> It is something I do all the time, and it would be nice if it did not consume
> two levels on indentation.
And when you add the "else" clause that's supported by both "for" and
"if", what does that mean in the abbreviated form?
for item in items if item is not None:
...
else:
# ???
Or is the implicit proposal that this form be special cased to
disallow the "else" clause?
Comprehensions don't have that concern, as they don't support "else"
clauses at all.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan(a)gmail.com | Brisbane, Australia
I had a bug where nan floats failed to compare equal because there seems
to be more than one nan value and comparison seems to be binary based.
How about we make float eq test if both are math. Isnan?
-- Arkadiusz Bulski --
Forwarding to the list, since I took the broken Google Group cc out of
the reply list, but didn't added the proper one.
---------- Forwarded message ----------
From: Nick Coghlan <ncoghlan(a)gmail.com>
Date: 2 October 2016 at 17:45
Subject: Re: [Python-ideas] Conditional context manager
To: Neil Girdhar <mistersheik(a)gmail.com>
On 2 October 2016 at 04:07, Neil Girdhar <mistersheik(a)gmail.com> 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_function())
> suite()
>
> I suggest a more compact notation:
>
> with cm_function() as cm if condition:
> suite()
As Chris notes, this is typically going to be better handled by
creating an *un*conditional CM that encapsulates the optionality so
you don't need to worry about it at the point of use.
If you wanted a generic version of that, then the stack creation and
cm creation can be combined into a single context manager:
@contextmanager
def optional_cm(condition, cm_factory, *args, **kwds):
stack = ExitStack()
cm = None
with stack:
if condition:
cm = stack.enter_context(cm_factory(*args, **kwds))
yield stack, cm
However, even simpler than both this and Chris's maybe_cm() example is
the plain ExitStack-as-the-null-context-manager function approach
already covered in the contextlib docs:
https://docs.python.org/3/library/contextlib.html#simplifying-support-for-s…
Applying that approach to this particular pattern looks like:
def optional_cm(condition, cm_factory, *args, **kwds):
if condition:
return cm_factory(*args, **kwds)
return ExitStack()
Resulting in:
with optional_cm(condition, cm_function):
suite()
which is fine for a construct that is uncommon in general, but may be
popular in a particular code base.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan(a)gmail.com | Brisbane, Australia
--
Nick Coghlan | ncoghlan(a)gmail.com | Brisbane, Australia
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 more compact notation:
with cm_function() as cm if condition:
suite()
I'm not sure that this is possible within the grammar. (For some reason
with with_expr contains '"as" expr' rather than '"as" NAME'?
I realize this comes up somewhat rarely. I use context managers a lot, and
it comes up maybe 1 in 5k lines of code.
For some extensions of this notation, an else clause could bind a value to
cm in the case that condition is false.
Best,
Neil
Resending, because Google Groups messes up replying to the list :-(
On 1 October 2016 at 21:09, Paul Moore <p.f.moore(a)gmail.com> wrote:
> On 1 October 2016 at 19:07, Neil Girdhar <mistersheik(a)gmail.com> wrote:
>> 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 more compact notation:
>>
>> with cm_function() as cm if condition:
>> suite()
>
> This sounds like exactly the sort of situation ExitStack was designed
> for. I'm not sure the situation is common enough to need dedicated
> syntax beyond that. I actually find the ExitStack version easier to
> understand, as the condition is more visible when it's not tucked away
> at the end of the with statement.
>
> If compactness is that important, you could refactor the code to use a
> custom context manager that encapsulates the "cm_function if condition
> else nothing" pattern.
>
> Paul