def _fragment_in_reverse(iterable, start=0, limit=None):
"""
Selects a fragment of the iterable and returns the items in reverse order.
The lowest index is 0 and designates the last line of the file.
'Limit' specifies the number of lines to return.
"""
# TODO: Naive implementation. Needs to be rewritten to a solution with
# file pointer moving around etc. if it turns out that this isn't
# performant enough.
maxlen = None
if limit >= 0:
maxlen = start + limit
fragment = collections.deque(iterable, maxlen)
try:
for _ in six.moves.range(start):
fragment.pop()
except IndexError:
raise exc.InvalidInputError(__name__,
'Index start=%s is out of range.'
% str(start))
fragment.reverse()
return fragment
评论列表
文章目录