This is a general-purpose utility function, but since it uses lazy sequences via itertools, so it should be suitable for use with Querysets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | def chunked(seq, size, overflow=False):
"""
Chunk the sequence `seq` into chunks of `size`. It it's a string,
return a sequence of strings. Otherwise, return a sequence of tuples.
The returned object is a generator:
>>> chunked("abcDEFghiJKL", 3)
<generator object at 0x...>
But for the sake of testing and example, we'll make lists:
>>> list(chunked("abcDEFghiJKL", 3))
['abc', 'DEF', 'ghi', 'JKL']
>>> list(chunked("abcDEFghiJKL", 9))
['abcDEFghi']
>>> list(chunked("abcDEFghiJKL", 9, overflow=True))
['abcDEFghi', 'JKL']
>>> list(chunked(range(15), 5))
[(0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, 11, 12, 13, 14)]
"""
from itertools import izip, chain
chunks = izip(*([iter(seq)] * size))
modulus = len(seq) % size
if overflow and modulus:
chunks = chain(chunks, [seq[-modulus:]])
if isinstance(seq, basestring):
return (''.join(c) for c in chunks)
else:
return (tuple(c) for c in chunks)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.