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]