From c1948f5b1d0e5cf3bf45e601b2e907c0155084bc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 Nov 2023 11:46:13 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGELOG.md | 2 +- setup.py | 10 ++-- src/biocutils/factor.py | 3 +- src/biocutils/intersect.py | 4 +- src/biocutils/normalize_subscript.py | 5 +- src/biocutils/print_truncated.py | 54 +++++++++++++------- src/biocutils/print_wrapped_table.py | 22 +++----- src/biocutils/subset.py | 5 +- src/biocutils/union.py | 4 +- tests/conftest.py | 11 ++-- tests/test_factor.py | 22 ++++---- tests/test_intersect.py | 18 +++---- tests/test_list_type_checks.py | 2 +- tests/test_map_to_index.py | 16 +++--- tests/test_match.py | 2 + tests/test_normalize_subscript.py | 76 ++++++++++++++++++++-------- tests/test_package_utils.py | 3 +- tests/test_print_truncated.py | 54 ++++++++++++++++---- tests/test_print_wrapped_table.py | 32 ++++++++---- tests/test_subset.py | 10 ++-- tests/test_union.py | 9 ++-- 21 files changed, 227 insertions(+), 137 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa4e2f3..506f301 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # Changelog -## Version 0.0.1 +## Version 0.0.1 - First release of the package. diff --git a/setup.py b/setup.py index 8a0c567..7f9e0df 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,8 @@ -""" - Setup file for biocutils. - Use setup.cfg to configure your project. +"""Setup file for biocutils. Use setup.cfg to configure your project. - This file was generated with PyScaffold 4.5. - PyScaffold helps you to put up the scaffold of your new Python project. - Learn more under: https://pyscaffold.org/ +This file was generated with PyScaffold 4.5. +PyScaffold helps you to put up the scaffold of your new Python project. +Learn more under: https://pyscaffold.org/ """ from setuptools import setup diff --git a/src/biocutils/factor.py b/src/biocutils/factor.py index 8a570af..3fc7391 100644 --- a/src/biocutils/factor.py +++ b/src/biocutils/factor.py @@ -4,8 +4,7 @@ def factor( x: Sequence, levels: Optional[Sequence] = None, sort_levels: bool = False ) -> Tuple[list, list]: - """ - Convert a sequence of hashable values into a factor. + """Convert a sequence of hashable values into a factor. Args: x (Sequence): A sequence of hashable values. diff --git a/src/biocutils/intersect.py b/src/biocutils/intersect.py index 837f6f0..aeb99e6 100644 --- a/src/biocutils/intersect.py +++ b/src/biocutils/intersect.py @@ -4,8 +4,8 @@ def intersect(*x: Sequence, duplicate_method: DUPLICATE_METHOD = "first") -> list: - """Identify the intersection of values in multiple sequences, while - preserving the order of values in the first sequence. + """Identify the intersection of values in multiple sequences, while preserving the order of values in the first + sequence. Args: x (Sequence): diff --git a/src/biocutils/normalize_subscript.py b/src/biocutils/normalize_subscript.py index 3c7b2c8..f3bb214 100644 --- a/src/biocutils/normalize_subscript.py +++ b/src/biocutils/normalize_subscript.py @@ -25,9 +25,8 @@ def normalize_subscript( names: Optional[Sequence[str]] = None, non_negative_only: bool = True, ) -> Tuple: - """ - Normalize a subscript for ``__getitem__`` or friends into a sequence of - integer indices, for consistent downstream use. + """Normalize a subscript for ``__getitem__`` or friends into a sequence of integer indices, for consistent + downstream use. Args: sub: diff --git a/src/biocutils/print_truncated.py b/src/biocutils/print_truncated.py index cb003b3..777456d 100644 --- a/src/biocutils/print_truncated.py +++ b/src/biocutils/print_truncated.py @@ -2,10 +2,8 @@ def print_truncated(x, truncated_to: int = 3, full_threshold: int = 10) -> str: - """ - Pretty-print an object, replacing the middle elements of lists/dictionaries - with an ellipsis if there are too many. This provides a useful preview of - an object without spewing out all of its contents on the screen. + """Pretty-print an object, replacing the middle elements of lists/dictionaries with an ellipsis if there are too + many. This provides a useful preview of an object without spewing out all of its contents on the screen. Args: x: Object to be printed. @@ -22,18 +20,27 @@ def print_truncated(x, truncated_to: int = 3, full_threshold: int = 10) -> str: String containing the pretty-printed contents. """ if isinstance(x, dict): - return print_truncated_dict(x, truncated_to=truncated_to, full_threshold=full_threshold) + return print_truncated_dict( + x, truncated_to=truncated_to, full_threshold=full_threshold + ) elif isinstance(x, list): - return print_truncated_list(x, truncated_to=truncated_to, full_threshold=full_threshold) + return print_truncated_list( + x, truncated_to=truncated_to, full_threshold=full_threshold + ) else: return repr(x) -def print_truncated_list(x: List, truncated_to: int = 3, full_threshold: int = 10, transform: Optional[Callable] = None, sep: str = ", ", include_brackets: bool = True) -> str: - """ - Pretty-print a list, replacing the middle elements with an ellipsis if - there are too many. This provides a useful preview of an object without - spewing out all of its contents on the screen. +def print_truncated_list( + x: List, + truncated_to: int = 3, + full_threshold: int = 10, + transform: Optional[Callable] = None, + sep: str = ", ", + include_brackets: bool = True, +) -> str: + """Pretty-print a list, replacing the middle elements with an ellipsis if there are too many. This provides a useful + preview of an object without spewing out all of its contents on the screen. Args: x: List to be printed. @@ -58,11 +65,13 @@ def print_truncated_list(x: List, truncated_to: int = 3, full_threshold: int = 1 Whether to include the start/end brackets. Returns: - String containing the pretty-printed truncated list. + String containing the pretty-printed truncated list. """ collected = [] if transform is None: - transform = lambda y : print_truncated(y, truncated_to=truncated_to, full_threshold=full_threshold) + transform = lambda y: print_truncated( + y, truncated_to=truncated_to, full_threshold=full_threshold + ) if len(x) > full_threshold and len(x) > truncated_to * 2: for i in range(truncated_to): @@ -80,11 +89,16 @@ def print_truncated_list(x: List, truncated_to: int = 3, full_threshold: int = 1 return output -def print_truncated_dict(x: Dict, truncated_to: int = 3, full_threshold: int = 10, transform: Optional[Callable] = None, sep: str = ", ", include_brackets: bool = True) -> str: - """ - Pretty-print a dictionary, replacing the middle elements with an ellipsis - if there are too many. This provides a useful preview of an object without - spewing out all of its contents on the screen. +def print_truncated_dict( + x: Dict, + truncated_to: int = 3, + full_threshold: int = 10, + transform: Optional[Callable] = None, + sep: str = ", ", + include_brackets: bool = True, +) -> str: + """Pretty-print a dictionary, replacing the middle elements with an ellipsis if there are too many. This provides a + useful preview of an object without spewing out all of its contents on the screen. Args: x: Dictionary to be printed. @@ -113,7 +127,9 @@ def print_truncated_dict(x: Dict, truncated_to: int = 3, full_threshold: int = 1 """ collected = [] if transform is None: - transform = lambda y : print_truncated(y, truncated_to=truncated_to, full_threshold=full_threshold) + transform = lambda y: print_truncated( + y, truncated_to=truncated_to, full_threshold=full_threshold + ) all_keys = x.keys() if len(x) > full_threshold and len(x) > truncated_to * 2: diff --git a/src/biocutils/print_wrapped_table.py b/src/biocutils/print_wrapped_table.py index 00df847..6ad851e 100644 --- a/src/biocutils/print_wrapped_table.py +++ b/src/biocutils/print_wrapped_table.py @@ -17,11 +17,9 @@ def print_wrapped_table( sep: str = " ", window: Optional[int] = None, ) -> str: - """ - Pretty-print a table with aligned and wrapped columns. All column contents - are padded so that they are right-justified. Wrapping is performed whenever - a new column would exceed the window width, in which case the entire column - (and all subsequent columns) are printed below the previous columns. + """Pretty-print a table with aligned and wrapped columns. All column contents are padded so that they are right- + justified. Wrapping is performed whenever a new column would exceed the window width, in which case the entire + column (and all subsequent columns) are printed below the previous columns. Args: columns: @@ -105,9 +103,8 @@ def reinitialize(): def create_floating_names( names: Optional[List[str]], indices: Sequence[int] ) -> List[str]: - """ - Create the floating names to use in :py:meth:`~print_wrapped_table`. If no - names are present, positional indices are used instead. + """Create the floating names to use in :py:meth:`~print_wrapped_table`. If no names are present, positional indices + are used instead. Args: names: @@ -126,8 +123,7 @@ def create_floating_names( def truncate_strings(values: List[str], width: int = 40) -> List[str]: - """ - Truncate long strings for printing in :py:meth:`~print_wrapped_table`. + """Truncate long strings for printing in :py:meth:`~print_wrapped_table`. Args: values: @@ -147,10 +143,8 @@ def truncate_strings(values: List[str], width: int = 40) -> List[str]: def print_type(x) -> str: - """ - Print the type of an object, with some special behavior for certain classes - (e.g., to add the data type of NumPy arrays). This is intended for display - at the top of the columns of :py:meth:`~print_wrapped_table`. + """Print the type of an object, with some special behavior for certain classes (e.g., to add the data type of NumPy + arrays). This is intended for display at the top of the columns of :py:meth:`~print_wrapped_table`. Args: x: Some object. diff --git a/src/biocutils/subset.py b/src/biocutils/subset.py index ba9ed31..cb8dede 100644 --- a/src/biocutils/subset.py +++ b/src/biocutils/subset.py @@ -2,9 +2,8 @@ def subset(x: Any, indices: Union[Sequence[int], slice]) -> Any: - """Subset ``x`` by ``indices`` to obtain a new object with the desired - subset of elements. This attempts to use ``x``'s ``__getitem__`` method, if - available; otherwise it falls back to iteration over the indices. + """Subset ``x`` by ``indices`` to obtain a new object with the desired subset of elements. This attempts to use + ``x``'s ``__getitem__`` method, if available; otherwise it falls back to iteration over the indices. If ``x`` has a ``shape`` method that returns a tuple (a la NumPy arrays), subsetting is only attempted on the first dimension via the ``__getitem__`` diff --git a/src/biocutils/union.py b/src/biocutils/union.py index 3441430..5687582 100644 --- a/src/biocutils/union.py +++ b/src/biocutils/union.py @@ -4,8 +4,8 @@ def union(*x: Sequence, duplicate_method: DUPLICATE_METHOD = "first") -> list: - """Identify the union of values in multiple sequences, while preserving the - order of the first (or last) occurence of each value. + """Identify the union of values in multiple sequences, while preserving the order of the first (or last) occurence + of each value. Args: x (Sequence): diff --git a/tests/conftest.py b/tests/conftest.py index 8cfd151..1407ecd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,10 +1,9 @@ -""" - Dummy conftest.py for biocutils. +"""Dummy conftest.py for biocutils. - If you don't know what this is for, just leave it empty. - Read more about conftest.py under: - - https://docs.pytest.org/en/stable/fixture.html - - https://docs.pytest.org/en/stable/writing_plugins.html +If you don't know what this is for, just leave it empty. +Read more about conftest.py under: +- https://docs.pytest.org/en/stable/fixture.html +- https://docs.pytest.org/en/stable/writing_plugins.html """ # import pytest diff --git a/tests/test_factor.py b/tests/test_factor.py index 9a2f72d..44e6337 100644 --- a/tests/test_factor.py +++ b/tests/test_factor.py @@ -18,30 +18,32 @@ def test_factor_simple(): def test_factor_levels(): - revlev = [5,4,3,2,1] + revlev = [5, 4, 3, 2, 1] lev, ind = factor([1, 3, 5, 5, 3, 1], levels=revlev) assert lev == revlev - assert ind == [4,2,0,0,2,4] + assert ind == [4, 2, 0, 0, 2, 4] # Preserves duplicates. - duplicated = [5,4,5,4,3,4,2,3,1,1,2] + duplicated = [5, 4, 5, 4, 3, 4, 2, 3, 1, 1, 2] lev, ind = factor([1, 3, 5, 5, 3, 1], levels=duplicated) assert lev == duplicated - assert ind == [8,4,0,0,4,8] + assert ind == [8, 4, 0, 0, 4, 8] # Ignores None. - noney = [None,1,2,3,4,5,None] + noney = [None, 1, 2, 3, 4, 5, None] lev, ind = factor([1, 3, 5, 5, 3, 1], levels=noney) assert lev == noney - assert ind == [1,3,5,5,3,1] + assert ind == [1, 3, 5, 5, 3, 1] def test_factor_sorted(): - lev, ind = factor(["C", "D", "A", "B", "C", "A"], sort_levels = True) + lev, ind = factor(["C", "D", "A", "B", "C", "A"], sort_levels=True) assert lev == ["A", "B", "C", "D"] - assert ind == [2,3,0,1,2,0] + assert ind == [2, 3, 0, 1, 2, 0] # Not affected if you supply the levels directly. - lev, ind = factor(["C", "D", "A", "B", "C", "A"], levels = ["D", "C", "B", "A"], sort_levels = True) + lev, ind = factor( + ["C", "D", "A", "B", "C", "A"], levels=["D", "C", "B", "A"], sort_levels=True + ) assert lev == ["D", "C", "B", "A"] - assert ind == [1,0,3,2,1,3] + assert ind == [1, 0, 3, 2, 1, 3] diff --git a/tests/test_intersect.py b/tests/test_intersect.py index def45a8..698f036 100644 --- a/tests/test_intersect.py +++ b/tests/test_intersect.py @@ -17,32 +17,26 @@ def test_intersect_simple(): def test_intersect_duplicates(): # Doesn't report B, D, or F, despite the fact they have multiple counts. - out = intersect( - ["B", "B", "C", "A", "D", "D", "E"], ["A", "A", "C", "E", "F", "F"] - ) + out = intersect(["B", "B", "C", "A", "D", "D", "E"], ["A", "A", "C", "E", "F", "F"]) assert out == ["C", "A", "E"] # Doesn't report A multiple times. - out = intersect( - ["C", "A", "D", "A", "E", "A"], ["A", "C", "E", "F"] - ) + out = intersect(["C", "A", "D", "A", "E", "A"], ["A", "C", "E", "F"]) assert out == ["C", "A", "E"] # Switches the order of A being reported. out = intersect( - ["C", "A", "D", "A", "E", "A"], - ["A", "C", "E", "F"], - duplicate_method = "last" + ["C", "A", "D", "A", "E", "A"], ["A", "C", "E", "F"], duplicate_method="last" ) assert out == ["C", "E", "A"] # Handles the single case correctly. single = ["A", "B", "A", "C", "D", "E", "D", "C"] out = intersect(single) - assert out == [ "A", "B", "C", "D", "E" ] + assert out == ["A", "B", "C", "D", "E"] - out = intersect(single, duplicate_method = "last") - assert out == [ "B", "A", "E", "D", "C" ] + out = intersect(single, duplicate_method="last") + assert out == ["B", "A", "E", "D", "C"] def test_intersect_none(): diff --git a/tests/test_list_type_checks.py b/tests/test_list_type_checks.py index 78a8f6a..6496ca9 100644 --- a/tests/test_list_type_checks.py +++ b/tests/test_list_type_checks.py @@ -16,7 +16,7 @@ def test_simple_list(): xt = (1, 2, None) assert not is_list_of_type(xt, int) - assert is_list_of_type(xt, int, ignore_none = True) + assert is_list_of_type(xt, int, ignore_none=True) def test_should_fail(): diff --git a/tests/test_map_to_index.py b/tests/test_map_to_index.py index 7c307be..9918c78 100644 --- a/tests/test_map_to_index.py +++ b/tests/test_map_to_index.py @@ -3,18 +3,22 @@ def test_map_to_index_simple(): mapping = map_to_index(["A", "B", "C", "D"]) - assert mapping == { "A": 0, "B": 1, "C": 2, "D": 3 } + assert mapping == {"A": 0, "B": 1, "C": 2, "D": 3} + def test_map_to_index_duplicates(): duplicated = ["A", "B", "C", "D", "A", "B", "C", "D"] mapping = map_to_index(duplicated) - assert mapping == { "A": 0, "B": 1, "C": 2, "D": 3 } + assert mapping == {"A": 0, "B": 1, "C": 2, "D": 3} + + mapping = map_to_index( + ["A", "B", "C", "D", "A", "B", "C", "D"], duplicate_method="last" + ) + assert mapping == {"A": 4, "B": 5, "C": 6, "D": 7} - mapping = map_to_index(["A", "B", "C", "D", "A", "B", "C", "D"], duplicate_method="last") - assert mapping == { "A": 4, "B": 5, "C": 6, "D": 7 } def test_map_to_index_none(): - noney = [ None, "A", None, "B", None, "C", None, "D", None ] + noney = [None, "A", None, "B", None, "C", None, "D", None] mapping = map_to_index(noney) - assert mapping == { "A": 1, "B": 3, "C": 5, "D": 7 } + assert mapping == {"A": 1, "B": 3, "C": 5, "D": 7} diff --git a/tests/test_match.py b/tests/test_match.py index 2c1dc2e..23af59b 100644 --- a/tests/test_match.py +++ b/tests/test_match.py @@ -11,6 +11,7 @@ def test_match_simple(): mm2 = match(x, map_to_index(levels)) assert mm == mm2 + def test_match_duplicates(): x = [5, 1, 2, 3, 5, 6, 7, 7, 2, 1] mm = match(x, [1, 2, 3, 3, 5, 6, 1, 7, 6]) @@ -19,6 +20,7 @@ def test_match_duplicates(): mm = match(x, [1, 2, 3, 3, 5, 6, 1, 7, 6], duplicate_method="last") assert mm == [4, 6, 1, 3, 4, 8, 7, 7, 1, 6] + def test_match_none(): mm = match(["A", None, "B", "D", None, "A", "C", None, "B"], ["D", "C", "B", "A"]) assert list(mm) == [3, None, 2, 0, None, 3, 1, None, 2] diff --git a/tests/test_normalize_subscript.py b/tests/test_normalize_subscript.py index 3516ae7..77ea68e 100644 --- a/tests/test_normalize_subscript.py +++ b/tests/test_normalize_subscript.py @@ -8,8 +8,11 @@ def test_normalize_subscript_scalars(): assert normalize_subscript(-1, 100) == ([99], True) assert normalize_subscript(True, 100) == ([0], True) assert normalize_subscript(False, 100) == ([], False) - assert normalize_subscript("C", 5, ["A", "B", "C", "D", "E" ]) == ([2], True) - assert normalize_subscript("B", 5, ["A", "B", "C", "B", "E" ]) == ([1], True) # takes first occurence. + assert normalize_subscript("C", 5, ["A", "B", "C", "D", "E"]) == ([2], True) + assert normalize_subscript("B", 5, ["A", "B", "C", "B", "E"]) == ( + [1], + True, + ) # takes first occurence. with pytest.raises(IndexError) as ex: normalize_subscript(100, 10) @@ -35,8 +38,14 @@ def test_normalize_subscript_slice(): def test_normalize_subscript_range(): assert normalize_subscript(range(5, 2), 100) == ([], False) assert normalize_subscript(range(10, 40), 100) == (range(10, 40), False) - assert normalize_subscript(range(-10, 40), 100) == (list(range(90, 100)) + list(range(40)), False) - assert normalize_subscript(range(50, -10, -1), 100) == (list(range(50, -1, -1)) + list(range(99, 90, -1)), False) + assert normalize_subscript(range(-10, 40), 100) == ( + list(range(90, 100)) + list(range(40)), + False, + ) + assert normalize_subscript(range(50, -10, -1), 100) == ( + list(range(50, -1, -1)) + list(range(99, 90, -1)), + False, + ) assert normalize_subscript(range(-10, -50, -1), 100) == (range(90, 50, -1), False) with pytest.raises(IndexError) as ex: @@ -61,52 +70,79 @@ def test_normalize_subscript_range(): def test_normalize_subscript_chaos(): - assert normalize_subscript([0,2,4,6,8], 50) == ([0,2,4,6,8], False) + assert normalize_subscript([0, 2, 4, 6, 8], 50) == ([0, 2, 4, 6, 8], False) with pytest.raises(IndexError) as ex: - normalize_subscript([0,2,50,6,8], 50) + normalize_subscript([0, 2, 50, 6, 8], 50) assert str(ex.value).find("subscript (50)") >= 0 - assert normalize_subscript([0,-1,2,-3,4,-5,6,-7,8], 50) == ([0,49,2,47,4,45,6,43,8], False) + assert normalize_subscript([0, -1, 2, -3, 4, -5, 6, -7, 8], 50) == ( + [0, 49, 2, 47, 4, 45, 6, 43, 8], + False, + ) with pytest.raises(IndexError) as ex: - normalize_subscript([0,2,-51,6,8], 50) + normalize_subscript([0, 2, -51, 6, 8], 50) assert str(ex.value).find("subscript (-51)") >= 0 - assert normalize_subscript([False,10,True,20,False,30,True], 50) == ([10,2,20,30,6], False) + assert normalize_subscript([False, 10, True, 20, False, 30, True], 50) == ( + [10, 2, 20, 30, 6], + False, + ) names = ["A", "B", "C", "D", "E", "F"] - assert normalize_subscript(["B",1,"D",2,"F",3,"A"], 6, names) == ([1,1,3,2,5,3,0], False) - assert normalize_subscript(["B",1,"A",2,"B",3,"A"], 6, ["A", "B", "A", "B", "A", "B"]) == ([1,1,0,2,1,3,0], False) # Takes the first occurence. + assert normalize_subscript(["B", 1, "D", 2, "F", 3, "A"], 6, names) == ( + [1, 1, 3, 2, 5, 3, 0], + False, + ) + assert normalize_subscript( + ["B", 1, "A", 2, "B", 3, "A"], 6, ["A", "B", "A", "B", "A", "B"] + ) == ( + [1, 1, 0, 2, 1, 3, 0], + False, + ) # Takes the first occurence. with pytest.raises(KeyError) as ex: - normalize_subscript(["B",1,"D",2,"G",3,"A"], 6, names) + normalize_subscript(["B", 1, "D", 2, "G", 3, "A"], 6, names) with pytest.raises(IndexError) as ex: - normalize_subscript(["B",1,"D",2,"F",3,"A"], 6) + normalize_subscript(["B", 1, "D", 2, "F", 3, "A"], 6) assert str(ex.value).find("vector-like object with no names") >= 0 def test_normalize_subscript_numpy(): out, x = normalize_subscript(numpy.array([1, 3, 5]), 6) - assert (out == numpy.array([1,3,5])).all() + assert (out == numpy.array([1, 3, 5])).all() out, x = normalize_subscript(numpy.array([-1, -3, -5]), 6) - assert (out == numpy.array([5,3,1])).all() + assert (out == numpy.array([5, 3, 1])).all() assert normalize_subscript(numpy.int64(5), 6) == ([5], True) assert normalize_subscript(numpy.bool_(True), 6) == ([0], True) # Now the trickiest part - are booleans converted correctly? - assert normalize_subscript(numpy.array([True, False, True, False, True]), 5) == ([0, 2, 4], False) + assert normalize_subscript(numpy.array([True, False, True, False, True]), 5) == ( + [0, 2, 4], + False, + ) def test_normalize_subscript_allow_negative(): assert normalize_subscript(-50, 100, non_negative_only=False) == ([-50], True) - assert normalize_subscript(range(50, -10, -1), 100, non_negative_only=False) == (range(50, -10, -1), False) - assert normalize_subscript(range(-10, -50, -1), 100, non_negative_only=False) == (range(-10, -50, -1), False) - assert normalize_subscript([0,-1,2,-3,4,-5,6,-7,8], 50, non_negative_only=False) == ([0,-1,2,-3,4,-5,6,-7,8], False) + assert normalize_subscript(range(50, -10, -1), 100, non_negative_only=False) == ( + range(50, -10, -1), + False, + ) + assert normalize_subscript(range(-10, -50, -1), 100, non_negative_only=False) == ( + range(-10, -50, -1), + False, + ) + assert normalize_subscript( + [0, -1, 2, -3, 4, -5, 6, -7, 8], 50, non_negative_only=False + ) == ([0, -1, 2, -3, 4, -5, 6, -7, 8], False) with pytest.raises(IndexError) as ex: - normalize_subscript([0,-1,2,-3,4,-51,6,-7,8], 50, non_negative_only=False) + normalize_subscript( + [0, -1, 2, -3, 4, -51, 6, -7, 8], 50, non_negative_only=False + ) assert str(ex.value).find("subscript (-51) out of range") >= 0 diff --git a/tests/test_package_utils.py b/tests/test_package_utils.py index 5373269..46a571c 100644 --- a/tests/test_package_utils.py +++ b/tests/test_package_utils.py @@ -16,7 +16,8 @@ def test_for_scipy(): assert pkg is False + def test_for_numpy(): pkg = is_package_installed("numpy") - assert pkg is True \ No newline at end of file + assert pkg is True diff --git a/tests/test_print_truncated.py b/tests/test_print_truncated.py index df49439..02b1629 100644 --- a/tests/test_print_truncated.py +++ b/tests/test_print_truncated.py @@ -5,19 +5,55 @@ def test_print_truncated_list(): assert print_truncated_list(range(6)) == repr(list(range(6))) assert print_truncated_list(range(10)) == repr(list(range(10))) assert print_truncated_list(range(200)) == "[0, 1, 2, ..., 197, 198, 199]" - assert print_truncated_list(["A", "B", "C", "D", "E", "F"], transform=lambda x : repr("foo_" + x)) == "['foo_A', 'foo_B', 'foo_C', 'foo_D', 'foo_E', 'foo_F']" - assert print_truncated_list(["A", "B", "C", "D", "E", "F"], truncated_to=2, full_threshold=5, transform=lambda x : repr("foo_" + x)) == "['foo_A', 'foo_B', ..., 'foo_E', 'foo_F']" - assert print_truncated_list(range(200), sep=" ", include_brackets=False) == "0 1 2 ... 197 198 199" + assert ( + print_truncated_list( + ["A", "B", "C", "D", "E", "F"], transform=lambda x: repr("foo_" + x) + ) + == "['foo_A', 'foo_B', 'foo_C', 'foo_D', 'foo_E', 'foo_F']" + ) + assert ( + print_truncated_list( + ["A", "B", "C", "D", "E", "F"], + truncated_to=2, + full_threshold=5, + transform=lambda x: repr("foo_" + x), + ) + == "['foo_A', 'foo_B', ..., 'foo_E', 'foo_F']" + ) + assert ( + print_truncated_list(range(200), sep=" ", include_brackets=False) + == "0 1 2 ... 197 198 199" + ) def test_print_truncated_dict(): - assert print_truncated_dict({"A":"B"}) == "{'A': 'B'}" - assert print_truncated_dict({"A":"B", "C": [1,2,3,4,5,6,7,8,9,10,11]}) == "{'A': 'B', 'C': [1, 2, 3, ..., 9, 10, 11]}" - assert print_truncated_dict({"A":-1, "B": 0, "C": 1, "D": 2, "E": True, "F": False }, truncated_to = 2, full_threshold = 5) == "{'A': -1, 'B': 0, ..., 'E': True, 'F': False}" - assert print_truncated_dict({"A":-1, "B": 0, "C": 1, "D": 2, "E": True, "F": False }, sep=" ", include_brackets=False) == "'A': -1 'B': 0 'C': 1 'D': 2 'E': True 'F': False" + assert print_truncated_dict({"A": "B"}) == "{'A': 'B'}" + assert ( + print_truncated_dict({"A": "B", "C": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]}) + == "{'A': 'B', 'C': [1, 2, 3, ..., 9, 10, 11]}" + ) + assert ( + print_truncated_dict( + {"A": -1, "B": 0, "C": 1, "D": 2, "E": True, "F": False}, + truncated_to=2, + full_threshold=5, + ) + == "{'A': -1, 'B': 0, ..., 'E': True, 'F': False}" + ) + assert ( + print_truncated_dict( + {"A": -1, "B": 0, "C": 1, "D": 2, "E": True, "F": False}, + sep=" ", + include_brackets=False, + ) + == "'A': -1 'B': 0 'C': 1 'D': 2 'E': True 'F': False" + ) def test_print_truncated(): - internal = {"A":-1, "B": 0, "C": 1, "D": 2, "E": True, "F": False } + internal = {"A": -1, "B": 0, "C": 1, "D": 2, "E": True, "F": False} expected = "{'A': -1, 'B': 0, ..., 'E': True, 'F': False}" - assert print_truncated([internal] * 10, truncated_to = 2, full_threshold = 5) == "[" + (expected + ", " ) * 2 + "..." + (", " + expected) * 2 + "]" + assert ( + print_truncated([internal] * 10, truncated_to=2, full_threshold=5) + == "[" + (expected + ", ") * 2 + "..." + (", " + expected) * 2 + "]" + ) diff --git a/tests/test_print_wrapped_table.py b/tests/test_print_wrapped_table.py index 5df943d..f2d9906 100644 --- a/tests/test_print_wrapped_table.py +++ b/tests/test_print_wrapped_table.py @@ -1,4 +1,9 @@ -from biocutils import print_wrapped_table, create_floating_names, truncate_strings, print_type +from biocutils import ( + print_wrapped_table, + create_floating_names, + truncate_strings, + print_type, +) import numpy as np @@ -9,7 +14,11 @@ def test_print_wrapped_table(): ["asyudgausydga", "A", "B", "C", "D"], ] print(print_wrapped_table(contents)) - print(print_wrapped_table(contents, floating_names=["", "aarg", "boo", "ffoo", "stuff"])) + print( + print_wrapped_table( + contents, floating_names=["", "aarg", "boo", "ffoo", "stuff"] + ) + ) print(print_wrapped_table(contents, window=10)) print( print_wrapped_table( @@ -19,16 +28,21 @@ def test_print_wrapped_table(): def test_create_floating_names(): - assert create_floating_names(None, [1,2,3,4]) == [ "[1]", "[2]", "[3]", "[4]" ] - assert create_floating_names(["A", "B", "C", "D", "E", "F"], [1,2,3,4]) == [ "B", "C", "D", "E" ] + assert create_floating_names(None, [1, 2, 3, 4]) == ["[1]", "[2]", "[3]", "[4]"] + assert create_floating_names(["A", "B", "C", "D", "E", "F"], [1, 2, 3, 4]) == [ + "B", + "C", + "D", + "E", + ] def test_truncate_strings(): - ref = ["A"*10, "B"*20, "C"*30] - assert truncate_strings(ref, width=25) == [ "A"*10, "B"*20, "C"*22 + "..." ] + ref = ["A" * 10, "B" * 20, "C" * 30] + assert truncate_strings(ref, width=25) == ["A" * 10, "B" * 20, "C" * 22 + "..."] def test_print_type(): - assert print_type(np.array([1,2,3])) == "ndarray[int64]" - assert print_type(np.array([1,2.5,3.3])) == "ndarray[float64]" - assert print_type([1,2,3]) == "list" + assert print_type(np.array([1, 2, 3])) == "ndarray[int64]" + assert print_type(np.array([1, 2.5, 3.3])) == "ndarray[float64]" + assert print_type([1, 2, 3]) == "list" diff --git a/tests/test_subset.py b/tests/test_subset.py index 869b7c8..afb6efc 100644 --- a/tests/test_subset.py +++ b/tests/test_subset.py @@ -3,14 +3,14 @@ def test_subset_list(): - x = [1,2,3,4,5] - assert subset(x, [0,2,4]) == [1,3,5] + x = [1, 2, 3, 4, 5] + assert subset(x, [0, 2, 4]) == [1, 3, 5] - x = [1,2,3,4,5] + x = [1, 2, 3, 4, 5] assert subset(x, slice(None)) == x - x = [1,2,3,4,5] - assert subset(x, range(4, -1, -1)) == [5,4,3,2,1] + x = [1, 2, 3, 4, 5] + assert subset(x, range(4, -1, -1)) == [5, 4, 3, 2, 1] def test_subset_numpy(): diff --git a/tests/test_union.py b/tests/test_union.py index 3de8e31..3fd5078 100644 --- a/tests/test_union.py +++ b/tests/test_union.py @@ -7,7 +7,7 @@ def test_union_simple(): y = ["B", "C", "A", "D", "E"] assert union(y) == y - out = union(y, [ "A", "C", "E", "F" ]) + out = union(y, ["A", "C", "E", "F"]) assert out == ["B", "C", "A", "D", "E", "F"] out = union(["B", "C", "A", "D", "E"], ["A", "C", "K", "E"], ["G", "K"]) @@ -21,13 +21,10 @@ def test_union_duplicates(): out = union(y1, y2) assert out == ["B", "C", "A", "D", "E", "F"] - out = union(y1, y2, duplicate_method = "last") + out = union(y1, y2, duplicate_method="last") assert out == ["B", "D", "A", "C", "E", "F"] def test_union_none(): - out = union( - ["B", None, "C", "A", None, "D", "E"], - ["A", None, "C", "E", None, "F"] - ) + out = union(["B", None, "C", "A", None, "D", "E"], ["A", None, "C", "E", None, "F"]) assert out == ["B", "C", "A", "D", "E", "F"]