Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'that?': 0.05; 'val': 0.07; 'iterate': 0.09; 'raised,': 0.09; 'def': 0.10; 'dec': 0.15; 'cleaner': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'iterable': 0.16; 'iterable:': 0.16; 'iterator': 0.16; 'iterator.': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'nick': 0.16; 'object()': 0.16; 'partitioning': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'sequence,': 0.16; 'sequence.': 0.16; 'true:': 0.16; 'twice.': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'odd': 0.17; 'yield': 0.17; 'skip:" 30': 0.20; 'subject:skip:i 10': 0.22; 'this:': 0.23; 'seems': 0.23; 'raise': 0.24; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'chris': 0.28; 'received:192.168.1.3': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'received:84': 0.32; 'could': 0.32; 'to:addr :python-list': 0.33; 'fresh': 0.35; 'sequence': 0.35; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'next': 0.35; 'but': 0.36; 'skip:p 20': 0.36; 'item': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'description': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'end': 0.40; 'think': 0.40; 'red': 0.60; 'kind': 0.61; 'skip:n 10': 0.63; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'ask,': 0.84; 'object:': 0.84; 'reply-to:addr:python.org': 0.84; 'drops': 0.91; 'subject:Good': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=BusfMPr5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=O2Kvzccb_dQA:10 a=2HYzhsMY58wA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=8qMxVT5i32MA:10 a=pGLkceISAAAA:8 a=Mp2tdt9SQ3vPMpyuRmAA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Wed, 05 Dec 2012 17:57:19 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Good use for itertools.dropwhile and itertools.takewhile References: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354730246 news.xs4all.nl 6879 [2001:888:2000:d::a6]:44668 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34300 On 2012-12-05 13:45, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor wrote: >> >> takewhile mines for gold at the start of a sequence, dropwhile drops the dross at the start of a sequence. > > When you're using both over the same sequence and with the same > condition, it seems odd that you need to iterate over it twice. > Perhaps a partitioning iterator would be cleaner - something like > this: > > def partitionwhile(predicate, iterable): > iterable = iter(iterable) > while True: > val = next(iterable) > if not predicate(val): break > yield val > raise StopIteration # Signal the end of Phase 1 > for val in iterable: yield val # or just "yield from iterable", I think > > Only the cold hard boot of reality just stomped out the spark of an > idea. Once StopIteration has been raised, that's it, there's no > "resuming" the iterator. Is there a way around that? Is there a clean > way to say "Done for now, but next time you ask, there'll be more"? > Perhaps you could have some kind of partitioner object: class Partitioner: _SENTINEL = object() def __init__(self, iterable): self._iterable = iter(iterable) self._unused_item = self._SENTINEL def takewhile(self, condition): if self._unused_item is not self._SENTINEL: if not condition(self._unused_item): raise StopIteration yield self._unused_item self._unused_item = self._SENTINEL for item in self._iterable: if not condition(item): self._unused_item = item break yield item raise StopIteration def remainder(self): if self._unused_item is not self._SENTINEL: yield self._unused_item self._unused_item = self._SENTINEL for item in self._iterable: yield item raise StopIteration def is_all_caps(word): return word == word.upper() part = Partitioner("CAPSICUM RED fresh from QLD".split()) product = " ".join(part.takewhile(is_all_caps)) description = " ".join(part.remainder()) print([product, description])