Skip to content

Commit

Permalink
Use SubscriptTypes to describe the subscript types everywhere.
Browse files Browse the repository at this point in the history
Also cleaned up some of the normalize_subscript-related docs.
  • Loading branch information
LTLA committed Nov 14, 2023
1 parent fe03a7b commit 2a59f9b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/biocutils/Factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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``.
Expand Down
25 changes: 13 additions & 12 deletions src/biocutils/NamedList.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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`.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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``.
Expand Down
6 changes: 2 additions & 4 deletions src/biocutils/StringList.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/biocutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/biocutils/normalize_subscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2a59f9b

Please sign in to comment.