def __getitem__(self, idx):
if isinstance(idx, slice):
start, stop, step = idx.indices(len(self))
return [self._revoked_cert(i) for i in range(start, stop, step)]
else:
idx = operator.index(idx)
if idx < 0:
idx += len(self)
if not 0 <= idx < len(self):
raise IndexError
return self._revoked_cert(idx)
python类index()的实例源码
def __init__(self, capacity):
if not isinstance(capacity, int):
raise TypeError("capacity must be an integer")
if capacity < 1:
raise ValueError("capacity must be >= 1")
# Invariants:
# get_semaphore.value() == len(self._data)
# put_semaphore.value() + get_semaphore.value() = capacity
self.capacity = operator.index(capacity)
self._put_semaphore = Semaphore(capacity, max_value=capacity)
self._get_semaphore = Semaphore(0, max_value=capacity)
self._data = deque()
self._join_lot = _core.ParkingLot()
self._unprocessed = 0
def _check_max_bytes(self, max_bytes):
if max_bytes is None:
return
max_bytes = operator.index(max_bytes)
if max_bytes < 1:
raise ValueError("max_bytes must be >= 1")
def __index__(self):
return operator.index(self.__wrapped__)
def test_basic(self):
self.o.ind = -2
self.n.ind = 2
self.assertEqual(operator.index(self.o), -2)
self.assertEqual(operator.index(self.n), 2)
def test_error(self):
self.o.ind = 'dumb'
self.n.ind = 'bad'
self.assertRaises(TypeError, operator.index, self.o)
self.assertRaises(TypeError, operator.index, self.n)
self.assertRaises(TypeError, slice(self.o).indices, 0)
self.assertRaises(TypeError, slice(self.n).indices, 0)
def __getitem__(self, index):
return self._list[index]
def test_proxy_index(self):
class C:
def __index__(self):
return 10
o = C()
p = weakref.proxy(o)
self.assertEqual(operator.index(p), 10)
def test_indexing(self):
a = np.array([[[5]]])
def assert_deprecated(*args, **kwargs):
self.assert_deprecated(*args, exceptions=(IndexError,), **kwargs)
assert_deprecated(lambda: a[0.0])
assert_deprecated(lambda: a[0, 0.0])
assert_deprecated(lambda: a[0.0, 0])
assert_deprecated(lambda: a[0.0,:])
assert_deprecated(lambda: a[:, 0.0])
assert_deprecated(lambda: a[:, 0.0,:])
assert_deprecated(lambda: a[0.0,:,:])
assert_deprecated(lambda: a[0, 0, 0.0])
assert_deprecated(lambda: a[0.0, 0, 0])
assert_deprecated(lambda: a[0, 0.0, 0])
assert_deprecated(lambda: a[-1.4])
assert_deprecated(lambda: a[0, -1.4])
assert_deprecated(lambda: a[-1.4, 0])
assert_deprecated(lambda: a[-1.4,:])
assert_deprecated(lambda: a[:, -1.4])
assert_deprecated(lambda: a[:, -1.4,:])
assert_deprecated(lambda: a[-1.4,:,:])
assert_deprecated(lambda: a[0, 0, -1.4])
assert_deprecated(lambda: a[-1.4, 0, 0])
assert_deprecated(lambda: a[0, -1.4, 0])
# Test that the slice parameter deprecation warning doesn't mask
# the scalar index warning.
assert_deprecated(lambda: a[0.0:, 0.0], num=2)
assert_deprecated(lambda: a[0.0:, 0.0,:], num=2)
def test_bool_as_int_argument(self):
a = np.array([[[1]]])
self.assert_deprecated(np.reshape, args=(a, (True, -1)))
self.assert_deprecated(np.reshape, args=(a, (np.bool_(True), -1)))
# Note that operator.index(np.array(True)) does not work, a boolean
# array is thus also deprecated, but not with the same message:
assert_raises(TypeError, operator.index, np.array(True))
self.assert_deprecated(np.take, args=(a, [0], False))
self.assert_deprecated(lambda: a[False:True:True], exceptions=IndexError, num=3)
self.assert_deprecated(lambda: a[False, 0], exceptions=IndexError)
self.assert_deprecated(lambda: a[False, 0, 0], exceptions=IndexError)
def test_array_to_index_deprecation(self):
# This drops into the non-integer deprecation, which is ignored here,
# so no exception is expected. The raising is effectively tested above.
a = np.array([[[1]]])
self.assert_deprecated(operator.index, args=(np.array([1]),))
self.assert_deprecated(np.reshape, args=(a, (a, -1)), exceptions=())
self.assert_deprecated(np.take, args=(a, [0], a), exceptions=())
# Check slicing. Normal indexing checks arrays specifically.
self.assert_deprecated(lambda: a[a:a:a], exceptions=(), num=3)
def _validate_axis(axis, ndim, argname):
try:
axis = [operator.index(axis)]
except TypeError:
axis = list(axis)
axis = [a + ndim if a < 0 else a for a in axis]
if not builtins.all(0 <= a < ndim for a in axis):
raise ValueError('invalid axis for this array in `%s` argument' %
argname)
if len(set(axis)) != len(axis):
raise ValueError('repeated axis in `%s` argument' % argname)
return axis
def test_basic(self):
self.o.ind = -2
self.n.ind = 2
self.assertEqual(operator.index(self.o), -2)
self.assertEqual(operator.index(self.n), 2)
def test_error(self):
self.o.ind = 'dumb'
self.n.ind = 'bad'
self.assertRaises(TypeError, operator.index, self.o)
self.assertRaises(TypeError, operator.index, self.n)
self.assertRaises(TypeError, slice(self.o).indices, 0)
self.assertRaises(TypeError, slice(self.n).indices, 0)
def __getitem__(self, index):
return self._list[index]
def test_proxy_index(self):
class C:
def __index__(self):
return 10
o = C()
p = weakref.proxy(o)
self.assertEqual(operator.index(p), 10)
def test_basic(self):
self.o.ind = -2
self.n.ind = 2
self.assertEqual(operator.index(self.o), -2)
self.assertEqual(operator.index(self.n), 2)
def test_error(self):
self.o.ind = 'dumb'
self.n.ind = 'bad'
self.assertRaises(TypeError, operator.index, self.o)
self.assertRaises(TypeError, operator.index, self.n)
self.assertRaises(TypeError, slice(self.o).indices, 0)
self.assertRaises(TypeError, slice(self.n).indices, 0)
def __getitem__(self, index):
return self._list[index]
def test_proxy_index(self):
class C:
def __index__(self):
return 10
o = C()
p = weakref.proxy(o)
self.assertEqual(operator.index(p), 10)