本文整理汇总了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)