Skip to content

Commit

Permalink
New _utils.py module
Browse files Browse the repository at this point in the history
  • Loading branch information
avalentino committed Nov 18, 2023
1 parent fc077c3 commit 774afb5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions asfsmd/_utils.py
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
23 changes: 23 additions & 0 deletions tests/test__utils.py
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

0 comments on commit 774afb5

Please sign in to comment.