def __div__(self, other):
"""Implement the ``/`` operator.
In a column context, produces the clause ``a / b``.
"""
return self.operate(div, other)
python类div()的实例源码
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 __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 __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 __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 test_div(self):
self.assertRaises(TypeError, operator.div, 5)
self.assertRaises(TypeError, operator.div, None, None)
self.assertTrue(operator.floordiv(5, 2) == 2)
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