From 18c2d08011fbf7f97d574f10f84ef68717d68ad8 Mon Sep 17 00:00:00 2001 From: Antony MECHIN Date: Mon, 24 Sep 2018 18:08:17 +0200 Subject: [PATCH] utils: Add unique_everseen (from itertools recipies). Signed-off-by: Antony MECHIN --- compose/utils.py | 9 +++++++++ tests/unit/utils_test.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/compose/utils.py b/compose/utils.py index 8f0b3e549..b9b6ab9bd 100644 --- a/compose/utils.py +++ b/compose/utils.py @@ -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 diff --git a/tests/unit/utils_test.py b/tests/unit/utils_test.py index 84becb975..186b6b14e 100644 --- a/tests/unit/utils_test.py +++ b/tests/unit/utils_test.py @@ -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]