Add an utils.newest_slice.

This takes an iterable and uses itertools.islice to get the n newest elements
from it.
This commit is contained in:
Florian Bruhin 2015-03-13 19:25:48 +01:00
parent 96da7d9fe6
commit 94f694bd77
2 changed files with 72 additions and 0 deletions

View File

@ -404,5 +404,57 @@ class ForceEncodingTests(unittest.TestCase):
self.assertEqual(utils.force_encoding(text, 'ascii'), 'hell? w?rld') self.assertEqual(utils.force_encoding(text, 'ascii'), 'hell? w?rld')
class NewestSliceTests(unittest.TestCase):
"""Test newest_slice."""
def test_count_minus_two(self):
"""Test with a count of -2."""
with self.assertRaises(ValueError):
utils.newest_slice([], -2)
def test_count_minus_one(self):
"""Test with a count of -1 (all elements)."""
items = range(20)
sliced = utils.newest_slice(items, -1)
self.assertEqual(list(sliced), list(items))
def test_count_zero(self):
"""Test with a count of 0 (no elements)."""
items = range(20)
sliced = utils.newest_slice(items, 0)
self.assertEqual(list(sliced), [])
def test_count_much_smaller(self):
"""Test with a count which is much smaller than the iterable."""
items = range(20)
sliced = utils.newest_slice(items, 5)
self.assertEqual(list(sliced), [15, 16, 17, 18, 19])
def test_count_smaller(self):
"""Test with a count which is exactly one smaller."""
items = range(5)
sliced = utils.newest_slice(items, 4)
self.assertEqual(list(sliced), [1, 2, 3, 4])
def test_count_equal(self):
"""Test with a count which is just as large as the iterable."""
items = range(5)
sliced = utils.newest_slice(items, 5)
self.assertEqual(list(sliced), list(items))
def test_count_bigger(self):
"""Test with a count which is one bigger than the iterable."""
items = range(5)
sliced = utils.newest_slice(items, 6)
self.assertEqual(list(sliced), list(items))
def test_count_much_bigger(self):
"""Test with a count which is much bigger than the iterable."""
items = range(5)
sliced = utils.newest_slice(items, 50)
self.assertEqual(list(sliced), list(items))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -27,6 +27,7 @@ import os.path
import collections import collections
import functools import functools
import contextlib import contextlib
import itertools
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtGui import QKeySequence, QColor from PyQt5.QtGui import QKeySequence, QColor
@ -552,3 +553,22 @@ def force_encoding(text, encoding):
This replaces all chars not encodable with question marks. This replaces all chars not encodable with question marks.
""" """
return text.encode(encoding, errors='replace').decode(encoding) return text.encode(encoding, errors='replace').decode(encoding)
def newest_slice(iterable, count):
"""Get an iterable for the n newest items of the given iterable.
Args:
count: How many elements to get.
0: get no items:
n: get the n newest items
-1: get all items
"""
if count < -1:
raise ValueError("count can't be smaller than -1!")
elif count == 0:
return []
elif count == -1 or len(iterable) < count:
return iterable
else:
return itertools.islice(iterable, len(iterable) - count, len(iterable))