From 64851b26a7ee7d3506a5c42c969c08e90cfe4c6f Mon Sep 17 00:00:00 2001 From: Vladimir Prusakov Date: Sun, 26 Nov 2023 07:46:57 +0200 Subject: [PATCH] 0.1.14 (2023-11-26) ------------------- * [new] vlist.is_in() * [fix] requirements tomli --- CHANGELOG.rst | 6 ++++++ README.rst | 17 ++++++++++++++++- __init__.py | 12 ------------ examples/vlist_.py | 8 +++++++- pyproject.toml | 6 ++++-- tests/test_vlist.py | 14 ++++++++++++++ vhelpers/vlist.py | 20 +++++++++++++++++++- 7 files changed, 66 insertions(+), 17 deletions(-) delete mode 100644 __init__.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7e5cb3f..017800b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,12 @@ CHANGELOG ========= +0.1.14 (2023-11-26) +------------------- +* [new] vlist.is_in() +* [fix] requirements tomli + + 0.1.13 (2023-11-04) ------------------- * [new] vlist.replace() diff --git a/README.rst b/README.rst index 4eba0d6..de21f86 100644 --- a/README.rst +++ b/README.rst @@ -24,7 +24,7 @@ or install the package from github.com release .. code:: bash - pip install https://github.com/vladimirs-git/vhelpers/archive/refs/tags/0.1.13.tar.gz + pip install https://github.com/vladimirs-git/vhelpers/archive/refs/tags/0.1.14.tar.gz or install the package from github.com repository @@ -267,6 +267,21 @@ Return assert vlist.flatten([1, [2, [3]], 4, [5, [6]]]) == [1, 2, 3, 4, 5, 6] +is_in(items1, items2) +--------------------- +Check if any item in items1 is present in items2. + +=========== ====== =================================================================================== +Parameter Type Description +=========== ====== =================================================================================== +items1 *list* A list of items. +items2 *list* A list of items. +=========== ====== =================================================================================== + +Return + *bool* True if any item in items1 is present in items2, False otherwise. + + no_dupl(items) -------------- Remove duplicates from a list of items. diff --git a/__init__.py b/__init__.py deleted file mode 100644 index 66201f9..0000000 --- a/__init__.py +++ /dev/null @@ -1,12 +0,0 @@ -"""vhelpers.""" - -from vhelpers import vdate -from vhelpers import vdict -from vhelpers import vint -from vhelpers import vip -from vhelpers import vlist -from vhelpers import vparam -from vhelpers import vpath -from vhelpers import vre -from vhelpers import vstr -from vhelpers import vyml diff --git a/examples/vlist_.py b/examples/vlist_.py index ca2e2b6..56987a8 100644 --- a/examples/vlist_.py +++ b/examples/vlist_.py @@ -8,11 +8,17 @@ # Convert a multidimensional list to a flattened list. assert vlist.flatten([1, [2, [3]], 4, [5, [6]]]) == [1, 2, 3, 4, 5, 6] +# Check is one of items in the items1 is present in the items2. +assert vlist.is_in(items1=[1, 2], items2=[2, 3]) is True +assert vlist.is_in(items1=[1, 2], items2=[3, 4]) is False + # Remove duplicates from a list of items. assert vlist.no_dupl(items=[1, 2, 1]) == [1, 2] # Replace one item with another. -assert vlist.replace(items=[1, 2, 3], old=2, new=4) == [1, 4, 3] +items = [1, 2, 3] +vlist.replace(items=items, old=2, new=4) +assert items == [1, 4, 3] # Split string by punctuation chars. assert vlist.split(text="1; 2_3-4X5,6", chars="_X", ignore=",") == ["1", "2", "3", "4", "5,6"] diff --git a/pyproject.toml b/pyproject.toml index 67a65cc..3092628 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "vhelpers" -version = "0.1.13" +version = "0.1.14" description = "Often used functions in vladimirs-git projects" authors = ["Vladimir Prusakov "] readme = "README.rst" @@ -15,8 +15,10 @@ classifiers = [ "Programming Language :: Python :: 3.8", "Natural Language :: English", ] + [tool.poetry.dependencies] python = "^3.8" +tomli = "^2.0.1" [tool.poetry.group.test.dependencies] pytest = "^7.4.2" @@ -41,7 +43,7 @@ test = ["pytest"] [tool.poetry.urls] "Bug Tracker" = "https://github.com/vladimirs-git/vhelpers/issues" -"Download URL" = "https://github.com/vladimirs-git/vhelpers/archive/refs/tags/0.1.13.tar.gz" +"Download URL" = "https://github.com/vladimirs-git/vhelpers/archive/refs/tags/0.1.14.tar.gz" [build-system] diff --git a/tests/test_vlist.py b/tests/test_vlist.py index 099a424..1a6b6ca 100644 --- a/tests/test_vlist.py +++ b/tests/test_vlist.py @@ -44,6 +44,20 @@ def test__flatten(items, expected): assert actual == expected +@pytest.mark.parametrize("items1, items2, expected", [ + ([], [], False), + ([], [1], False), + ([1, 2], [], False), + ([1, 2], [3], False), + ([1, 2], [1, 3], True), + ([1, 2], [2, 3], True), +]) +def test__is_in(items1, items2, expected): + """vlist.is_in().""" + actual = vlist.is_in(items1=items1, items2=items2) + assert actual == expected + + @pytest.mark.parametrize("items, expected", [ ([], []), (["a", "b", "a"], ["a", "b"]), diff --git a/vhelpers/vlist.py b/vhelpers/vlist.py index 9bee28b..b6d8221 100644 --- a/vhelpers/vlist.py +++ b/vhelpers/vlist.py @@ -75,6 +75,22 @@ def _flatten(items: Sequence, ignore_types=(str, bytes)) -> Generator: yield item +def is_in(items1: list, items2: list) -> bool: + """Check if any item in items1 is present in items2. + + :param items1: A list of items. + :param items2: A list of items. + :return: True if any item in items1 is present in items2, False otherwise. + :example: + is_in(items1=[1, 2], items2=[2, 3]) is True + is_in(items1=[1, 2], items2=[2, 3]) is True + """ + for items in items1: + if items in items2: + return True + return False + + def no_dupl(items: SeqTy) -> ListTy: """Remove duplicates from a list of items. @@ -98,7 +114,9 @@ def replace(items: list, old: Any, new: Any) -> None: :param new: The item to replace with. :return: None. Update items. :example: - replace(items=[1, 2, 3], old=2, new=4) -> [1, 4, 3] + items = [1, 2, 3] + vlist.replace(items=items, old=2, new=4) + assert items == [1, 4, 3] """ if old in items: idx = items.index(old)