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)
python类div()的实例源码
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 __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 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 __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, 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 __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, other):
return operator.div(self.__wrapped__, other)
def __rdiv__(self, other):
return operator.div(other, self.__wrapped__)
def __rdiv__(self, other):
return operator.div(other, self.__wrapped__)
def __div__(self, other):
return operator.div(self.__wrapped__, other)
def __rdiv__(self, other):
return operator.div(other, self.__wrapped__)
def __rdiv__(self, other):
"""Implement the ``/`` operator in reverse.
See :meth:`.ColumnOperators.__div__`.
"""
return self.reverse_operate(div, other)