本文整理匯總了Python中operator.neg方法的典型用法代碼示例。如果您正苦於以下問題:Python operator.neg方法的具體用法?Python operator.neg怎麽用?Python operator.neg使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類operator
的用法示例。
在下文中一共展示了operator.neg方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _add_numeric_methods_unary
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def _add_numeric_methods_unary(cls):
"""
Add in numeric unary methods.
"""
def _make_evaluate_unary(op, opstr):
def _evaluate_numeric_unary(self):
self._validate_for_numeric_unaryop(op, opstr)
attrs = self._get_attributes_dict()
attrs = self._maybe_update_attributes(attrs)
return Index(op(self.values), **attrs)
_evaluate_numeric_unary.__name__ = opstr
return _evaluate_numeric_unary
cls.__neg__ = _make_evaluate_unary(operator.neg, '__neg__')
cls.__pos__ = _make_evaluate_unary(operator.pos, '__pos__')
cls.__abs__ = _make_evaluate_unary(np.abs, '__abs__')
cls.__inv__ = _make_evaluate_unary(lambda x: -x, '__inv__')
示例2: _add_numeric_methods_unary
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def _add_numeric_methods_unary(cls):
""" add in numeric unary methods """
def _make_evaluate_unary(op, opstr):
def _evaluate_numeric_unary(self):
self._validate_for_numeric_unaryop(op, opstr)
attrs = self._get_attributes_dict()
attrs = self._maybe_update_attributes(attrs)
return Index(op(self.values), **attrs)
return _evaluate_numeric_unary
cls.__neg__ = _make_evaluate_unary(operator.neg, '__neg__')
cls.__pos__ = _make_evaluate_unary(operator.pos, '__pos__')
cls.__abs__ = _make_evaluate_unary(np.abs, '__abs__')
cls.__inv__ = _make_evaluate_unary(lambda x: -x, '__inv__')
示例3: testNeg
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def testNeg(self):
self.unaryCheck(operator.neg)
示例4: __neg__
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def __neg__(self) -> bool:
return operator.neg(self.size)
示例5: __neg__
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def __neg__(self):
return DeferredTensor.apply(operator.neg, self)
# We need to override "both sides" of the below operators (e.g. both __add__
# and __radd__) since other might not be a DeferredTensor (but will be casted
# to one inside apply()).
示例6: test_unary_methods
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def test_unary_methods(self):
array = np.array([-1, 0, 1, 2])
array_like = ArrayLike(array)
for op in [operator.neg,
operator.pos,
abs,
operator.invert]:
_assert_equal_type_and_value(op(array_like), ArrayLike(op(array)))
示例7: test_exceptions
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def test_exceptions(self):
a = np.ones((), dtype=np.bool_)[()]
assert_raises(TypeError, operator.neg, a)
示例8: test_result
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def test_result(self):
types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
with suppress_warnings() as sup:
sup.filter(RuntimeWarning)
for dt in types:
a = np.ones((), dtype=dt)[()]
assert_equal(operator.neg(a) + a, 0)
示例9: _add_unary_ops
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def _add_unary_ops(cls):
cls.__pos__ = cls._create_unary_method(operator.pos)
cls.__neg__ = cls._create_unary_method(operator.neg)
cls.__invert__ = cls._create_unary_method(operator.invert)
示例10: eval_unary_operator
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def eval_unary_operator(node, env):
operations = {
'-': operator.neg,
'!': operator.not_,
}
return operations[node.operator](eval_expression(node.right, env))
示例11: __neg__
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def __neg__(self):
''' Return element-wise negative. '''
return PhyDim2(*map(neg, self))
示例12: __neg__
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def __neg__(self):
return NonStandardInteger(operator.neg(self.val))
示例13: test_max
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def test_max(self):
self.assertEqual(max('123123'), '3')
self.assertEqual(max(1, 2, 3), 3)
self.assertEqual(max((1, 2, 3, 1, 2, 3)), 3)
self.assertEqual(max([1, 2, 3, 1, 2, 3]), 3)
self.assertEqual(max(1, 2L, 3.0), 3.0)
self.assertEqual(max(1L, 2.0, 3), 3)
self.assertEqual(max(1.0, 2, 3L), 3L)
for stmt in (
"max(key=int)", # no args
"max(1, key=int)", # single arg not iterable
"max(1, 2, keystone=int)", # wrong keyword
"max(1, 2, key=int, abc=int)", # two many keywords
"max(1, 2, key=1)", # keyfunc is not callable
):
try:
exec(stmt) in globals()
except TypeError:
pass
else:
self.fail(stmt)
self.assertEqual(max((1,), key=neg), 1) # one elem iterable
self.assertEqual(max((1,2), key=neg), 1) # two elem iterable
self.assertEqual(max(1, 2, key=neg), 1) # two elems
data = [random.randrange(200) for i in range(100)]
keys = dict((elem, random.randrange(50)) for elem in data)
f = keys.__getitem__
self.assertEqual(max(data, key=f),
sorted(reversed(data), key=f)[-1])
示例14: test_imap
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def test_imap(self):
self.assertEqual(list(imap(operator.pow, range(3), range(1,7))),
[0**1, 1**2, 2**3])
self.assertEqual(list(imap(None, 'abc', range(5))),
[('a',0),('b',1),('c',2)])
self.assertEqual(list(imap(None, 'abc', count())),
[('a',0),('b',1),('c',2)])
self.assertEqual(take(2,imap(None, 'abc', count())),
[('a',0),('b',1)])
self.assertEqual(list(imap(operator.pow, [])), [])
self.assertRaises(TypeError, imap)
self.assertRaises(TypeError, imap, operator.neg)
self.assertRaises(TypeError, imap(10, range(5)).next)
self.assertRaises(ValueError, imap(errfunc, [4], [5]).next)
self.assertRaises(TypeError, imap(onearg, [4], [5]).next)
示例15: test_neg
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import neg [as 別名]
def test_neg(self):
self.assertRaises(TypeError, operator.neg)
self.assertRaises(TypeError, operator.neg, None)
self.assertTrue(operator.neg(5) == -5)
self.assertTrue(operator.neg(-5) == 5)
self.assertTrue(operator.neg(0) == 0)
self.assertTrue(operator.neg(-0) == 0)