本文整理匯總了Python中operator.getitem方法的典型用法代碼示例。如果您正苦於以下問題:Python operator.getitem方法的具體用法?Python operator.getitem怎麽用?Python operator.getitem使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類operator
的用法示例。
在下文中一共展示了operator.getitem方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_index_and_slice
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def test_index_and_slice(self, size):
t = (None,) * size
self.assertEqual(len(t), size)
self.assertEqual(t[-1], None)
self.assertEqual(t[5], None)
self.assertEqual(t[size - 1], None)
self.assertRaises(IndexError, operator.getitem, t, size)
self.assertEqual(t[:5], (None,) * 5)
self.assertEqual(t[-5:], (None,) * 5)
self.assertEqual(t[20:25], (None,) * 5)
self.assertEqual(t[-25:-20], (None,) * 5)
self.assertEqual(t[size - 5:], (None,) * 5)
self.assertEqual(t[size - 5:size], (None,) * 5)
self.assertEqual(t[size - 6:size - 2], (None,) * 4)
self.assertEqual(t[size:size], ())
self.assertEqual(t[size:size+5], ())
# Like test_concat, split in two.
示例2: emit
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def emit(self):
"""
Add the currently parsed input to the result.
"""
if self._buffer:
attrs = [getattr(attributes, name) for name in self._attrs]
attrs.extend(filter(None, [self.foreground, self.background]))
if not attrs:
attrs.append(attributes.normal)
attrs.append(self._buffer)
attr = _foldr(operator.getitem, attrs.pop(), attrs)
if self._result is None:
self._result = attr
else:
self._result[attr]
self._buffer = ''
示例3: test_recordIndexable
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def test_recordIndexable(self):
"""
The shadow user record returned by I{getpwnam} and I{getspall} is
indexable, with successive indexes starting from 0 corresponding to the
values of the C{sp_nam}, C{sp_pwd}, C{sp_lstchg}, C{sp_min}, C{sp_max},
C{sp_warn}, C{sp_inact}, C{sp_expire}, and C{sp_flag} attributes,
respectively.
"""
db = self.database
(username, password, lastChange, min, max, warn, inact, expire,
flag) = self.getExistingUserInfo()
for entry in [db.getspnam(username), db.getspall()[0]]:
self.assertEqual(entry[0], username)
self.assertEqual(entry[1], password)
self.assertEqual(entry[2], lastChange)
self.assertEqual(entry[3], min)
self.assertEqual(entry[4], max)
self.assertEqual(entry[5], warn)
self.assertEqual(entry[6], inact)
self.assertEqual(entry[7], expire)
self.assertEqual(entry[8], flag)
self.assertEqual(len(entry), len(list(entry)))
self.assertRaises(IndexError, getitem, entry, 9)
示例4: get_in
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def get_in(keys, coll, default=None):
""" Reaches into nested associative data structures. Returns the value for path ``keys``.
If the path doesn't exist returns ``default``.
>>> transaction = {'name': 'Alice',
... 'purchase': {'items': ['Apple', 'Orange'],
... 'costs': [0.50, 1.25]},
... 'credit card': '5555-1234-1234-1234'}
>>> get_in(['purchase', 'items', 0], transaction)
'Apple'
>>> get_in(['name'], transaction)
'Alice'
>>> get_in(['purchase', 'total'], transaction)
>>> get_in(['purchase', 'items', 'apple'], transaction)
>>> get_in(['purchase', 'items', 10], transaction)
>>> get_in(['purchase', 'total'], transaction, 0)
0
"""
try:
return functools.reduce(operator.getitem, keys, coll)
except (KeyError, IndexError, TypeError):
return default
示例5: test_training_log
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def test_training_log():
log = TrainingLog()
# test basic writing capabilities
log[0]['field'] = 45
assert log[0]['field'] == 45
assert log[1] == {}
assert log.current_row['field'] == 45
log.status['iterations_done'] += 1
assert log.status['iterations_done'] == 1
assert log.previous_row['field'] == 45
assert_raises(ValueError, getitem, log, -1)
# test iteration
assert len(list(log)) == 2
示例6: __reduce__
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def __reduce__(self):
if self._special:
return self._name
if self._name:
origin = globals()[self._name]
else:
origin = self.__origin__
if (origin is Callable and
not (len(self.__args__) == 2 and self.__args__[0] is Ellipsis)):
args = list(self.__args__[:-1]), self.__args__[-1]
else:
args = tuple(self.__args__)
if len(args) == 1 and not isinstance(args[0], tuple):
args, = args
return operator.getitem, (origin, args)
示例7: _dot_get
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def _dot_get(self, metric, obj):
try:
return reduce(getitem, metric.split('.'), obj)
except Exception as e:
return False
示例8: __getitem__
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def __getitem__(self, slice_spec):
# Notice that because the slice_spec is a part of the callback, we can't use
# operator.getitem, and therefore derived DeferredTensors created by
# indexing the same DeferredTensor in the same way will not be considered
# equal.
return DeferredTensor.apply(lambda arg: arg[slice_spec], self)
示例9: assert_equal_records
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def assert_equal_records(a, b):
"""
Asserts that two records are equal.
Pretty crude for now.
"""
assert_equal(a.dtype, b.dtype)
for f in a.dtype.names:
(af, bf) = (operator.getitem(a, f), operator.getitem(b, f))
if not (af is masked) and not (bf is masked):
assert_equal(operator.getitem(a, f), operator.getitem(b, f))
return
示例10: test_fields_by_index
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def test_fields_by_index(self):
dt = np.dtype([('a', np.int8), ('b', np.float32, 3)])
assert_dtype_equal(dt[0], np.dtype(np.int8))
assert_dtype_equal(dt[1], np.dtype((np.float32, 3)))
assert_dtype_equal(dt[-1], dt[1])
assert_dtype_equal(dt[-2], dt[0])
assert_raises(IndexError, lambda: dt[-3])
assert_raises(TypeError, operator.getitem, dt, 3.0)
assert_raises(TypeError, operator.getitem, dt, [])
assert_equal(dt[1], dt[np.int8(1)])
示例11: bit
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def bit(h, i):
return (operator.getitem(h, i // 8) >> (i % 8)) & 1
示例12: signature_unsafe
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def signature_unsafe(m, sk, pk):
"""
Not safe to use with secret keys or secret data.
See module docstring. This function should be used for testing only.
"""
h = H(sk)
a = 2 ** (b - 2) + sum(2 ** i * bit(h, i) for i in range(3, b - 2))
r = Hint(
bytes([operator.getitem(h, j) for j in range(b // 8, b // 4)]) + m
)
R = scalarmult_B(r)
S = (r + Hint(encodepoint(R) + pk + m) * a) % l
return encodepoint(R) + encodeint(S)
示例13: test_char_ptr
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def test_char_ptr(self):
s = "abcdefghijklmnopqrstuvwxyz"
dll = CDLL(_ctypes_test.__file__)
dll.my_strdup.restype = POINTER(c_char)
dll.my_free.restype = None
res = dll.my_strdup(s)
self.assertEqual(res[:len(s)], s)
self.assertEqual(res[:3], s[:3])
self.assertEqual(res[:len(s):], s)
self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
self.assertEqual(res[0:-1:-1], s[0::-1])
import operator
self.assertRaises(ValueError, operator.getitem,
res, slice(None, None, None))
self.assertRaises(ValueError, operator.getitem,
res, slice(0, None, None))
self.assertRaises(ValueError, operator.getitem,
res, slice(None, 5, -1))
self.assertRaises(ValueError, operator.getitem,
res, slice(-5, None, None))
self.assertRaises(TypeError, operator.setslice,
res, 0, 5, u"abcde")
self.assertRaises(TypeError, operator.setitem,
res, slice(0, 5), u"abcde")
dll.my_free(res)
dll.my_strdup.restype = POINTER(c_byte)
res = dll.my_strdup(s)
self.assertEqual(res[:len(s)], range(ord("a"), ord("z")+1))
self.assertEqual(res[:len(s):], range(ord("a"), ord("z")+1))
dll.my_free(res)
示例14: test_slice_and_getitem
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def test_slice_and_getitem(self, size):
SUBSTR = '0123456789'
sublen = len(SUBSTR)
s = SUBSTR * (size // sublen)
stepsize = len(s) // 100
stepsize = stepsize - (stepsize % sublen)
for i in range(0, len(s) - stepsize, stepsize):
self.assertEqual(s[i], SUBSTR[0])
self.assertEqual(s[i:i + sublen], SUBSTR)
self.assertEqual(s[i:i + sublen:2], SUBSTR[::2])
if i > 0:
self.assertEqual(s[i + sublen - 1:i - 1:-3],
SUBSTR[sublen::-3])
# Make sure we do some slicing and indexing near the end of the
# string, too.
self.assertEqual(s[len(s) - 1], SUBSTR[-1])
self.assertEqual(s[-1], SUBSTR[-1])
self.assertEqual(s[len(s) - 10], SUBSTR[0])
self.assertEqual(s[-sublen], SUBSTR[0])
self.assertEqual(s[len(s):], '')
self.assertEqual(s[len(s) - 1:], SUBSTR[-1])
self.assertEqual(s[-1:], SUBSTR[-1])
self.assertEqual(s[len(s) - sublen:], SUBSTR)
self.assertEqual(s[-sublen:], SUBSTR)
self.assertEqual(len(s[:]), len(s))
self.assertEqual(len(s[:len(s) - 5]), len(s) - 5)
self.assertEqual(len(s[5:-5]), len(s) - 10)
self.assertRaises(IndexError, operator.getitem, s, len(s))
self.assertRaises(IndexError, operator.getitem, s, len(s) + 1)
self.assertRaises(IndexError, operator.getitem, s, len(s) + 1<<31)
示例15: test_getwindowsversion
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import getitem [as 別名]
def test_getwindowsversion(self):
# Raise SkipTest if sys doesn't have getwindowsversion attribute
test.test_support.get_attribute(sys, "getwindowsversion")
v = sys.getwindowsversion()
self.assertEqual(len(v), 5)
self.assertIsInstance(v[0], int)
self.assertIsInstance(v[1], int)
self.assertIsInstance(v[2], int)
self.assertIsInstance(v[3], int)
self.assertIsInstance(v[4], str)
self.assertRaises(IndexError, operator.getitem, v, 5)
self.assertIsInstance(v.major, int)
self.assertIsInstance(v.minor, int)
self.assertIsInstance(v.build, int)
self.assertIsInstance(v.platform, int)
self.assertIsInstance(v.service_pack, str)
if sys.platform != 'cli':
# These are not available on .NET
self.assertIsInstance(v.service_pack_minor, int)
self.assertIsInstance(v.service_pack_major, int)
self.assertIsInstance(v.suite_mask, int)
self.assertIsInstance(v.product_type, int)
self.assertEqual(v[0], v.major)
self.assertEqual(v[1], v.minor)
self.assertEqual(v[2], v.build)
self.assertEqual(v[3], v.platform)
self.assertEqual(v[4], v.service_pack)
# This is how platform.py calls it. Make sure tuple
# still has 5 elements
maj, min, buildno, plat, csd = sys.getwindowsversion()