firmant.paginate

Paginate or group objects according to certain properties.

Functions

firmant.paginate.paginate(num_per_page, obj_list)

Break obj_list into lists of at most num_per_page objects.

>>> pprint(paginate(1, [1, 2, 3, 4, 5, 6, 7]))
[([1], Paginated(None, 1, 2)),
 ([2], Paginated(1, 2, 3)),
 ([3], Paginated(2, 3, 4)),
 ([4], Paginated(3, 4, 5)),
 ([5], Paginated(4, 5, 6)),
 ([6], Paginated(5, 6, 7)),
 ([7], Paginated(6, 7, None))]
>>> pprint(paginate(2, [1, 2, 3, 4, 5, 6, 7]))
[([1, 2], Paginated(None, 1, 2)),
 ([3, 4], Paginated(1, 2, 3)),
 ([5, 6], Paginated(2, 3, 4)),
 ([7], Paginated(3, 4, None))]
>>> paginate(2, [])
[]
>>> paginate(2, [1])
[([1], Paginated(None, 1, None))]
firmant.paginate.split_list(key_func, obj_list)

Split obj_list at boundaries determined by key_func.

At each point the return value of key_func(obj) changes, split obj_list. The return value is a list of these split lists.

The function’s behavior is undefined if key_func ever returns None (If you’re tempted to return None anyways, read this statement as “Your code will break if key_func ever returns None“).

>>> def parity(x):
...     return x % 2
>>> pprint(split_list(parity, [1, 3, 5, 2, 4, 7, 8, 9, 10]))
[([1, 3, 5], Paginated(None, 1, 0)),
 ([2, 4], Paginated(1, 0, 1)),
 ([7], Paginated(0, 1, 0)),
 ([8], Paginated(1, 0, 1)),
 ([9], Paginated(0, 1, 0)),
 ([10], Paginated(1, 0, None))]
>>> split_list(parity, [])
[]
>>> split_list(parity, [1])
[([1], Paginated(None, 1, None))]
firmant.paginate.split_paginate(num_per_page, key_func, obj_list)

Split obj_list with split_list; then use paginate.

The lists will be split according to split_list. Each of the resulting lists will then be passed to paginate. The result will be a nested datastructure that is best explained by example.

>>> def parity(x):
...     return x % 2
>>> pprint(split_paginate(2, parity, [1, 3, 5, 7, 9, 2, 4, 7, 9, 10]))
[([1, 3], Paginated(None, 1, 0), Paginated(None, 1, 2)),
 ([5, 7], Paginated(None, 1, 0), Paginated(1, 2, 3)),
 ([9], Paginated(None, 1, 0), Paginated(2, 3, None)),
 ([2, 4], Paginated(1, 0, 1), Paginated(None, 1, None)),
 ([7, 9], Paginated(0, 1, 0), Paginated(None, 1, None)),
 ([10], Paginated(1, 0, None), Paginated(None, 1, None))]
>>> split_paginate(2, parity, [])
[]
>>> split_paginate(2, parity, [1])
[([1], Paginated(None, 1, None), Paginated(None, 1, None))]
>>> pprint(split_paginate(2, parity, [1, 2]))
[([1], Paginated(None, 1, 0), Paginated(None, 1, None)),
 ([2], Paginated(1, 0, None), Paginated(None, 1, None))]

Classes

class firmant.paginate.Paginated(prev, cur, nex)

Bases: object

An object representing keys in paginated lists.

The key used for each of the lists is stored in the properties prev, cur, next. Instances are just nice containers in place of 3-tuples:

.. doctest::
>>> p = Paginated(1, 2, 3)
>>> p
Paginated(1, 2, 3)
>>> p.prev
1
>>> p.cur
2
>>> p.next
3
cur
The key of the current list.
next
The key of the next list.
prev
The key of the previous list.

Table Of Contents

Previous topic

firmant.globs

Next topic

firmant.parsers

This Page