我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用operator.div()。
def op_to_opstr(op): if op is operator.le: return "<=" elif op is operator.ge: return ">=" elif op is operator.eq: return "=" elif op is operator.add: return "+" elif op is operator.sub: return "-" elif op is operator.mul: return "*" elif op is operator.div: return "/" else: raise Exception("Operator '{}' not supported yet.", op)
def __floordiv__(a, b): """a // b""" # Will be math.floor(a / b) in 3.0. div = a / b if isinstance(div, Rational): # trunc(math.floor(div)) doesn't work if the rational is # more precise than a float because the intermediate # rounding may cross an integer boundary. return div.numerator // div.denominator else: return math.floor(div)
def __rfloordiv__(b, a): """a // b""" # Will be math.floor(a / b) in 3.0. div = a / b if isinstance(div, Rational): # trunc(math.floor(div)) doesn't work if the rational is # more precise than a float because the intermediate # rounding may cross an integer boundary. return div.numerator // div.denominator else: return math.floor(div)
def __mod__(a, b): """a % b""" div = a // b return a - b * div
def __rmod__(b, a): """a % b""" div = a // b return a - b * div
def __truediv__(self, other): return Vector(self._scalarOp(other, operator.div), keep=True)
def __itruediv__(self, other): self.values = self._scalarOp(other, operator.div) return self
def __rdiv__(self, other): """Implement the ``/`` operator in reverse. See :meth:`.ColumnOperators.__div__`. """ return self.reverse_operate(div, other)
def __div__(self, other): """Implement the ``/`` operator. In a column context, produces the clause ``a / b``. """ return self.operate(div, other)
def division(self, *args): if not len(args) > 1: raise EvaluateException('\ requires at least 2 parameters!' + ' (' + str(len(args)) + ' given).') elif False in [isinstance(x, NumberType) for x in args]: raise EvaluateException('\ requires all parameters to be numbers!') elif 0 in [x.content for x in args[1:]]: raise EvaluateException('division by zero!') return reduce(op.div, args[1:], args[0])
def __div__(self, other): fieldop = FieldOp(operator.div, self, other) return R(fieldop)
def test_div_scalar(self): if six.PY3: return with testing.NumpyError(divide='ignore'): self.check_array_scalar_op(operator.div)
def test_rdiv_scalar(self): if six.PY3: return with testing.NumpyError(divide='ignore'): self.check_array_scalar_op(operator.div, swap=True)
def test_div_array(self): if six.PY3: return with testing.NumpyError(divide='ignore'): self.check_array_array_op(operator.div)
def test_broadcasted_div(self): if six.PY3: return with testing.NumpyError(divide='ignore'): self.check_array_broadcasted_op(operator.div)
def test_doubly_broadcasted_div(self): if six.PY3: return with testing.NumpyError(divide='ignore'): self.check_array_doubly_broadcasted_op(operator.div)
def __div__(self, other): return operator.div(self.__wrapped__, other)
def __rdiv__(self, other): return operator.div(other, self.__wrapped__)
def __div__(self, y): return NonStandardInteger(operator.div(self.val, y))
def __rdiv__(self, y): return NonStandardInteger(operator.div(y, self.val))
def test_registry_association(): ds = fake_random_ds(64, nprocs=1, length_unit=10) a = ds.quan(3, 'cm') b = YTQuantity(4, 'm') c = ds.quan(6, '') d = 5 assert_equal(id(a.units.registry), id(ds.unit_registry)) def binary_op_registry_comparison(op): e = op(a, b) f = op(b, a) g = op(c, d) h = op(d, c) assert_equal(id(e.units.registry), id(ds.unit_registry)) assert_equal(id(f.units.registry), id(b.units.registry)) assert_equal(id(g.units.registry), id(h.units.registry)) assert_equal(id(g.units.registry), id(ds.unit_registry)) def unary_op_registry_comparison(op): c = op(a) d = op(b) assert_equal(id(c.units.registry), id(ds.unit_registry)) assert_equal(id(d.units.registry), id(b.units.registry)) binary_ops = [operator.add, operator.sub, operator.mul, operator.truediv] if hasattr(operator, "div"): binary_ops.append(operator.div) for op in binary_ops: binary_op_registry_comparison(op) for op in [operator.abs, operator.neg, operator.pos]: unary_op_registry_comparison(op)
def test_subclass(): class YTASubclass(YTArray): pass a = YTASubclass([4, 5, 6], 'g') b = YTASubclass([7, 8, 9], 'kg') nu = YTASubclass([10, 11, 12], '') nda = np.array([3, 4, 5]) yta = YTArray([6, 7, 8], 'mg') loq = [YTQuantity(6, 'mg'), YTQuantity(7, 'mg'), YTQuantity(8, 'mg')] ytq = YTQuantity(4, 'cm') ndf = np.float64(3) def op_comparison(op, inst1, inst2, compare_class): assert_isinstance(op(inst1, inst2), compare_class) assert_isinstance(op(inst2, inst1), compare_class) ops = [operator.mul, operator.truediv] if hasattr(operator, "div"): ops.append(operator.div) for op in ops: for inst in (b, ytq, ndf, yta, nda, loq): op_comparison(op, a, inst, YTASubclass) op_comparison(op, ytq, nda, YTArray) op_comparison(op, ytq, yta, YTArray) for op in (operator.add, operator.sub): op_comparison(op, nu, nda, YTASubclass) op_comparison(op, a, b, YTASubclass) op_comparison(op, a, yta, YTASubclass) op_comparison(op, a, loq, YTASubclass) assert_isinstance(a[0], YTQuantity) assert_isinstance(a[:], YTASubclass) assert_isinstance(a[:2], YTASubclass) assert_isinstance(YTASubclass(yta), YTASubclass)
def __div__(self, other): assert type(other) in scalar_types return Vector2(operator.div(self.x, other), operator.div(self.y, other))
def __rdiv__(self, other): assert type(other) in scalar_types return Vector2(operator.div(other, self.x), operator.div(other, self.y))
def __mul__(self, other): if isinstance(other, Vector3): # TODO component-wise mul/div in-place and on Vector2; docs. if self.__class__ is Point3 or other.__class__ is Point3: _class = Point3 else: _class = Vector3 return _class(self.x * other.x, self.y * other.y, self.z * other.z) else: assert type(other) in scalar_types return Vector3(self.x * other, self.y * other, self.z * other)
def __div__(self, other): assert type(other) in scalar_types return Vector3(operator.div(self.x, other), operator.div(self.y, other), operator.div(self.z, other))
def __rdiv__(self, other): assert type(other) in scalar_types return Vector3(operator.div(other, self.x), operator.div(other, self.y), operator.div(other, self.z))
def test_div(self): self.assertRaises(TypeError, operator.div, 5) self.assertRaises(TypeError, operator.div, None, None) self.assertTrue(operator.floordiv(5, 2) == 2)