

def paginate(items, page=0, max_per_page=10):
    'Simple generic pagination.\n\n    Given an iterable, this function returns:\n     * the slice of objects on the requested page,\n     * the total number of items, and\n     * the total number of pages.\n\n    The `items` parameter can be a list, tuple, or iterator:\n\n    >>> items = range(12)\n    >>> items\n    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n    >>> paginate(items)\n    ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 12, 2)\n    >>> paginate(items, page=1)\n    ([10, 11], 12, 2)\n    >>> paginate(iter(items))\n    ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 12, 2)\n    >>> paginate(iter(items), page=1)\n    ([10, 11], 12, 2)\n\n    This function also works with generators:\n\n    >>> def generate():\n    ...     for idx in range(12):\n    ...         yield idx\n    >>> paginate(generate())\n    ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 12, 2)\n    >>> paginate(generate(), page=1)\n    ([10, 11], 12, 2)\n\n    The `max_per_page` parameter can be used to set the number of items that\n    should be displayed per page:\n\n    >>> items = range(12)\n    >>> paginate(items, page=0, max_per_page=6)\n    ([0, 1, 2, 3, 4, 5], 12, 2)\n    >>> paginate(items, page=1, max_per_page=6)\n    ([6, 7, 8, 9, 10, 11], 12, 2)\n\n    :raises TracError: if `page` is out of the range of the paginated\n                       results.\n    '
    if (not page):
        page = 0
    start = (page * max_per_page)
    stop = (start + max_per_page)
    count = None
    if hasattr(items, '__len__'):
        count = len(items)
        if (count and (start >= count)):
            from trac.util.translation import _
            raise TracError(_('Page %(page)s is out of range.', page=page))
    try:
        retval = items[start:stop]
    except TypeError:
        retval = []
        idx = (- 1)
        for (idx, item) in enumerate(items):
            if (start <= retval < stop):
                retval.append(item)
            if ((count is not None) and (idx >= stop)):
                break
        if (count is None):
            count = (idx + 1)
    return (retval, count, int(ceil((float(count) / max_per_page))))

dataset/ETHPy150Open edgewall/trac/trac/util/presentation.py paginate/VarMisuse@156/840 `idx`->`retval`