From 134a67f9e55f8ae0cca462f863cdc825170d8a0c Mon Sep 17 00:00:00 2001 From: David Nicholson Date: Thu, 1 Feb 2024 21:04:12 -0500 Subject: [PATCH] Fix type comparisons -> isinstance in some modules --- src/crowsetta/formats/seq/birdsongrec.py | 2 +- src/crowsetta/formats/seq/yarden.py | 4 ++-- src/crowsetta/sequence.py | 12 ++++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/crowsetta/formats/seq/birdsongrec.py b/src/crowsetta/formats/seq/birdsongrec.py index 0f001f2..2da229c 100644 --- a/src/crowsetta/formats/seq/birdsongrec.py +++ b/src/crowsetta/formats/seq/birdsongrec.py @@ -81,7 +81,7 @@ def __init__(self, wav_file: PathLike, position: int, length: int, syl_list: lis raise TypeError(f"length must be an int, not type {type(length)}") if not isinstance(syl_list, list): raise TypeError(f"syl_list must be a list, not type {type(syl_list)}") - if not all([type(syl) == BirdsongRecSyllable for syl in syl_list]): + if not all([isinstance(syl, BirdsongRecSyllable) for syl in syl_list]): raise TypeError("not all elements in syl list are of type BirdsongRecSyllable: " f"{syl_list}") self.wav_file = wav_file self.position = position diff --git a/src/crowsetta/formats/seq/yarden.py b/src/crowsetta/formats/seq/yarden.py index c7ad399..e4d4615 100644 --- a/src/crowsetta/formats/seq/yarden.py +++ b/src/crowsetta/formats/seq/yarden.py @@ -16,11 +16,11 @@ def _cast_to_arr(val): """helper function that casts single elements to 1-d numpy arrays""" - if type(val) == int or type(val) == float: + if isinstance(val, int) or isinstance(val, float): # this happens when there's only one syllable in the file # with only one corresponding label return np.asarray([val]) # so make it a one-element list - elif type(val) == np.ndarray: + elif isinstance(val, np.ndarray): # this should happen whenever there's more than one label return val else: diff --git a/src/crowsetta/sequence.py b/src/crowsetta/sequence.py index a31097d..57bc918 100644 --- a/src/crowsetta/sequence.py +++ b/src/crowsetta/sequence.py @@ -82,9 +82,9 @@ def __init__(self, segments, labels, onsets_s=None, offsets_s=None, onset_sample Numpy array of type char, label for each annotated segment. """ if segments is not None: - if type(segments) == Segment: + if isinstance(segments, Segment): segments = (segments,) - elif type(segments) in (list, tuple): + elif isinstance(segments, (list, tuple)): segments = tuple(segments) else: raise TypeError( @@ -140,7 +140,7 @@ def __hash__(self): self._offset_samples, self._labels, ] - list_for_hash = [tuple(item.tolist()) if type(item) == np.ndarray else item for item in list_for_hash] + list_for_hash = [tuple(item.tolist()) if isinstance(item, np.ndarray) else item for item in list_for_hash] tup_for_hash = tuple(list_for_hash) return hash(tup_for_hash) @@ -181,9 +181,9 @@ def __ge__(self, other): @staticmethod def _convert_labels(labels): - if type(labels) == str: + if isinstance(labels, str): labels = np.asarray(list(labels)) - elif type(labels) == list or type(labels) == tuple: + elif isinstance(labels, list) or isinstance(labels, tuple): try: labels = [str(label) for label in labels] except ValueError: @@ -194,7 +194,7 @@ def _convert_labels(labels): @staticmethod def _validate_segments_type(segments): """Validate that all items in list of segments are Segment""" - if not all([type(seg) == Segment for seg in segments]): + if not all([isinstance(seg, Segment) for seg in segments]): raise TypeError( "A Sequence must be made from a list of Segments but not all " "items in the list passed were Segments." )