Skip to content

Commit a7a8e86

Browse files
committed
missing files
1 parent a3de92a commit a7a8e86

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/textual/_unique.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""A utility function to de-duplicate iterables while preserving order."""
2+
3+
from __future__ import annotations
4+
5+
from itertools import chain
6+
from typing import Iterable, TypeVar
7+
8+
T = TypeVar("T")
9+
10+
11+
def unique_ordered(*widgets: Iterable[T]) -> list[T]:
12+
"""Converts a number of iterables of an object in to a list
13+
where each value appears only once, while preserving order."""
14+
unique_objects = list(dict.fromkeys(chain(*widgets)))
15+
return unique_objects

tests/test_unique.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from textual._unique import unique_ordered
2+
3+
4+
def test_unique_ordered():
5+
"""Test result is both unique and the same order as given."""
6+
assert unique_ordered() == []
7+
assert unique_ordered([1]) == [1]
8+
assert unique_ordered([1, 1, 1]) == [1]
9+
assert unique_ordered([1], [2], [3]) == [1, 2, 3]
10+
assert unique_ordered([1, 2], [2], [3, 1]) == [1, 2, 3]
11+
assert unique_ordered([3, 1], [2], [1]) == [3, 1, 2]
12+
assert unique_ordered([10, 9, 8, 7, 6, 5], [8, 7, 6, 5, 4], [5, 4, 3, 2, 1]) == [
13+
10,
14+
9,
15+
8,
16+
7,
17+
6,
18+
5,
19+
4,
20+
3,
21+
2,
22+
1,
23+
]

0 commit comments

Comments
 (0)