Login

The chunkmaker

Author:
pbx
Posted:
May 11, 2007
Language:
Python
Version:
.96
Tags:
python utility sequence itertools
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. Pull ID from arbitrary sequence by nirvdrum 7 years, 9 months ago
  2. Group sequence into rows and columns for a TABLE by davidwtbuxton 4 years, 2 months ago
  3. listutils.py by statico 5 years, 3 months ago
  4. MultipleChoiceCommaField by rubic 8 years, 1 month ago
  5. Table by Samus 7 years, 10 months ago

Comments

Please login first before commenting.