mirror of https://github.com/docker/compose.git
utils: Add unique_everseen (from itertools recipies).
Signed-off-by: Antony MECHIN <antony.mechin@docker.com>
This commit is contained in:
parent
bb87a3d040
commit
18c2d08011
|
@ -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
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue