Python operator.div() Examples

The following are 30 code examples of operator.div(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module operator , or try the search function .
Example #1
Source File: base.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _add_numeric_methods_binary(cls):
        """
        Add in numeric methods.
        """
        cls.__add__ = _make_arithmetic_op(operator.add, cls)
        cls.__radd__ = _make_arithmetic_op(ops.radd, cls)
        cls.__sub__ = _make_arithmetic_op(operator.sub, cls)
        cls.__rsub__ = _make_arithmetic_op(ops.rsub, cls)
        cls.__rpow__ = _make_arithmetic_op(ops.rpow, cls)
        cls.__pow__ = _make_arithmetic_op(operator.pow, cls)

        cls.__truediv__ = _make_arithmetic_op(operator.truediv, cls)
        cls.__rtruediv__ = _make_arithmetic_op(ops.rtruediv, cls)
        if not compat.PY3:
            cls.__div__ = _make_arithmetic_op(operator.div, cls)
            cls.__rdiv__ = _make_arithmetic_op(ops.rdiv, cls)

        # TODO: rmod? rdivmod?
        cls.__mod__ = _make_arithmetic_op(operator.mod, cls)
        cls.__floordiv__ = _make_arithmetic_op(operator.floordiv, cls)
        cls.__rfloordiv__ = _make_arithmetic_op(ops.rfloordiv, cls)
        cls.__divmod__ = _make_arithmetic_op(divmod, cls)
        cls.__mul__ = _make_arithmetic_op(operator.mul, cls)
        cls.__rmul__ = _make_arithmetic_op(ops.rmul, cls) 
Example #2
Source File: base.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _add_arithmetic_ops(cls):
        cls.__add__ = cls._create_arithmetic_method(operator.add)
        cls.__radd__ = cls._create_arithmetic_method(ops.radd)
        cls.__sub__ = cls._create_arithmetic_method(operator.sub)
        cls.__rsub__ = cls._create_arithmetic_method(ops.rsub)
        cls.__mul__ = cls._create_arithmetic_method(operator.mul)
        cls.__rmul__ = cls._create_arithmetic_method(ops.rmul)
        cls.__pow__ = cls._create_arithmetic_method(operator.pow)
        cls.__rpow__ = cls._create_arithmetic_method(ops.rpow)
        cls.__mod__ = cls._create_arithmetic_method(operator.mod)
        cls.__rmod__ = cls._create_arithmetic_method(ops.rmod)
        cls.__floordiv__ = cls._create_arithmetic_method(operator.floordiv)
        cls.__rfloordiv__ = cls._create_arithmetic_method(ops.rfloordiv)
        cls.__truediv__ = cls._create_arithmetic_method(operator.truediv)
        cls.__rtruediv__ = cls._create_arithmetic_method(ops.rtruediv)
        if not PY3:
            cls.__div__ = cls._create_arithmetic_method(operator.div)
            cls.__rdiv__ = cls._create_arithmetic_method(ops.rdiv)

        cls.__divmod__ = cls._create_arithmetic_method(divmod)
        cls.__rdivmod__ = cls._create_arithmetic_method(ops.rdivmod) 
Example #3
Source File: base.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _add_numeric_methods_binary(cls):
        """ add in numeric methods """
        cls.__add__ = _make_arithmetic_op(operator.add, cls)
        cls.__radd__ = _make_arithmetic_op(ops.radd, cls)
        cls.__sub__ = _make_arithmetic_op(operator.sub, cls)
        cls.__rsub__ = _make_arithmetic_op(ops.rsub, cls)
        cls.__mul__ = _make_arithmetic_op(operator.mul, cls)
        cls.__rmul__ = _make_arithmetic_op(ops.rmul, cls)
        cls.__rpow__ = _make_arithmetic_op(ops.rpow, cls)
        cls.__pow__ = _make_arithmetic_op(operator.pow, cls)
        cls.__mod__ = _make_arithmetic_op(operator.mod, cls)
        cls.__floordiv__ = _make_arithmetic_op(operator.floordiv, cls)
        cls.__rfloordiv__ = _make_arithmetic_op(ops.rfloordiv, cls)
        cls.__truediv__ = _make_arithmetic_op(operator.truediv, cls)
        cls.__rtruediv__ = _make_arithmetic_op(ops.rtruediv, cls)
        if not compat.PY3:
            cls.__div__ = _make_arithmetic_op(operator.div, cls)
            cls.__rdiv__ = _make_arithmetic_op(ops.rdiv, cls)

        cls.__divmod__ = _make_arithmetic_op(divmod, cls) 
Example #4
Source File: test_operators.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_op_method(self, opname, ts):
        # check that Series.{opname} behaves like Series.__{opname}__,
        series = ts[0](self.ts)
        other = ts[1](self.ts)
        check_reverse = ts[2]

        if opname == 'div' and compat.PY3:
            pytest.skip('div test only for Py3')

        op = getattr(Series, opname)

        if op == 'div':
            alt = operator.truediv
        else:
            alt = getattr(operator, opname)

        result = op(series, other)
        expected = alt(series, other)
        assert_almost_equal(result, expected)
        if check_reverse:
            rop = getattr(Series, "r" + opname)
            result = rop(series, other)
            expected = alt(other, series)
            assert_almost_equal(result, expected) 
Example #5
Source File: test_operator.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_div(self):
        self.assertRaises(TypeError, operator.div, 5)
        self.assertRaises(TypeError, operator.div, None, None)
        self.assertTrue(operator.floordiv(5, 2) == 2) 
Example #6
Source File: UserFloat.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def __div__(self, other):
        return self._op(other, operator.div) 
Example #7
Source File: fractions.py    From Computable with MIT License 5 votes vote down vote up
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) 
Example #8
Source File: fractions.py    From Computable with MIT License 5 votes vote down vote up
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) 
Example #9
Source File: test_deprecations.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_int_dtypes(self):
        #scramble types and do some mix and match testing
        deprecated_types = [
           'bool_', 'int_', 'intc', 'uint8', 'int8', 'uint64', 'int32', 'uint16',
           'intp', 'int64', 'uint32', 'int16'
            ]
        if sys.version_info[0] < 3 and sys.py3kwarning:
            import operator as op
            dt2 = 'bool_'
            for dt1 in deprecated_types:
                a = np.array([1,2,3], dtype=dt1)
                b = np.array([1,2,3], dtype=dt2)
                self.assert_deprecated(op.div, args=(a,b))
                dt2 = dt1 
Example #10
Source File: UserFloat.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def __rdiv__(self, other):
        return self._rop(other, operator.div) 
Example #11
Source File: UserInt.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def __div__(self, other):
        return self._op(other, operator.div) 
Example #12
Source File: UserInt.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def __rdiv__(self, other):
        return self._rop(other, operator.div) 
Example #13
Source File: UserLong.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def __div__(self, other):
        return self._op(other, operator.div) 
Example #14
Source File: vec2d.py    From omnitool with MIT License 5 votes vote down vote up
def __rdiv__(self, other):
        return self._r_o2(other, operator.div) 
Example #15
Source File: adts.py    From MoAL with Apache License 2.0 5 votes vote down vote up
def div(self, divisor):
        self.do(operator.div, val=divisor)
        return self 
Example #16
Source File: vec2d.py    From omnitool with MIT License 5 votes vote down vote up
def __idiv__(self, other):
        return self._io(other, operator.div) 
Example #17
Source File: fractions.py    From Computable with MIT License 5 votes vote down vote up
def __rmod__(b, a):
        """a % b"""
        div = a // b
        return a - b * div 
Example #18
Source File: fractions.py    From BinderFilter with MIT License 5 votes vote down vote up
def __rmod__(b, a):
        """a % b"""
        div = a // b
        return a - b * div 
Example #19
Source File: fractions.py    From BinderFilter with MIT License 5 votes vote down vote up
def __mod__(a, b):
        """a % b"""
        div = a // b
        return a - b * div 
Example #20
Source File: fractions.py    From BinderFilter with MIT License 5 votes vote down vote up
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) 
Example #21
Source File: fractions.py    From BinderFilter with MIT License 5 votes vote down vote up
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) 
Example #22
Source File: calculator.py    From 15-minute-apps with MIT License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.setupUi(self)

        # Setup numbers.
        for n in range(0, 10):
            getattr(self, 'pushButton_n%s' % n).pressed.connect(lambda v=n: self.input_number(v))

        # Setup operations.
        self.pushButton_add.pressed.connect(lambda: self.operation(operator.add))
        self.pushButton_sub.pressed.connect(lambda: self.operation(operator.sub))
        self.pushButton_mul.pressed.connect(lambda: self.operation(operator.mul))
        self.pushButton_div.pressed.connect(lambda: self.operation(operator.truediv))  # operator.div for Python2.7

        self.pushButton_pc.pressed.connect(self.operation_pc)
        self.pushButton_eq.pressed.connect(self.equals)

        # Setup actions
        self.actionReset.triggered.connect(self.reset)
        self.pushButton_ac.pressed.connect(self.reset)

        self.actionExit.triggered.connect(self.close)

        self.pushButton_m.pressed.connect(self.memory_store)
        self.pushButton_mr.pressed.connect(self.memory_recall)

        self.memory = 0
        self.reset()

        self.show() 
Example #23
Source File: missing.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def dispatch_missing(op, left, right, result):
    """
    Fill nulls caused by division by zero, casting to a diffferent dtype
    if necessary.

    Parameters
    ----------
    op : function (operator.add, operator.div, ...)
    left : object (Index for non-reversed ops)
    right : object (Index fof reversed ops)
    result : ndarray

    Returns
    -------
    result : ndarray
    """
    opstr = '__{opname}__'.format(opname=op.__name__).replace('____', '__')
    if op in [operator.truediv, operator.floordiv,
              getattr(operator, 'div', None)]:
        result = mask_zero_div_zero(left, right, result)
    elif op is operator.mod:
        result = fill_zeros(result, left, right, opstr, np.nan)
    elif op is divmod:
        res0 = mask_zero_div_zero(left, right, result[0])
        res1 = fill_zeros(result[1], left, right, opstr, np.nan)
        result = (res0, res1)
    return result 
Example #24
Source File: test_deprecations.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_int_dtypes(self):
        #scramble types and do some mix and match testing
        deprecated_types = [
           'bool_', 'int_', 'intc', 'uint8', 'int8', 'uint64', 'int32', 'uint16',
           'intp', 'int64', 'uint32', 'int16'
            ]
        if sys.version_info[0] < 3 and sys.py3kwarning:
            import operator as op
            dt2 = 'bool_'
            for dt1 in deprecated_types:
                a = np.array([1,2,3], dtype=dt1)
                b = np.array([1,2,3], dtype=dt2)
                self.assert_deprecated(op.div, args=(a,b))
                dt2 = dt1 
Example #25
Source File: test_operator.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_div(self):
        self.assertRaises(TypeError, operator.div, 5)
        self.assertRaises(TypeError, operator.div, None, None)
        self.assertTrue(operator.floordiv(5, 2) == 2) 
Example #26
Source File: fractions.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def __rmod__(b, a):
        """a % b"""
        div = a // b
        return a - b * div 
Example #27
Source File: fractions.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def __mod__(a, b):
        """a % b"""
        div = a // b
        return a - b * div 
Example #28
Source File: fractions.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
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) 
Example #29
Source File: fractions.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
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) 
Example #30
Source File: wrappers.py    From gist-alfred with MIT License 5 votes vote down vote up
def __div__(self, other):
        return operator.div(self.__wrapped__, other)