From 2a59f9b6c5487be5a548c132b6a7a4bb0a7f35b3 Mon Sep 17 00:00:00 2001 From: LTLA Date: Tue, 14 Nov 2023 12:12:55 -0800 Subject: [PATCH] Use SubscriptTypes to describe the subscript types everywhere. Also cleaned up some of the normalize_subscript-related docs. --- src/biocutils/Factor.py | 6 +++--- src/biocutils/NamedList.py | 25 +++++++++++++------------ src/biocutils/StringList.py | 6 ++---- src/biocutils/__init__.py | 2 +- src/biocutils/normalize_subscript.py | 7 ++++--- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/biocutils/Factor.py b/src/biocutils/Factor.py index d6431cc..656d3f3 100644 --- a/src/biocutils/Factor.py +++ b/src/biocutils/Factor.py @@ -6,7 +6,7 @@ from .StringList import StringList from .match import match from .factorize import factorize -from .normalize_subscript import normalize_subscript +from .normalize_subscript import normalize_subscript, SubscriptTypes from .is_missing_scalar import is_missing_scalar from .print_truncated import print_truncated_list from .combine_sequences import combine_sequences @@ -144,7 +144,7 @@ def __str__(self) -> str: message += "ordered: " + str(self._ordered) return message - def __getitem__(self, sub: Union[int, bool, Sequence]) -> Union[str, "Factor"]: + def __getitem__(self, sub: SubscriptTypes) -> Union[str, "Factor"]: """Subset the ``Factor`` to the specified subset of indices. Args: @@ -170,7 +170,7 @@ def __getitem__(self, sub: Union[int, bool, Sequence]) -> Union[str, "Factor"]: return None return type(self)(self._codes[sub], self._levels, self._ordered, validate=False) - def replace(self, sub: Sequence, value: Union[str, "Factor"], in_place: bool = False): + def replace(self, sub: SubscriptTypes, value: Union[str, "Factor"], in_place: bool = False): """ Replace items in the ``Factor`` list. The ``subs`` elements in the current object are replaced with the corresponding values in ``value``. diff --git a/src/biocutils/NamedList.py b/src/biocutils/NamedList.py index e30d069..2c79537 100644 --- a/src/biocutils/NamedList.py +++ b/src/biocutils/NamedList.py @@ -2,7 +2,7 @@ from copy import deepcopy from .Names import Names -from .normalize_subscript import normalize_subscript +from .normalize_subscript import normalize_subscript, SubscriptTypes from .subset_sequence import subset_sequence from .combine_sequences import combine_sequences from .assign_sequence import assign_sequence @@ -183,15 +183,15 @@ def get_value(self, index: Union[str, int]) -> Any: index = _name_to_position(self._names, index) return self._data[index] - def get_slice(self, index: Union[str, int, bool, Sequence]) -> "NamedList": + def get_slice(self, index: SubscriptTypes) -> "NamedList": """ Args: index: Subset of elements to obtain, see - :py:func:`~normalize_subscript.normalize_subscript` for - details. Strings are matched to names in the current object, - using the first occurrence if duplicate names are present. - Scalars are treated as length-1 vectors. + :py:func:`~biocutils.normalize_subscript.normalize_subscript` + for details. Strings are matched to names in the current + object, using the first occurrence if duplicate names are + present. Scalars are treated as length-1 vectors. Returns: A ``NamedList`` is returned containing the specified subset. @@ -203,7 +203,7 @@ def get_slice(self, index: Union[str, int, bool, Sequence]) -> "NamedList": outnames = subset_sequence(self._names, index) return type(self)(outdata, outnames, _validate=False) - def __getitem__(self, index: Union[str, int, bool, Sequence]) -> Union["NamedList", Any]: + def __getitem__(self, index: SubscriptTypes) -> Union["NamedList", Any]: """ If ``index`` is a scalar, this is an alias for :py:attr:`~get_item`. @@ -262,14 +262,15 @@ def set_value(self, index: Union[str, int], value: Any, in_place: bool = False) return output - def set_slice(self, index: Union[int, str, slice], value: Sequence, in_place: bool = False) -> "NamedList": + def set_slice(self, index: SubscriptTypes, value: Sequence, in_place: bool = False) -> "NamedList": """ Args: index: Subset of elements to replace, see - :py:func:`~normalize_subscript.normalize_subscript` for - details. Strings are matched to names in the current object, - using the first occurrence if duplicate names are present. + :py:func:`~biocutils.normalize_subscript.normalize_subscript` + for details. Strings are matched to names in the current + object, using the first occurrence if duplicate names are + present. value: If ``index`` is a sequence, a sequence of the same length @@ -303,7 +304,7 @@ def set_slice(self, index: Union[int, str, slice], value: Sequence, in_place: bo output._data[j] = value[i] return output - def __setitem__(self, index: Union[int, str, slice], value: Any): + def __setitem__(self, index: SubscriptTypes, value: Any): """ If ``index`` is a scalar, this is an alias for :py:attr:`~set_item` with ``in_place = True``. diff --git a/src/biocutils/StringList.py b/src/biocutils/StringList.py index 2d3098e..6cac4ad 100644 --- a/src/biocutils/StringList.py +++ b/src/biocutils/StringList.py @@ -3,9 +3,7 @@ from .Names import Names from .NamedList import NamedList -from .subset_sequence import subset_sequence -from .combine_sequences import combine_sequences -from .assign_sequence import assign_sequence +from .normalize_subscript import SubscriptTypes def _coerce_to_str(x: Any) -> bool: @@ -57,7 +55,7 @@ def set_value(self, index: Union[int, str], value: Any, in_place: bool = False) """Calls :py:meth:`~NamedList.NamedList.set_value` after coercing ``value`` to a string.""" return super().set_value(index, _coerce_to_str(value), in_place=in_place) - def set_slice(self, index: Union[int, str, slice], value: Sequence, in_place: bool = False) -> "StringList": + def set_slice(self, index: SubscriptTypes, value: Sequence, in_place: bool = False) -> "StringList": """Calls :py:meth:`~NamedList.NamedList.set_slice` after coercing ``value`` to strings.""" return super().set_slice(index, _SubscriptCoercer(value), in_place=in_place) diff --git a/src/biocutils/__init__.py b/src/biocutils/__init__.py index 9eaf7ad..9801f3d 100644 --- a/src/biocutils/__init__.py +++ b/src/biocutils/__init__.py @@ -26,7 +26,7 @@ from .is_missing_scalar import is_missing_scalar from .map_to_index import map_to_index from .match import match -from .normalize_subscript import normalize_subscript +from .normalize_subscript import normalize_subscript, SubscriptTypes from .print_truncated import print_truncated, print_truncated_dict, print_truncated_list from .print_wrapped_table import create_floating_names, print_type, print_wrapped_table, truncate_strings from .union import union diff --git a/src/biocutils/normalize_subscript.py b/src/biocutils/normalize_subscript.py index 66e7b8a..ff31073 100644 --- a/src/biocutils/normalize_subscript.py +++ b/src/biocutils/normalize_subscript.py @@ -21,14 +21,15 @@ 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: The subscript. This can be any of the following: - - A slice of elements. + - A slice. - A range containing indices to elements. Negative values are allowed. An error is raised if the indices are out of range. - A single integer specifying the index of an element. A negative