本文整理匯總了Python中operator.abs方法的典型用法代碼示例。如果您正苦於以下問題:Python operator.abs方法的具體用法?Python operator.abs怎麽用?Python operator.abs使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類operator
的用法示例。
在下文中一共展示了operator.abs方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: argmax
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import abs [as 別名]
def argmax(iterable, key=None, both=False):
"""
>>> argmax([4,2,-5])
0
>>> argmax([4,2,-5], key=abs)
2
>>> argmax([4,2,-5], key=abs, both=True)
(2, 5)
"""
if key is not None:
it = imap(key, iterable)
else:
it = iter(iterable)
score, argmax = reduce(max, izip(it, count()))
if both:
return argmax, score
return argmax
示例2: compose
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import abs [as 別名]
def compose(*funcs):
"""Compose `funcs` to a single function.
>>> compose(operator.abs, operator.add)(-2,-3)
5
>>> compose()('nada')
'nada'
>>> compose(sorted, set, partial(filter, None))(range(3)[::-1]*2)
[1, 2]
"""
# slightly optimized for most common cases and hence verbose
if len(funcs) == 2: f0,f1=funcs; return lambda *a,**kw: f0(f1(*a,**kw))
elif len(funcs) == 3: f0,f1,f2=funcs; return lambda *a,**kw: f0(f1(f2(*a,**kw)))
elif len(funcs) == 0: return lambda x:x # XXX single kwarg
elif len(funcs) == 1: return funcs[0]
else:
def composed(*args,**kwargs):
y = funcs[-1](*args,**kwargs)
for f in funcs[:0:-1]: y = f(y)
return y
return composed
示例3: assignment
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import abs [as 別名]
def assignment(self):
varLst = []
while self.isAssignment():
varLst .append(self.match().value)
self.match(ASSIGN)
self.sentenceValue()
sym0 = self.getSymbol(varLst[0])
lastLevel=abs(self.level-sym0.level)
lastAddr = sym0.addr
self.genIns('STO',lastLevel,sym0.addr)
for var in varLst[1:]:
sym = self.getSymbol(var)
if sym.type=='CONST':
raise Exception('[Error]: Const "{}" can\'t be reassigned'.format(sym.name))
self.genIns('LOD',lastLevel,lastAddr)
lastLevel = abs(self.level-sym.level)
lastAddr = sym.addr
self.genIns('STO',lastLevel,sym.addr)
示例4: testAbs
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import abs [as 別名]
def testAbs(self):
self.unaryCheck(operator.abs)
示例5: checkBounds
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import abs [as 別名]
def checkBounds(self, i, j, op):
a = intbv(i)
assert a == i # just to be sure
try:
exec("a %s int(j)" % op)
except (ZeroDivisionError, ValueError):
return # prune
if not isinstance(a._val, int):
return # prune
if abs(a) > maxint * maxint:
return # keep it reasonable
if a > i:
b = intbv(i, min=i, max=a+1)
for m in (i+1, a):
b = intbv(i, min=i, max=m)
with pytest.raises(ValueError):
exec("b %s int(j)" % op)
elif a < i :
b = intbv(i, min=a, max=i+1)
exec("b %s int(j)" % op) # should be ok
for m in (a+1, i):
b = intbv(i, min=m, max=i+1)
with pytest.raises(ValueError):
exec("b %s int(j)" % op)
else: # a == i
b = intbv(i, min=i, max=i+1)
exec("b %s int(j)" % op) # should be ok
示例6: foo2
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import abs [as 別名]
def foo2():
return map(o.abs, [1, 2, 3])
示例7: __abs__
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import abs [as 別名]
def __abs__(self):
return NonStandardInteger(operator.abs(self.val))
示例8: test_abs
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import abs [as 別名]
def test_abs(self):
self.assertRaises(TypeError, operator.abs)
self.assertRaises(TypeError, operator.abs, None)
self.assertTrue(operator.abs(-1) == 1)
self.assertTrue(operator.abs(1) == 1)
示例9: setUp
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import abs [as 別名]
def setUp(self):
super(CoreUnaryOpsTest, self).setUp()
self.ops = [
('abs', operator.abs, math_ops.abs, core.abs_function),
('neg', operator.neg, math_ops.negative, core.neg),
# TODO(shoyer): add unary + to core TensorFlow
('pos', None, None, None),
('sign', None, math_ops.sign, core.sign),
('reciprocal', None, math_ops.reciprocal, core.reciprocal),
('square', None, math_ops.square, core.square),
('round', None, math_ops.round, core.round_function),
('sqrt', None, math_ops.sqrt, core.sqrt),
('rsqrt', None, math_ops.rsqrt, core.rsqrt),
('log', None, math_ops.log, core.log),
('exp', None, math_ops.exp, core.exp),
('log', None, math_ops.log, core.log),
('ceil', None, math_ops.ceil, core.ceil),
('floor', None, math_ops.floor, core.floor),
('cos', None, math_ops.cos, core.cos),
('sin', None, math_ops.sin, core.sin),
('tan', None, math_ops.tan, core.tan),
('acos', None, math_ops.acos, core.acos),
('asin', None, math_ops.asin, core.asin),
('atan', None, math_ops.atan, core.atan),
('lgamma', None, math_ops.lgamma, core.lgamma),
('digamma', None, math_ops.digamma, core.digamma),
('erf', None, math_ops.erf, core.erf),
('erfc', None, math_ops.erfc, core.erfc),
('lgamma', None, math_ops.lgamma, core.lgamma),
]
total_size = np.prod([v.size for v in self.original_lt.axes.values()])
self.test_lt = core.LabeledTensor(
math_ops.cast(self.original_lt, dtypes.float32) / total_size,
self.original_lt.axes)
示例10: __abs__
# 需要導入模塊: import operator [as 別名]
# 或者: from operator import abs [as 別名]
def __abs__(self):
return self._uop(operator.abs)