'Helper function to support returning parts of a selection, for
example, for use in a web app where only a part of the results
are to be presented on each screen.'

def __paginate(self, data, page=None, items_per_page=None, token=None):

    default_page = 1
    default_items_per_page = 25
    try:
        page = int(page)
        if (page < 1):
            page = default_page
    except:
        page = default_page
    try:
        items_per_page = int(items_per_page)
        if (items_per_page <= 0):
            items_per_page = default_items_per_page
    except:
        items_per_page = default_items_per_page
    num_items = len(data)
    num_pages = (((num_items - 1) / items_per_page) + 1)
    if (num_pages == 0):
        num_pages = 1
    if (page > num_pages):
        page = num_pages
    start_item = (items_per_page * (page - 1))
    end_item = (start_item + items_per_page)
    if (start_item > num_items):
        start_item = (num_items - 1)
    if (end_item > num_items):
        end_item = num_items
    data = data[start_item:end_item]
    if (page > 1):
        prev_page = (page - 1)
    else:
        prev_page = None
    if (page < num_pages):
        next_page = (page + 1)
    else:
        next_page = None
    return (data, {'page': page, 'prev_page': prev_page, 'next_page': next_page, 'pages': range(1, (num_pages + 1)), 'num_pages': num_pages, 'num_items': num_items, 'start_item': start_item, 'end_item': end_item, 'items_per_page': items_per_page, 'items_per_page_list': [10, 20, 50, 100, 200, 500]})
