Re: Is this PEP-able? fwhile

Discussion in 'Python' started by Ian Kelly, Jun 24, 2013.

  1. Ian Kelly

    Ian Kelly Guest

    On Mon, Jun 24, 2013 at 1:52 PM, <> wrote:
    > Syntax:
    >
    > fwhile X in ListY and conditionZ:
    >
    > The following would actually exactly as: for X in ListY:
    >
    > fwhile X in ListY and True:
    >
    > fwhile would act much like 'for', but would stop if the condition after the
    > 'and' is no longer True.
    >
    > The motivation is to be able to make use of all the great aspects of the
    > python 'for' (no indexing or explict
    > end condition check, etc.) and at the same time avoiding a 'break' from the
    > 'for'.


    I would advocate using the break myself. Another alternative is this:

    for X in itertools.takewhile(lambda X: conditionZ, ListY):
    ...
    Ian Kelly, Jun 24, 2013
    #1
    1. Advertising

  2. Ian Kelly

    alex23 Guest

    On 25/06/2013 6:12 AM, Ian Kelly wrote:
    > On Mon, Jun 24, 2013 at 1:52 PM, <> wrote:
    >> Syntax:
    >> fwhile X in ListY and conditionZ:
    >>
    >> fwhile would act much like 'for', but would stop if the condition after the
    >> 'and' is no longer True.

    >
    > I would advocate using the break myself. Another alternative is this:
    >
    > for X in itertools.takewhile(lambda X: conditionZ, ListY):
    > ...


    I'd probably just go with a generator expression to feed the for loop:

    for X in (i for i in ListY if conditionZ):
    ....
    alex23, Jun 25, 2013
    #2
    1. Advertising

  3. On 25 Jun 2013 00:31, "alex23" <> wrote:
    >
    > On 25/06/2013 6:12 AM, Ian Kelly wrote:
    >>
    >> On Mon, Jun 24, 2013 at 1:52 PM, <> wrote:
    >>>
    >>> Syntax:
    >>> fwhile X in ListY and conditionZ:
    >>>
    >>> fwhile would act much like 'for', but would stop if the condition after

    the
    >>> 'and' is no longer True.

    >>
    >>
    >> I would advocate using the break myself. Another alternative is this:
    >>
    >> for X in itertools.takewhile(lambda X: conditionZ, ListY):
    >> ...

    >
    >
    > I'd probably just go with a generator expression to feed the for loop:
    >
    > for X in (i for i in ListY if conditionZ):
    > ....


    That is nice but it's not lazy. If the condition or the iterables took too
    long to compute, it would be troublesome.
    Fábio Santos, Jun 25, 2013
    #3
  4. Ian Kelly

    alex23 Guest

    On 25/06/2013 9:35 AM, Fábio Santos wrote:
    > > I'd probably just go with a generator expression to feed the for loop:
    > >
    > > for X in (i for i in ListY if conditionZ):
    > > ....

    >
    > That is nice but it's not lazy. If the condition or the iterables took
    > too long to compute, it would be troublesome.


    I'm not sure I follow. It's a generator expression, not a list
    comprehension, so the condition will be evaluated per item iterated over
    in the generator, not across all valid items in ListY at once.
    alex23, Jun 25, 2013
    #4
  5. On 25 Jun 2013 01:08, "alex23" <> wrote:
    >
    > On 25/06/2013 9:35 AM, Fábio Santos wrote:
    >>
    >> > I'd probably just go with a generator expression to feed the for loop:
    >> >
    >> > for X in (i for i in ListY if conditionZ):
    >> > ....

    >>
    >> That is nice but it's not lazy. If the condition or the iterables took
    >> too long to compute, it would be troublesome.

    >
    >
    > I'm not sure I follow. It's a generator expression, not a list

    comprehension, so the condition will be evaluated per item iterated over in
    the generator, not across all valid items in ListY at once.
    >


    for X in (i for i in open('largefile') if is_part_of_header(i)):

    The above code would be wasting time on IO and processing. It would load
    another line and calculate the condition for every line of the large file
    and I just wanted to loop over the few header lines.

    itertools.takewhile and fwhile/for..while actually stops the loop when the
    condition is not meant, while your example keeps checking the condition
    until the end of file, even though it is a generator expression.
    Fábio Santos, Jun 25, 2013
    #5
  6. Ian Kelly

    wu wei Guest

    On Tue, Jun 25, 2013 at 10:19 AM, Fábio Santos <>
    wrote:

    > for X in (i for i in open('largefile') if is_part_of_header(i)):
    >
    > The above code would be wasting time on IO and processing. It would load
    > another line and calculate the condition for every line of the large file
    > and I just wanted to loop over the few header lines.
    >
    > itertools.takewhile and fwhile/for..while actually stops the loop when the
    > condition is not meant, while your example keeps checking the condition
    > until the end of file, even though it is a generator expression.
    >

    Ah yes, of course, my bad.

    It's still possible by raising a StopIteration within the condition
    function:

    def is_part_of_header(x):
    if header_condition:
    return True
    else:
    raise StopIteration

    But yes, by this point any clarity of the generator expression approach
    comes with the cost of more explicit setup of the breaking condition.
    wu wei, Jun 25, 2013
    #6
  7. Ian Kelly

    Ian Kelly Guest

    On Mon, Jun 24, 2013 at 6:41 PM, wu wei <> wrote:
    > It's still possible by raising a StopIteration within the condition
    > function:
    >
    > def is_part_of_header(x):
    > if header_condition:
    > return True
    > else:
    > raise StopIteration


    Which is basically just taking the break and moving it to somewhere
    else in the code, and meanwhile rendering the is_part_of_header
    function non-reusable.
    Ian Kelly, Jun 25, 2013
    #7
  8. Ian Kelly

    rusi Guest

    On Tuesday, June 25, 2013 4:44:44 AM UTC+5:30, alex23 wrote:
    > I'd probably just go with a generator expression to feed the for loop:
    >
    > for X in (i for i in ListY if conditionZ):
    >
    > ....


    Nice idiom -- thanks
    Yes it does not correspond to a takewhile (or break in the control structure world). It does correspond to a filter
    rusi, Jun 25, 2013
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    93
    Chris Angelico
    Jun 26, 2013
  2. Ian Kelly

    Re: Is this PEP-able? fwhile

    Ian Kelly, Jun 24, 2013, in forum: Python
    Replies:
    0
    Views:
    91
    Ian Kelly
    Jun 24, 2013
  3. Fábio Santos

    Re: Is this PEP-able? fwhile

    Fábio Santos, Jun 24, 2013, in forum: Python
    Replies:
    0
    Views:
    96
    Fábio Santos
    Jun 24, 2013
  4. Joshua Landau

    Re: Is this PEP-able? fwhile

    Joshua Landau, Jun 24, 2013, in forum: Python
    Replies:
    0
    Views:
    93
    Joshua Landau
    Jun 24, 2013
  5. Replies:
    0
    Views:
    77
Loading...

Share This Page