Login

The chunkmaker

Author:
pbx
Posted:
May 11, 2007
Language:
Python
Version:
.96
Score:
4 (after 4 ratings)

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

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.