"""
A :py:mod:`filter <tiddlyweb.filters>` type to limit a group of entities
using a syntax similar to SQL Limit::

    limit=<index>,<count>
    limit=<count>
"""

import itertools


def limit_parse(count='0'):
    """
    Parse the argument of a ``limit`` :py:mod:`filter <tiddlyweb.filters>`
    for a count and index argument, return a function which does the limiting.

    Exceptions while parsing are passed up the stack.
    """
    index = '0'
    if ',' in count:
        index, count = count.split(',', 1)
    index = int(index)
    count = int(count)

    def limiter(entities, indexable=False, environ=None):
        return limit(entities, index=index, count=count)

    return limiter


def limit(entities, count=0, index=0):
    """
    Make a slice of a list of entities based on a count and index.
    """

    return itertools.islice(entities, index, index + count)

[((36, 11, 36, 59), 'itertools.islice', 'itertools.islice', ({(36, 28, 36, 36): 'entities', (36, 38, 36, 43): 'index', (36, 45, 36, 58): '(index + count)'}, {}), '(entities, index, index + count)', False, 'import itertools\n')]