def _imul_div(self, other, magnitude_op, units_op=None):
"""Perform multiplication or division operation in-place and return the
result.
:param other: object to be multiplied/divided with self
:type other: Quantity or any type accepted by :func:`_to_magnitude`
:param magnitude_op: operator function to perform on the magnitudes
(e.g. operator.mul)
:type magnitude_op: function
:param units_op: operator function to perform on the units; if None,
*magnitude_op* is used
:type units_op: function or None
"""
if units_op is None:
units_op = magnitude_op
offset_units_self = self._get_non_multiplicative_units()
no_offset_units_self = len(offset_units_self)
if not self._check(other):
if not self._ok_for_muldiv(no_offset_units_self):
raise OffsetUnitCalculusError(self._units,
getattr(other, 'units', ''))
if len(offset_units_self) == 1:
if (self._units[offset_units_self[0]] != 1
or magnitude_op not in [operator.mul, operator.imul]):
raise OffsetUnitCalculusError(self._units,
getattr(other, 'units', ''))
try:
other_magnitude = _to_magnitude(other, self.force_ndarray)
except TypeError:
return NotImplemented
self._magnitude = magnitude_op(self._magnitude, other_magnitude)
self._units = units_op(self._units, UnitsContainer())
return self
if isinstance(other, self._REGISTRY.Unit):
other = 1.0 * other
if not self._ok_for_muldiv(no_offset_units_self):
raise OffsetUnitCalculusError(self._units, other._units)
elif no_offset_units_self == 1 and len(self._units) == 1:
self.ito_root_units()
no_offset_units_other = len(other._get_non_multiplicative_units())
if not other._ok_for_muldiv(no_offset_units_other):
raise OffsetUnitCalculusError(self._units, other._units)
elif no_offset_units_other == 1 and len(other._units) == 1:
other.ito_root_units()
self._magnitude = magnitude_op(self._magnitude, other._magnitude)
self._units = units_op(self._units, other._units)
return self
评论列表
文章目录