utils: Add unique_everseen (from itertools recipies).

Signed-off-by: Antony MECHIN <antony.mechin@docker.com>
This commit is contained in:
Antony MECHIN 2018-09-24 18:08:17 +02:00 committed by Silvin Lubecki
parent bb87a3d040
commit 18c2d08011
2 changed files with 15 additions and 0 deletions

View File

@ -170,3 +170,12 @@ def truncate_id(value):
if len(value) > 12:
return value[:12]
return value
def unique_everseen(iterable, key=lambda x: x):
"List unique elements, preserving order. Remember all elements ever seen."
seen = set()
for element in iterable:
if key(element) not in seen:
seen.add(element)
yield element

View File

@ -68,3 +68,9 @@ class TestParseBytes(object):
assert utils.parse_bytes(123) == 123
assert utils.parse_bytes('foobar') is None
assert utils.parse_bytes('123') == 123
class TestMoreItertools(object):
def test_unique_everseen(self):
assert list(utils.unique_everseen([2, 1, 2, 1])) == [2, 1]
assert list(utils.unique_everseen([2, 1, 2, 1], hash)) == [2, 1]