Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49115
| References | (1 earlier) <mailman.3760.1372104769.3114.python-list@python.org> <kqajj8$54h$1@dont-email.me> <mailman.3788.1372116912.3114.python-list@python.org> <kqam8g$f7n$1@dont-email.me> <CAA=1kxR0sMf2MT-UniaVsEY9pUhc0Tvq+O2aHc6ZBY=Gjt1hOg@mail.gmail.com> |
|---|---|
| Date | 2013-06-25 10:41 +1000 |
| Subject | Re: Is this PEP-able? fwhile |
| From | wu wei <wuwei23@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3791.1372120877.3114.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On Tue, Jun 25, 2013 at 10:19 AM, Fábio Santos <fabiosantosart@gmail.com>
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Is this PEP-able? fwhile Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-24 14:12 -0600
Re: Is this PEP-able? fwhile alex23 <wuwei23@gmail.com> - 2013-06-25 09:14 +1000
Re: Is this PEP-able? fwhile Fábio Santos <fabiosantosart@gmail.com> - 2013-06-25 00:35 +0100
Re: Is this PEP-able? fwhile alex23 <wuwei23@gmail.com> - 2013-06-25 10:00 +1000
Re: Is this PEP-able? fwhile Fábio Santos <fabiosantosart@gmail.com> - 2013-06-25 01:19 +0100
Re: Is this PEP-able? fwhile wu wei <wuwei23@gmail.com> - 2013-06-25 10:41 +1000
Re: Is this PEP-able? fwhile Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-24 22:50 -0600
Re: Is this PEP-able? fwhile rusi <rustompmody@gmail.com> - 2013-06-24 23:04 -0700
csiph-web