def opr(operation, op1, op2=()): result = () if isinstance(op1, types_Number): # prefix +/- if op2==(): if operation=='+': result = operator.pos(op1) elif operation=='-': result = operator.neg(op1) elif isinstance(op2, types_Number): if operation=='+': result = operator.add(op1,op2) elif operation=='-': result = operator.sub(op1,op2) # string '+' elif operation=='+' and all([isinstance(x,types.StringTypes) for x in (op1,op2)]): result = operator.add(op1,op2) return result
def test_tag(self): for frame_id, data, value, intval, info in self.DATA: kind = self._get_frame(frame_id) tag = kind._fromData(_23, 0, data) self.failUnless(tag.HashKey) self.failUnless(tag.pprint()) self.assertEquals(value, tag) if 'encoding' not in info: self.assertRaises(AttributeError, getattr, tag, 'encoding') for attr, value in iteritems(info): t = tag if not isinstance(value, list): value = [value] t = [t] for value, t in izip(value, iter(t)): if isinstance(value, float): self.failUnlessAlmostEqual(value, getattr(t, attr), 5) else: self.assertEquals(value, getattr(t, attr)) if isinstance(intval, integer_types): self.assertEquals(intval, operator.pos(t)) else: self.assertRaises(TypeError, operator.pos, t)
def visit_UnaryOpExpr(self, node): # TODO: handle TypeError 'bad operand type for ...' exceptions operand = self.visit(node.operand) if isinstance(node.op, nodes.UMinus): return operator.neg(self.node_as_value(operand)) elif isinstance(node.op, nodes.UPlus): return operator.pos(self.node_as_value(operand)) else: raise RuntimeError() # pragma: no cover
def op_pos(x): return _op.pos(x) @cutype("a -> a")
def __pos__(self): return operator.pos(self._as_ParseObj())
def test_pos(self): self.failUnlessRaises(TypeError, operator.pos) self.failUnlessRaises(TypeError, operator.pos, None) self.failUnless(operator.pos(5) == 5) self.failUnless(operator.pos(-5) == -5) self.failUnless(operator.pos(0) == 0) self.failUnless(operator.pos(-0) == 0)
def test_pos(self): self.assertRaises(TypeError, operator.pos) self.assertRaises(TypeError, operator.pos, None) self.assertTrue(operator.pos(5) == 5) self.assertTrue(operator.pos(-5) == -5) self.assertTrue(operator.pos(0) == 0) self.assertTrue(operator.pos(-0) == 0)
def main(): a = -1 b = 5.0 c = 2 d = 6 for k, v in {"a": a, "b": b, "c": c, "d": d}.items(): print(k + " =", v) print("\nPositive/Negative:") print("abs(a);", operator.abs(a)) print("neg(a);", operator.neg(a)) print("neg(b);", operator.neg(b)) print("pos(a);", operator.pos(a)) print("pos(b);", operator.pos(b)) print("\nArithmetic:") print("add(a, b) :", operator.add(a, b)) print("floordiv(a, b):", operator.floordiv(a, b)) print("floordiv(d, c):", operator.floordiv(d, c)) print("mod(a, b) :", operator.mod(a, b)) print("mul(a, b) :", operator.mul(a, b)) print("pow(c, d) :", operator.pow(c, d)) print("sub(b, a) :", operator.sub(b, a)) print("truediv(a, b) :", operator.truediv(a, b)) print("truediv(d, c) :", operator.truediv(d, c)) print("\nBitwise:") print("and_(c, d) :", operator.and_(c, d)) print("invert(c) :", operator.invert(c)) print("lshift(c, d):", operator.lshift(c, d)) print("or_(c, d) :", operator.or_(c, d)) print("rshift(d, c):", operator.rshift(d, c)) print("xor(c, d) :", operator.xor(c, d))
def test_pos(self): self.failUnlessRaises(TypeError, operator.pos) self.failUnlessRaises(TypeError, operator.pos, None) self.assertEqual(operator.pos(5), 5) self.assertEqual(operator.pos(-5), -5) self.assertEqual(operator.pos(0), 0) self.assertEqual(operator.pos(-0), 0)
def __pos__(self) -> "Vec2d": """Return the unary pos of the Vec2d. >>> +Vec2d(1,-2) Vec2d(1, -2) """ return Vec2d(operator.pos(self.x), operator.pos(self.y))
def test_pos(self): self.assertRaises(TypeError, operator.pos) self.assertRaises(TypeError, operator.pos, None) self.assertEqual(operator.pos(5), 5) self.assertEqual(operator.pos(-5), -5) self.assertEqual(operator.pos(0), 0) self.assertEqual(operator.pos(-0), 0)
def test_pos(self): #operator = self.module self.assertRaises(TypeError, operator.pos) self.assertRaises(TypeError, operator.pos, None) self.assertEqual(operator.pos(5), 5) self.assertEqual(operator.pos(-5), -5) self.assertEqual(operator.pos(0), 0) self.assertEqual(operator.pos(-0), 0)
def test_metrics_pos(): first_metric = DummyMetric(-1) final_pos = pos(first_metric) assert isinstance(final_pos, CompositionalMetric) final_pos.update() assert torch.allclose(tensor(1), final_pos.compute())
def overload_concat_add(*args): if len(args) == 1: return op.pos(args[0]) if all(hasattr(type(v),'__iter__') for v in (args)): return op.concat(*args) else: return op.add(*args)
def run301_03(): """ arithmetic ops :return: """ a = -1 b = 5.0 c = 2 d = 6 print('a =', a) print('b =', b) print('c =', c) print('d =', d) print('\nPositive/Negative:') print('abs(a):', abs(a)) print('neg(a):', neg(a)) print('neg(b):', neg(b)) print('pos(a):', pos(a)) print('pos(b):', pos(b)) print('\nArithmetic:') print('add(a, b) :', add(a, b)) print('floordiv(a, b):', floordiv(a, b)) # for py2 print('floordiv(d, c):', floordiv(d, c)) # for py2 print('mod(a, b) :', mod(a, b)) print('mul(a, b) :', mul(a, b)) print('pow(c, d) :', pow(c, d)) print('sub(b, a) :', sub(b, a)) print('truediv(a, b) :', truediv(a, b)) print('truediv(d, c) :', truediv(d, c)) print('\nBitwise:') print('and_(c, d) :', and_(c, d)) print('invert(c) :', invert(c)) # ~c print('lshift(c, d):', lshift(c, d)) # c << d print('or_(c, d) :', or_(c, d)) print('rshift(d, c):', rshift(d, c)) # d >> c print('xor(c, d) :', xor(c, d)) # 不同得1 ^
def operator_arithmetic(): """ arithmetic, math +, -, |a| +, -, *, //, /, %, pow &, |, ^, ~, <<, >> """ a, b, c, d = -1, 5.0, 2, 6 print('a =', a) print('b =', b) print('c =', c) print('d =', d) print('\nPositive/Negative:') print('abs(a):', operator.abs(a)) print('neg(a):', operator.neg(a)) print('neg(b):', operator.neg(b)) print('pos(a):', operator.pos(a)) print('pos(b):', operator.pos(b)) print('\nArithmetic:') print('add(a, b) :', operator.add(a, b)) print('sub(b, a) :', operator.sub(b, a)) print('mul(a, b) :', operator.mul(a, b)) print('truediv(a, b) :', operator.truediv(a, b)) print('truediv(d, c) :', operator.truediv(d, c)) print('floordiv(a, b) :', operator.floordiv(a, b)) print('pow(c, d) :', operator.pow(c, d)) print('mod(a, b) :', operator.mod(a, b)) print('\nBitwise:') print('and_(c, d) :', operator.and_(c, d)) # c & d print('or_(c, d) :', operator.or_(c, d)) # c | d print('invert(c) :', operator.invert(c)) # two's complement. ~c = -c - 1 print('xor(c, d) :', operator.xor(c, d)) # a ^ b print('lshift(c, d) :', operator.lshift(c, d)) # d << c print('rshift(d, c) :', operator.rshift(d, c)) # d >> c
def opr(operation, op1, op2=()): result = () if isinstance(op1, types_Number): # prefix +/- if op2 == (): if operation == '+': result = operator.pos(op1) elif operation == '-': result = operator.neg(op1) elif isinstance(op2, types_Number): if operation == '+': result = operator.add(op1, op2) elif operation == '-': result = operator.sub(op1, op2) # string '+' elif operation == '+' and all( [isinstance(x, types.StringTypes) for x in (op1, op2)]): result = operator.add(op1, op2) return result
def __pos__(self): return NonStandardInteger(operator.pos(self.val))
b = 1 print operator.is_(a, b) b = 2 print operator.is_(a, b) a = -1 print id(a) print operator.index(1) print operator.inv(8) print operator.lshift(4, 1) print operator.or_(2,4) print operator.pos(30) print operator.pow(2,8)
def test_pos_np(): a = np.array([1, 2]) * PhysicalQuantity(1, 'm') assert np.any(operator.pos(a) == a)
def positive(x, **_): return operator.pos(x)
def __pos__(self): return Vec3d(operator.pos(self.x), operator.pos(self.y), operator.pos(self.z))
def test_pos(self): self.failUnless(operator.pos(5) == 5) self.failUnless(operator.pos(-5) == -5) self.failUnless(operator.pos(0) == 0) self.failUnless(operator.pos(-0) == 0)
def unary_positive_usecase(x): return operator.pos(x)
#但是不改变自身的值,返回值返回相除的结果 a = 9 b = operator.idiv(a, 4) print a print b #将与自身相除取整的值赋给自身 同 //= #但是不改变自身的值,返回值返回相除的结果 a = 9.04 b = operator.ifloordiv(a, 4) print a print b #将与自身相移位的值赋给自身 同 <<= #但是不改变自身的值,返回值返回移位的结果 a = 3 b = operator.ilshift(3, 2) print a print b #将与自身相移位的值赋给自身 同 >>= #但是不改变自身的值,返回值返回移位的结果 a = 3 b = operator.irshift(3, 2) print a print b #同 +a #并不知道有什么用 operator.pos(3)
def __pos__(self): """Retourne la même valeur pour chaques éléments du vecteur""" return Vector2D(operator.pos(self.x), operator.pos(self.y))
def __pos__(self) -> int: return operator.pos(object.__getattribute__(self, "_obj"))
def pos(a): return operator.pos(a)
def __pos__ (self): return Euler([operator.pos(c) for c in self]) # binary operator helpers def _BinaryOp(self, other, op):
a = -1 b = 5.0 c = 2 d = 6 print('a = ', a) print('b = ', b) print('c = ', c) print('d = ', d) print('\nPositive/Negative:') print(f'abs({a}):', abs(a)) print(f'neg({a}):', neg(a)) print(f'neg({b}):', neg(b)) print(f'pos({a}):', pos(a)) print(f'pos({b}):', pos(b)) print('\nArithmetic:') print(f'add({a}, {b}):', add(a, b)) print(f'floordiv({a}, {b}):', floordiv(a, b)) print(f'mod({a},{b}): ', mod(a, b)) print(f'mul({a},{b}): ', mul(a, b)) print(f'pow({c},{d}):', pow(c, d)) print(f'sub({b},{a}):', sub(b, a)) print(f'truediv({a},{b}):', truediv(a, b)) print(f'truediv({d},{c}):', truediv(d, c)) print('\nBitwise:') print(f'and_({c}, {d}) :', and_(c, d)) print(f'invert({c}) :', invert(c))
def __pos__(self): return self.__class__(operator.pos(self._magnitude), self._units)
assert repeat(3, foo)() == ["foo", "foo", "foo"] assert repeat(5, foo)() == ["foo", "foo", "foo", "foo", "foo"] # since functions are values, we can use them anywhere we would use # a value # e.g., as elements in a dictionary assert {0: foo, 1: bar}[0]() == "foo" assert {0: foo, 1: bar}[1]() == "bar" # pos and neg represent +/- unary operators from operator import pos, neg assert pos(10) == 10 assert pos(-10) == -10 assert neg(10) == -10 assert neg(-10) == 10 assert abs(10) == abs(-10) == 10 # use them as values in a dictionary assert {"pos": pos, "neg": neg, "abs": abs}["pos"](-100) == -100 assert {"pos": pos, "neg": neg, "abs": abs}["neg"](-100) == 100 # use them as keys in a dictionary assert {pos: "pos", neg: "neg"}[pos] == "pos" assert {pos: "pos", neg: "neg"}[neg] == "neg"
def __pos__(self): return vec2d(operator.pos(self.x), operator.pos(self.y))
print("x.__or__(y) : ", (10).__or__(4)) print(" bit xor ", oct(10), oct(6)) print(" ^ : ", 10 ^ 6) print(" xor(a, b) : ", op.xor(10,6)) print("x.__xor__(y) : ", (10).__xor__(6)) print(" bit inversion ", oct(10)) print(" ~ : ", ~(10) ) print(" invert(a) : ", op.invert(10)) print("x.__invert__(y) : ", (10).__invert__()) print(" negative : ", -(10)) print(" neg(a) : ", op.neg((10))) print("a.__neg__ : ", (10).__neg__()) print(" positive : ", -(-10), +(10)) print(" pos(a) : ", op.pos((10))) print("a.__pos__ : ", (10).__pos__()) print(" right hand operator ") print(" x + y ", (8).__radd__(2)) print(" x + y ", (2).__add__(8)) print(" x ** y ", (3).__rpow__(2)) print(" x ** y ", (2).__pow__(3)) x = True y = False print('x and y is',x and y) print('x or y is',x or y) print('not x is',not x)
def test_pos(a: int): assert op.pos(a) == operator.pos(a)
def __pos__(self): return operator.pos(self.value) def __pow__(self, other): return operator.pow(self.value, other)
import operator
def __pos__(self): return Vec2d(operator.pos(self[0]), operator.pos(self[1]))
def test_vec2d_magic_unary(): import operator assert tuple(operator.neg(cy.Vec2d(-1,1))) == pytest.approx((1,-1)) assert tuple(operator.pos(cy.Vec2d(-1,1))) == pytest.approx((-1,1)) assert tuple(operator.abs(cy.Vec2d(-1,1))) == pytest.approx((1,1))
def __pos__(self): return Vector(operator.pos(self.x), operator.pos(self.y), operator.pos(self.z))
def test_pos(): a = PhysicalQuantity(4, 'm') assert operator.pos(a) == a
def __pos__(self): """Return each elements from the vectors with no changes""" return Vector2D(operator.pos(self.x), operator.pos(self.y))
def op_pos(x): return _op.pos(x)
def __pos__(self): return Quaternion([operator.pos(c) for c in self]) # concatenation/transformation operators def __mul__(self, other):
def __pos__(self): return Coordinates(operator.pos(self.x), operator.pos(self.y), operator.pos(self.z))
#但是不改变自身的值,返回值返回相除的结果 a = 9 b = operator.idiv(a,4) print a print b #将与自身相除取整的值赋给自身 同 //= #但是不改变自身的值,返回值返回相除的结果 a = 9.04 b = operator.ifloordiv(a,4) print a print b #将与自身相移位的值赋给自身 同 <<= #但是不改变自身的值,返回值返回移位的结果 a = 3 b = operator.ilshift(3,2) print a print b #将与自身相移位的值赋给自身 同 >>= #但是不改变自身的值,返回值返回移位的结果 a = 3 b = operator.irshift(3,2) print a print b #同 +a #并不知道有什么用 operator.pos(3)
def operator_pos(size): a = Array(size, 'int32') for i in range(size): a[i] = nb_types.int32(-i) return operator.pos(a)
def __pos__(self): return vec(operator.pos(self.x), operator.pos(self.y))
def __pos__(self): return Vec2D(operator.pos(self.x), operator.pos(self.y))
def __pos__(self): return Vec2d(operator.pos(self.x), operator.pos(self.y))
addConvenienceForClass('NSDecimalNumber', ( ('__new__', staticmethod(decimal_new)), ('__add__', lambda self, other: NSDecimalNumber(operator.add(NSDecimal(self), other))), ('__radd__', lambda self, other: NSDecimalNumber(operator.add(other, NSDecimal(self)))), ('__sub__', lambda self, other: NSDecimalNumber(operator.sub(NSDecimal(self), other))), ('__rsub__', lambda self, other: NSDecimalNumber(operator.sub(other, NSDecimal(self)))), ('__mul__', lambda self, other: NSDecimalNumber(operator.mul(NSDecimal(self), other))), ('__rmul__', lambda self, other: NSDecimalNumber(operator.mul(other, NSDecimal(self)))), ('__truediv__', lambda self, other: NSDecimalNumber(operator.truediv(NSDecimal(self), other))), ('__rtruediv__', lambda self, other: NSDecimalNumber(operator.truediv(other, NSDecimal(self)))), ('__floordiv__', lambda self, other: NSDecimalNumber(operator.floordiv(NSDecimal(self), other))), ('__rfloordiv__', lambda self, other: NSDecimalNumber(operator.floordiv(other, NSDecimal(self)))), ('__mod__', lambda self, other: NSDecimalNumber(operator.mod(NSDecimal(self), other))), ('__rmod__', lambda self, other: NSDecimalNumber(operator.mod(other, NSDecimal(self)))), ('__neg__', lambda self: NSDecimalNumber(operator.neg(NSDecimal(self)))), ('__pos__', lambda self: NSDecimalNumber(operator.pos(NSDecimal(self)))), ('__abs__', lambda self: NSDecimalNumber(abs(NSDecimal(self)))), ('__lt__', lambda self, other: (NSDecimal(self) < other)), ('__gt__', lambda self, other: (NSDecimal(self) > other)), ('__le__', lambda self, other: (NSDecimal(self) <= other)), ('__ge__', lambda self, other: (NSDecimal(self) >= other)), ('__eq__', lambda self, other: (NSDecimal(self) == other)), ('__ne__', lambda self, other: (NSDecimal(self) != other)), )) if sys.version_info[0] == 2: # pragma: no 3.x cover addConvenienceForClass('NSDecimalNumber', ( ('__div__', lambda self, other: NSDecimalNumber(operator.div(NSDecimal(self), other))), ('__rdiv__', lambda self, other: NSDecimalNumber(operator.div(other, NSDecimal(self)))), ('__cmp__', lambda self, other: cmp(NSDecimalNumber(NSDecimal(self), other))), ))