askbot.utils.lists

Utilities for working with lists and sequences.

class askbot.utils.lists.LazyList(get_data)

Bases: list

askbot.utils.lists.batch_size(items, size)

Retrieves items in batches of the given size.

>>> l = range(1, 11)
>>> batch_size(l, 3)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
>>> batch_size(l, 5)
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
askbot.utils.lists.batches(items, number)

Retrieves items in the given number of batches.

>>> l = range(1, 11)
>>> batches(l, 1)
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
>>> batches(l, 2)
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
>>> batches(l, 3)
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]]
>>> batches(l, 4)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
>>> batches(l, 5)
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]

Initial batches will contain as many items as possible in cases where there are not enough items to be distributed evenly.

>>> batches(l, 6)
[[1, 2], [3, 4], [5, 6], [7, 8], [9], [10]]
>>> batches(l, 7)
[[1, 2], [3, 4], [5, 6], [7], [8], [9], [10]]
>>> batches(l, 8)
[[1, 2], [3, 4], [5], [6], [7], [8], [9], [10]]
>>> batches(l, 9)
[[1, 2], [3], [4], [5], [6], [7], [8], [9], [10]]
>>> batches(l, 10)
[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]

If there are more batches than items, empty batches will be appended to the batch list.

>>> batches(l, 11)
[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], []]
>>> batches(l, 12)
[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [], []]
askbot.utils.lists.flatten(x)

Returns a single, flat list which contains all elements retrieved from the sequence and all recursively contained sub-sequences (iterables).

Examples: >>> [1, 2, [3, 4], (5, 6)] [1, 2, [3, 4], (5, 6)]

From http://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks

This Page