Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'subject:not': 0.03; '"this': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'argument': 0.05; 'elif': 0.05; 'sized': 0.07; 'arguments': 0.09; 'chunk': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rejected': 0.09; 'subject:Why': 0.09; 'def': 0.12; '3],': 0.16; 'benjamin': 0.16; 'chunks': 0.16; 'function).': 0.16; 'iterator': 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'url:312443': 0.16; 'url:djangoproject': 0.16; 'url:how-do-you- split-a-list-into-evenly-sized-chunks-in-python': 0.16; 'subject:python': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'library': 0.18; 'split': 0.19; '>>>': 0.22; 'python?': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'url:moin': 0.24; 'pass': 0.26; 'post': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'url:bugs': 0.29; 'raise': 0.29; 'url:code': 0.29; "doesn't": 0.30; "i'm": 0.30; 'asked': 0.31; "skip:' 10": 0.31; 'url:wiki': 0.31; 'extend': 0.32; 'url:python': 0.33; '(e.g.': 0.33; 'cases': 0.33; 'subject:the': 0.34; 'could': 0.34; 'skip:u 20': 0.35; 'something': 0.35; 'shorter': 0.36; 'yield': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'list': 0.37; 'url:library': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'quote': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'skip:z 20': 0.60; 'length': 0.61; 'skip:* 10': 0.61; 'simply': 0.61; "you're": 0.61; 'kind': 0.63; 'different': 0.65; 'charset:windows-1252': 0.65; 'overcome': 0.74; 'behavior': 0.77; 'given.': 0.84; 'oscar': 0.84; 'received:2': 0.84; 'url:ticket': 0.84; 'mistakes': 0.93; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Why chunks is not part of the python standard lib? Date: Wed, 01 May 2013 14:06:15 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: host-2-98-196-142.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 In-Reply-To: X-Antivirus: avast! (VPS 130501-0, 01/05/2013), Outbound message X-Antivirus-Status: Clean X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367413590 news.xs4all.nl 15896 [2001:888:2000:d::a6]:50304 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44590 On 01/05/2013 10:00, Oscar Benjamin wrote: > On 1 May 2013 08:10, Mark Lawrence wrote: >> On 01/05/2013 07:26, Ricardo Azpeitia Pimentel wrote: >>> >>> After reading How do you split a list into evenly sized chunks in >>> Python? >>> >>> >>> >>> and seeing this kind of mistakes happening >>> https://code.djangoproject.com/ticket/18972 all the time. >>> >>> Why is not a |chunks| function in itertools? >>> >>> |grouper| from >>> http://docs.python.org/2/library/itertools.html#recipes doesn't have the >>> same behavior as |chunks | >>> >>> Example: >>> | >>> >>> |chunks([1, 2, 3, 4, 5], 3) >>> # Should return [[1, 2, 3], [4, 5]] or the iterator equivalent.| >>> >>> |Original Post on StackOverflow: >>> >>> http://stackoverflow.com/questions/16313008/why-chunks-is-not-part-of-the-python-standard-lib >>> >> >> Asked and answered a trillion times. There's no concensus on how chucks >> should behave. > > I'm not sure that's a valid argument against it since a chunks > function could just do a different thing depending on the arguments > given. > > The issue is around how to deal with the last chunk if it isn't the > same length as the others and I can only think of 4 reasonable > responses: > > 1) Yield a shorter chunk > 2) Extend the chunk with fill values > 3) Raise an error > 4) Ignore the last chunk > > Cases 2 and 4 can be achieved with current itertools primitives e.g.: > 2) izip_longest(fillvalue=fillvalue, *[iter(iterable)] * n) > 4) zip(*[iter(iterable)] * n) > > However I have only ever had use cases for 1 and 3 and these are not > currently possible without something additional (e.g. a generator > function). > > In any case a chunks function can simply take arguments to give all 4 > behaviours: > > def chunks(iterable, chunksize, uneven='return_short', fillvalue=None): > # loop through yielding all even chunks > # and then > if uneven == 'return_short: > yield chunk > elif uneven == 'raise': > raise ValueError('No items left') > elif uneven == 'fill': > yield chunk + [fillvalue] * (chunksize - len(chunk)) > elif uneven == 'ignore': > pass > > > Oscar > All that's needed to get this into the standard library is to overcome this quote "This has been rejected before." from http://bugs.python.org/issue6021#msg87745 -- If you're using GoogleCrap™ please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence