-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc077c3
commit 774afb5
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Utility functions for asfsmd.""" | ||
|
||
from typing import Any, Iterable, List | ||
|
||
|
||
def unique(data: Iterable[Any]) -> List[Any]: | ||
"""Return a list of unique items preserving the input ordering.""" | ||
unique_items = [] | ||
unique_items_set = set() | ||
for item in data: | ||
if item not in unique_items_set: | ||
unique_items.append(item) | ||
unique_items_set.add(item) | ||
return unique_items |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Unit tests for the `asfsmd._utils` module.""" | ||
|
||
import itertools | ||
from asfsmd._utils import unique | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["in_", "out"], | ||
[ | ||
pytest.param(["a", "b", "c"], ["a", "b", "c"], id="unique-list"), | ||
pytest.param(["a", "b", "c", "b"], ["a", "b", "c"], id="list"), | ||
pytest.param((1, 2, 2, 3, 1, 2), [1, 2, 3], id="tuple"), | ||
pytest.param(range(3), [0, 1, 2], id="generator"), | ||
pytest.param( | ||
itertools.chain(range(3, 0, -1), range(3)), | ||
[3, 2, 1, 0], | ||
id="reversed-generator"), | ||
], | ||
) | ||
def test_unique(in_, out): | ||
assert unique(in_) == out |