Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attribute': 0.05; 'case.': 0.05; 'posting.': 0.09; 'yeah,': 0.09; 'def': 0.10; 'thread': 0.11; 'index': 0.13; 'dec': 0.15; 'alexander': 0.16; 'empty.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'indexerror:': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'string': 0.17; 'wrote:': 0.17; '>>>': 0.18; 'supposed': 0.21; '"",': 0.22; 'subject:skip:i 10': 0.22; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'skip:[ 10': 0.26; '(most': 0.27; 'am,': 0.27; 'post': 0.28; '>>>>': 0.29; 'received:192.168.1.3': 0.29; "i'm": 0.29; 'file': 0.32; 'traceback': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'version': 0.34; 'fail': 0.35; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'description': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'easily': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'range': 0.60; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'saw': 0.75; 'reply-to:addr:python.org': 0.84; 'you;': 0.84; 'subject:Good': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=TdYURGsh 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=3bUo5-wZAAAA:8 a=KpT6H3N6i9m1v7eK_ykA:9 a=wPNLvfGTeEIA:10 a=O0U4b4btQtkA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Tue, 04 Dec 2012 20:17: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: <50be3049$0$9517$9b4e6d93@newsspool1.arcor-online.net> <50be4566$0$9507$9b4e6d93@newsspool1.arcor-online.net> 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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354652248 news.xs4all.nl 6931 [2001:888:2000:d::a6]:36921 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34250 On 2012-12-04 19:37, Ian Kelly wrote: > On Tue, Dec 4, 2012 at 11:48 AM, Alexander Blinne > wrote: > > Am 04.12.2012 19:28, schrieb DJC: > >>>> (i for i,v in enumerate(w) if v.upper() != v).next() > > Traceback (most recent call last): > > File "", line 1, in > > AttributeError: 'generator' object has no attribute 'next' > > Yeah, i saw this problem right after i sent the posting. It now is > supposed to read like this > > >>> def split_product(p): > ... w = p.split(" ") > ... j = next(i for i,v in enumerate(w) if v.upper() != v) > ... return " ".join(w[:j]), " ".join(w[j:]) > > > It still fails if the product description is empty. > > >>> split_product("CAPSICUM RED") > Traceback (most recent call last): > File "", line 1, in > File "", line 3, in split_product > StopIteration > > I'm not meaning to pick on you; some of the other solutions in this > thread also fail in that case. > > >>> re.findall(r"(?m)^([A-Z\s]+) (.+)$", "CAPSICUM RED") > [('CAPSICUM', 'RED')] > That's easily fixed: >>> re.findall(r"(?m)^([A-Z\s]+)(?: (.*))?$", "CAPSICUM RED") [('CAPSICUM RED', '')] > >>> prod_desc("CAPSICUM RED") # the second version from Neil's post > Traceback (most recent call last): > File "", line 1, in > File "", line 14, in prod_desc > IndexError: string index out of range >