Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'slice': 0.07; 'slices': 0.07; 'terry': 0.07; 'assert': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'string)': 0.09; 'successive': 0.09; 'pm,': 0.10; 'output': 0.11; 'def': 0.12; 'wrote:': 0.14; 'input,': 0.16; 'n):': 0.16; 'reedy': 0.16; 'input': 0.17; 'yield': 0.19; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; 'subject:question': 0.23; 'indexing': 0.23; 'item.': 0.23; 'items.': 0.23; 'code': 0.24; '(and': 0.25; 'function': 0.25; 'skip:[ 10': 0.26; 'forgot': 0.29; 'class': 0.29; 'separate': 0.31; 'header:X-Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; 'lines': 0.33; 'rather': 0.34; 'however,': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'changes.': 0.35; 'couple': 0.35; 'test': 0.35; 'takes': 0.37; 'change': 0.37; 'checks': 0.37; 'sequence': 0.37; 'received:org': 0.38; 'could': 0.38; 'positive': 0.38; 'subject:: ': 0.38; 'case,': 0.39; 'should': 0.39; 'header :Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'current': 0.40; 'other.': 0.63; 'unique': 0.63; 'learned': 0.73; '12:52': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Lambda question Date: Mon, 06 Jun 2011 22:08:47 -0400 References: <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list 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: 31 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307412539 news.xs4all.nl 49178 [::ffff:82.94.164.166]:35055 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7127 On 6/6/2011 12:52 PM, Terry Reedy wrote: > def group(seq, n): > 'Yield from seq successive disjoint slices of length n & the remainder' > if n<=0: raise ValueError('group size must be positive') > for i in range(0,len(seq), n): > yield seq[i:i+n] > > for inn,out in ( > (('',1), []), # no input, no output > #(('abc',0), ValueError), # group size positive > (('abc',1), ['a','b','c']), > (('abcd',2), ['ab','cd']), > (('abcde',2), ['ab', 'cd', 'e']), # could change this > ): > assert list(group(*inn)) == out, (inn,out) I forgot to mention that any function that takes a 'sequence' as input should be tested with both strings and non-strings. I learned this when a function tested with one failed mysteriously with the other. Strings are unique in that indexing returns a slice (and hence a string) rather than a separate class of item. It this case, there is no indexing and no use of class to create new items. However, adding a couple of lines like (((),1), []), (((1,2,3,4),2), [(1,2), (3,4)]), to the test sequence checks the current code and guards against future changes. -- Terry Jan Reedy