[Python-ideas] itertools.chunks()
Carlo Pires
carlopires at gmail.com
Sat Apr 6 16:21:59 CEST 2013
+1
Very useful function.
2013/4/6 Giampaolo RodolĂ <g.rodola at gmail.com>
> def chunks(total, step):
> assert total >= step
> while total > step:
> yield step;
> total -= step;
> if total:
> yield total
>
> >>> chunks(12, 4)
> [4, 4, 4]
> >>> chunks(13, 4)
> [4, 4, 4, 1]
>
>
> I'm not sure how appropriate "chunks" is as a name for such a function.
> Anyway, I wrote that because in a unit test I had to create a file of
> a precise size, like this:
>
> FILESIZE = (10 * 1024 * 1024) + 423 # 10MB and 423 bytes
> with open(TESTFN, 'wb') as f:
> for csize in chunks(FILESIZE, 262144):
> f.write(b'x' * csize)
>
> Now I wonder, would it make sense to have something like this into
> itertools module?
>
>
> --- Giampaolo
> https://code.google.com/p/pyftpdlib/
> https://code.google.com/p/psutil/
> https://code.google.com/p/pysendfile/
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
--
Carlo Pires
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130406/a77de1b0/attachment.html>
More information about the Python-ideas
mailing list