From a9c07dba30b1fc4444ecb74d8476b627dc8ad161 Mon Sep 17 00:00:00 2001 From: LTLA Date: Tue, 7 Nov 2023 08:03:03 -0800 Subject: [PATCH] Officially rely on numpy as a dependency. This is unfortunately necessary as the base language doesn't support typed arrays, so we need to include it to just become equivalent to R. --- setup.cfg | 2 +- src/biocutils/normalize_subscript.py | 42 +++++++--------------------- src/biocutils/print_wrapped_table.py | 11 ++------ 3 files changed, 14 insertions(+), 41 deletions(-) diff --git a/setup.cfg b/setup.cfg index fe0ece4..7f77f53 100644 --- a/setup.cfg +++ b/setup.cfg @@ -49,6 +49,7 @@ python_requires = >=3.8 # For more information, check out https://semver.org/. install_requires = importlib-metadata; python_version<"3.8" + numpy [options.packages.find] @@ -66,7 +67,6 @@ testing = setuptools pytest pytest-cov - numpy [options.entry_points] # Add here console scripts like: diff --git a/src/biocutils/normalize_subscript.py b/src/biocutils/normalize_subscript.py index f3bb214..b673c06 100644 --- a/src/biocutils/normalize_subscript.py +++ b/src/biocutils/normalize_subscript.py @@ -1,22 +1,13 @@ from typing import Optional, Sequence, Tuple, Union +import numpy def _raise_int(idx: int, length): - raise IndexError( - "subscript (" - + str(idx) - + ") out of range for vector-like object of length " - + str(length) - ) + raise IndexError("subscript (" + str(idx) + ") out of range for vector-like object of length " + str(length)) -has_numpy = False -try: - import numpy - - has_numpy = True -except Exception: - pass +def _is_scalar_bool(sub): + return isinstance(sub, bool) or isinstance(sub, numpy.bool_) def normalize_subscript( @@ -64,15 +55,13 @@ def normalize_subscript( specifying the subscript elements, and (ii) a boolean indicating whether ``sub`` was a scalar. """ - if isinstance(sub, bool) or ( - has_numpy and isinstance(sub, numpy.bool_) - ): # before ints, as bools are ints. + if _is_scalar_bool(sub): # before ints, as bools are ints. if sub: return [0], True else: return [], False - if isinstance(sub, int) or (has_numpy and isinstance(sub, numpy.generic)): + if isinstance(sub, int) or isinstance(sub, numpy.integer): if sub < -length or sub >= length: _raise_int(sub, length) if sub < 0 and non_negative_only: @@ -81,11 +70,7 @@ def normalize_subscript( if isinstance(sub, str): if names is None: - raise IndexError( - "failed to find subscript '" - + sub - + "' for vector-like object with no names" - ) + raise IndexError("failed to find subscript '" + sub + "' for vector-like object with no names") return [names.index(sub)], True if isinstance(sub, slice): @@ -121,12 +106,7 @@ def normalize_subscript( can_return_early = True for x in sub: - if ( - isinstance(x, str) - or isinstance(x, bool) - or (has_numpy and isinstance(x, numpy.bool_)) - or (x < 0 and non_negative_only) - ): + if isinstance(x, str) or _is_scalar_bool(x) or (x < 0 and non_negative_only): can_return_early = False break @@ -144,7 +124,7 @@ def normalize_subscript( has_strings.add(x) string_positions.append(len(output)) output.append(None) - elif isinstance(x, bool) or (has_numpy and isinstance(x, numpy.bool_)): + elif _is_scalar_bool(x): if x: output.append(i) elif x < 0: @@ -158,9 +138,7 @@ def normalize_subscript( if len(has_strings): if names is None: - raise IndexError( - "cannot find string subscripts for vector-like object with no names" - ) + raise IndexError("cannot find string subscripts for vector-like object with no names") mapping = {} for i, y in enumerate(names): diff --git a/src/biocutils/print_wrapped_table.py b/src/biocutils/print_wrapped_table.py index 61cd3e7..bbf5b69 100644 --- a/src/biocutils/print_wrapped_table.py +++ b/src/biocutils/print_wrapped_table.py @@ -1,4 +1,5 @@ from typing import List, Optional, Sequence +import numpy from .subset import subset @@ -153,12 +154,6 @@ def print_type(x) -> str: String containing the class of the object. """ cls = type(x).__name__ - - import sys - - if "numpy" in sys.modules: - numpy = sys.modules["numpy"] - if isinstance(x, numpy.ndarray): - return cls + "[" + x.dtype.name + "]" - + if isinstance(x, numpy.ndarray): + return cls + "[" + x.dtype.name + "]" return cls