示例#1
0
def test_ifirst_is_not():
    assert ifirst_is_not([1, 2, 2], 2) == 0
    assert ifirst_is_not([2, 1, 1], 2) == 1
    assert ifirst_is_not([{}, None, None], None) == 0
    assert_raises(ValueError, ifirst_is_not, [], None)
    assert_raises(ValueError, ifirst_is_not, [None, None], None)

    assert ilast_is_not([1, 2, 2], 2) == 0
    assert ilast_is_not([2, 1, 1], 2) == 2
    assert ilast_is_not([{}, None, None], None) == 0
    assert_raises(ValueError, ilast_is_not, [], None)
    assert_raises(ValueError, ilast_is_not, [None, None], None)
示例#2
0
def test_ifirst_is_not():
    assert ifirst_is_not([1, 2, 2], 2) == 0
    assert ifirst_is_not([2, 1, 1], 2) == 1
    assert ifirst_is_not([{}, None, None], None) == 0
    assert_raises(ValueError, ifirst_is_not, [], None)
    assert_raises(ValueError, ifirst_is_not, [None, None], None,)

    assert ilast_is_not([1, 2, 2], 2) == 0
    assert ilast_is_not([2, 1, 1], 2) == 2
    assert ilast_is_not([{}, None, None], None) == 0
    assert_raises(ValueError, ilast_is_not, [], None)
    assert_raises(ValueError, ilast_is_not, [None, None], None,)
示例#3
0
    def _normalize_selection(self, selection, shape):
        # return the selection as an array of int or bool, a slice, an Ellipsis
        # or a tuple.
        if isinstance(selection, list) and len(selection) == 0:
            return np.array([], self._dtype_index)
        if selection is None or selection is Ellipsis or \
           isinstance(selection, tuple) and len(selection) == 0:
            return Ellipsis
        if isscalarlike(selection):
            return np.asarray([selection], self._dtype_index)
        if isinstance(selection, slice):
            return self._normalize_slice(selection, product(shape))
        if isinstance(selection, (list, tuple)):
            selection_ = np.asarray(selection)
            if selection_.dtype == object:
                if len(selection) > len(shape):
                    raise ValueError('Invalid selection dimensions.')
                selection = tuple(self._normalize_slice(_, s)
                                  if isinstance(_, slice) else _
                                  for _, s in zip(selection, shape))
                try:
                    return selection[:ilast_is_not(selection, Ellipsis)+1]
                except ValueError:
                    return Ellipsis
            else:
                selection = selection_
        elif not isinstance(selection, np.ndarray):
            raise TypeError('Invalid selection.')

        if selection.dtype not in (bool, int):
            raise TypeError('Invalid selection.')
        if selection.dtype == int:
            if selection.ndim != 1:
                raise ValueError('Index selection is not 1-dimensional.')
        elif selection.shape != shape:
            raise ValueError('Invalid boolean selection dimensions.')

        return selection
示例#4
0
    def _normalize_selection(self, selection, shape):
        # return the selection as an array of int or bool, a slice, an Ellipsis
        # or a tuple.
        if isinstance(selection, list) and len(selection) == 0:
            return np.array([], self._dtype_index)
        if selection is None or selection is Ellipsis or \
           isinstance(selection, tuple) and len(selection) == 0:
            return Ellipsis
        if isscalarlike(selection):
            return np.asarray([selection], self._dtype_index)
        if isinstance(selection, slice):
            return self._normalize_slice(selection, product(shape))
        if isinstance(selection, (list, tuple)):
            selection_ = np.asarray(selection)
            if selection_.dtype == object:
                if len(selection) > len(shape):
                    raise ValueError('Invalid selection dimensions.')
                selection = tuple(
                    self._normalize_slice(_, s) if isinstance(_, slice) else _
                    for _, s in zip(selection, shape))
                try:
                    return selection[:ilast_is_not(selection, Ellipsis) + 1]
                except ValueError:
                    return Ellipsis
            else:
                selection = selection_
        elif not isinstance(selection, np.ndarray):
            raise TypeError('Invalid selection.')

        if selection.dtype not in (bool, int):
            raise TypeError('Invalid selection.')
        if selection.dtype == int:
            if selection.ndim != 1:
                raise ValueError('Index selection is not 1-dimensional.')
        elif selection.shape != shape:
            raise ValueError('Invalid boolean selection dimensions.')

        return selection