def test_sanitize_slice(item, ndim, expected):
    new_item = sanitize_slices(item, ndim)
    # FIXME: do we still need the first two since the third assert
    # should cover it all?
    assert len(new_item) == ndim
    assert all(isinstance(i, (slice, int)) for i in new_item)
    assert new_item == expected
def test_sanitize_slice(item, ndim, expected):
    new_item = sanitize_slices(item, ndim)
    # FIXME: do we still need the first two since the third assert
    # should cover it all?
    assert len(new_item) == ndim
    assert all(isinstance(i, (slice, int)) for i in new_item)
    assert new_item == expected
    def __getitem__(self, value):

        log.debug(f"Retrieving slice {value} from {self}")

        value = sanitize_slices(value[::-1], self.ndim)

        # several cases:
        # integer: just use an integer
        # slice starting w/number: start number
        # slice starting w/None: use -1
        blc = [(-1 if slc.start is None else slc.start) if hasattr(
            slc, 'start') else slc for slc in value]
        # slice ending w/number >= 1: end number -1 (CASA is end-inclusive)
        # slice ending w/zero: use zero, not -1.
        # slice ending w/negative: use it, but ?????
        # slice ending w/None: use -1
        trc = [((slc.stop - 1 if slc.stop >= 1 else slc.stop) if slc.stop
                is not None else -1) if hasattr(slc, 'stop') else slc
               for slc in value]
        inc = [(slc.step or 1) if hasattr(slc, 'step') else 1 for slc in value]

        ia = self.iatool()
        # use the ia tool to get the file contents
        try:
            ia.open(self.filename, cache=False)
        except AssertionError as ex:
            if 'must be of cReqPath type' in str(ex):
                raise IOError("File {0} not found.  Error was: {1}".format(
                    self.filename, str(ex)))
            else:
                raise ex

        log.debug(f'blc={blc}, trc={trc}, inc={inc}, kwargs={self.ia_kwargs}')
        data = ia.getchunk(blc=blc, trc=trc, inc=inc, **self.ia_kwargs)
        ia.done()
        ia.close()

        log.debug(f"Done retrieving slice {value} from {self}")

        # keep all sliced axes but drop all integer axes
        new_view = [
            slice(None) if isinstance(slc, slice) else 0 for slc in value
        ]

        transposed_data = data[tuple(new_view)].transpose()

        log.debug(f"Done transposing data with view {new_view}")

        return transposed_data