Python operator.rshift() Examples

The following are 30 code examples of operator.rshift(). 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: mlab.py    From neural-network-animation with MIT License 14 votes vote down vote up
def binary_repr(number, max_length = 1025):
    """
    Return the binary representation of the input *number* as a
    string.

    This is more efficient than using :func:`base_repr` with base 2.

    Increase the value of max_length for very large numbers. Note that
    on 32-bit machines, 2**1023 is the largest integer power of 2
    which can be converted to a Python float.
    """

    #assert number < 2L << max_length
    shifts = list(map (operator.rshift, max_length * [number], \
                  range (max_length - 1, -1, -1)))
    digits = list(map (operator.mod, shifts, max_length * [2]))
    if not digits.count (1): return 0
    digits = digits [digits.index (1):]
    return ''.join (map (repr, digits)).replace('L','') 
Example #2
Source File: mlab.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def binary_repr(number, max_length=1025):
    """
    Return the binary representation of the input *number* as a
    string.

    This is more efficient than using :func:`base_repr` with base 2.

    Increase the value of max_length for very large numbers. Note that
    on 32-bit machines, 2**1023 is the largest integer power of 2
    which can be converted to a Python float.
    """

#   assert number < 2L << max_length
    shifts = map(operator.rshift, max_length * [number],
                 range(max_length - 1, -1, -1))
    digits = list(map(operator.mod, shifts, max_length * [2]))
    if not digits.count(1):
        return 0
    digits = digits[digits.index(1):]
    return ''.join(map(repr, digits)).replace('L', '') 
Example #3
Source File: mlab.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def binary_repr(number, max_length=1025):
    """
    Return the binary representation of the input *number* as a
    string.

    This is more efficient than using :func:`base_repr` with base 2.

    Increase the value of max_length for very large numbers. Note that
    on 32-bit machines, 2**1023 is the largest integer power of 2
    which can be converted to a Python float.
    """

#   assert number < 2L << max_length
    shifts = map(operator.rshift, max_length * [number],
                 range(max_length - 1, -1, -1))
    digits = list(map(operator.mod, shifts, max_length * [2]))
    if not digits.count(1):
        return 0
    digits = digits[digits.index(1):]
    return ''.join(map(repr, digits)).replace('L', '') 
Example #4
Source File: test_sectypes.py    From mpyc with MIT License 6 votes vote down vote up
def test_operatorerrors(self):
        secfld = sectypes.SecFld()
        secint = sectypes.SecInt()
        a = secfld(0)
        b = secint(1)
        self.assertRaises(TypeError, operator.add, a, b)
        self.assertRaises(TypeError, operator.add, a, 3.14)
        self.assertRaises(TypeError, operator.sub, a, b)
        self.assertRaises(TypeError, operator.mul, a, b)
        self.assertRaises(TypeError, operator.mul, 3.14, b)
        self.assertRaises(TypeError, operator.truediv, a, b)
        self.assertRaises(TypeError, operator.truediv, a, b)
        self.assertRaises(TypeError, operator.mod, a, b)
        self.assertRaises(TypeError, operator.mod, b, a)
        self.assertRaises(TypeError, operator.floordiv, a, b)
        self.assertRaises(TypeError, divmod, a, b)
        self.assertRaises(TypeError, divmod, b, a)
        self.assertRaises(TypeError, operator.pow, b, 3.14)
        self.assertRaises(TypeError, operator.lshift, b, 3.14)
        self.assertRaises(TypeError, operator.lshift, 3.14, b)
        self.assertRaises(TypeError, operator.rshift, b, 3.14)
        self.assertRaises(TypeError, operator.rshift, 3.14, b) 
Example #5
Source File: mlab.py    From Computable with MIT License 6 votes vote down vote up
def binary_repr(number, max_length = 1025):
    """
    Return the binary representation of the input *number* as a
    string.

    This is more efficient than using :func:`base_repr` with base 2.

    Increase the value of max_length for very large numbers. Note that
    on 32-bit machines, 2**1023 is the largest integer power of 2
    which can be converted to a Python float.
    """

    #assert number < 2L << max_length
    shifts = map (operator.rshift, max_length * [number], \
                  range (max_length - 1, -1, -1))
    digits = map (operator.mod, shifts, max_length * [2])
    if not digits.count (1): return 0
    digits = digits [digits.index (1):]
    return ''.join (map (repr, digits)).replace('L','') 
Example #6
Source File: test_ndarray_elementwise_op.py    From cupy with MIT License 6 votes vote down vote up
def test_rshift_scalarzero(self):
        self.check_array_scalarzero_op(operator.rshift) 
Example #7
Source File: mlab.py    From matplotlib-4-abaqus with MIT License 6 votes vote down vote up
def binary_repr(number, max_length = 1025):
    """
    Return the binary representation of the input *number* as a
    string.

    This is more efficient than using :func:`base_repr` with base 2.

    Increase the value of max_length for very large numbers. Note that
    on 32-bit machines, 2**1023 is the largest integer power of 2
    which can be converted to a Python float.
    """

    #assert number < 2L << max_length
    shifts = map (operator.rshift, max_length * [number], \
                  range (max_length - 1, -1, -1))
    digits = map (operator.mod, shifts, max_length * [2])
    if not digits.count (1): return 0
    digits = digits [digits.index (1):]
    return ''.join (map (repr, digits)).replace('L','') 
Example #8
Source File: SPI.py    From Adafruit_Python_GPIO with MIT License 6 votes vote down vote up
def set_bit_order(self, order):
        """Set order of bits to be read/written over serial lines.  Should be
        either MSBFIRST for most-significant first, or LSBFIRST for
        least-signifcant first.
        """
        # Set self._mask to the bitmask which points at the appropriate bit to
        # read or write, and appropriate left/right shift operator function for
        # reading/writing.
        if order == MSBFIRST:
            self._mask = 0x80
            self._write_shift = operator.lshift
            self._read_shift = operator.rshift
        elif order == LSBFIRST:
            self._mask = 0x01
            self._write_shift = operator.rshift
            self._read_shift = operator.lshift
        else:
            raise ValueError('Order must be MSBFIRST or LSBFIRST.') 
Example #9
Source File: mlab.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def binary_repr(number, max_length=1025):
    """
    Return the binary representation of the input *number* as a
    string.

    This is more efficient than using :func:`base_repr` with base 2.

    Increase the value of max_length for very large numbers. Note that
    on 32-bit machines, 2**1023 is the largest integer power of 2
    which can be converted to a Python float.
    """

#   assert number < 2L << max_length
    shifts = map(operator.rshift, max_length * [number],
                 range(max_length - 1, -1, -1))
    digits = list(map(operator.mod, shifts, max_length * [2]))
    if not digits.count(1):
        return 0
    digits = digits[digits.index(1):]
    return ''.join(map(repr, digits)).replace('L', '') 
Example #10
Source File: mlab.py    From ImageFusion with MIT License 6 votes vote down vote up
def binary_repr(number, max_length = 1025):
    """
    Return the binary representation of the input *number* as a
    string.

    This is more efficient than using :func:`base_repr` with base 2.

    Increase the value of max_length for very large numbers. Note that
    on 32-bit machines, 2**1023 is the largest integer power of 2
    which can be converted to a Python float.
    """

    #assert number < 2L << max_length
    shifts = list(map (operator.rshift, max_length * [number], \
                  range (max_length - 1, -1, -1)))
    digits = list(map (operator.mod, shifts, max_length * [2]))
    if not digits.count (1): return 0
    digits = digits [digits.index (1):]
    return ''.join (map (repr, digits)).replace('L','') 
Example #11
Source File: test_finfields.py    From mpyc with MIT License 6 votes vote down vote up
def test_operatorerrors(self):
        f2 = self.f2
        f2p = self.f2p
        f256 = self.f256
        f19 = self.f19
        self.assertRaises(TypeError, operator.add, f2(1), f2p(2))
        self.assertRaises(TypeError, operator.iadd, f2(1), f2p(2))
        self.assertRaises(TypeError, operator.sub, f2(1), f256(2))
        self.assertRaises(TypeError, operator.isub, f2(1), f256(2))
        self.assertRaises(TypeError, operator.mul, f2(1), f19(2))
        self.assertRaises(TypeError, operator.imul, f2(1), f19(2))
        self.assertRaises(TypeError, operator.truediv, f256(1), f19(2))
        self.assertRaises(TypeError, operator.itruediv, f256(1), f19(2))
        self.assertRaises(TypeError, operator.truediv, 3.14, f19(2))
        self.assertRaises(TypeError, operator.lshift, f2(1), f2(1))
        self.assertRaises(TypeError, operator.ilshift, f2(1), f2(1))
        self.assertRaises(TypeError, operator.lshift, 1, f2(1))
        self.assertRaises(TypeError, operator.rshift, f19(1), f19(1))
        self.assertRaises(TypeError, operator.irshift, f19(1), f19(1))
        self.assertRaises(TypeError, operator.irshift, f256(1), f256(1))
        self.assertRaises(TypeError, operator.pow, f2(1), f19(2))
        self.assertRaises(TypeError, operator.pow, f19(1), 3.14) 
Example #12
Source File: expr.py    From owasp-pysec with Apache License 2.0 5 votes vote down vote up
def __rshift__(self, other):
        return Expression((self, other), operator.rshift) 
Example #13
Source File: test_operator.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_rshift(self):
        self.failUnlessRaises(TypeError, operator.rshift)
        self.failUnlessRaises(TypeError, operator.rshift, None, 42)
        self.failUnless(operator.rshift(5, 1) == 2)
        self.failUnless(operator.rshift(5, 0) == 5)
        self.assertRaises(ValueError, operator.rshift, 2, -1) 
Example #14
Source File: transform.py    From holoviews with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __rshift__(self, other):    return type(self)(self, operator.rshift, other) 
Example #15
Source File: test_operator.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_rshift(self):
        self.failUnlessRaises(TypeError, operator.rshift)
        self.failUnlessRaises(TypeError, operator.rshift, None, 42)
        self.failUnless(operator.rshift(5, 1) == 2)
        self.failUnless(operator.rshift(5, 0) == 5)
        self.assertRaises(ValueError, operator.rshift, 2, -1) 
Example #16
Source File: test_util.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def __rshift__(self, y):
    return NonStandardInteger(operator.rshift(self.val, y)) 
Example #17
Source File: TVector.py    From uproot-methods with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __rrshift__(self, other):
        return self._scalar(operator.rshift, other, True) 
Example #18
Source File: nodes.py    From hyper-engine with Apache License 2.0 5 votes vote down vote up
def __rshift__(self, other):  return _op2(self, other, operator.rshift) 
Example #19
Source File: nodes.py    From hyper-engine with Apache License 2.0 5 votes vote down vote up
def __rrshift__(self, other): return _op2(self, other, operator.rshift, rev=True) 
Example #20
Source File: vec2d.py    From code-for-blog with The Unlicense 5 votes vote down vote up
def __rshift__(self, other):
        return self._o2(other, operator.rshift) 
Example #21
Source File: test_number.py    From hpy with MIT License 5 votes vote down vote up
def test_binary(self):
        import operator
        for c_name, op in [
                ('Add', operator.add),
                ('Subtract', operator.sub),
                ('Multiply', operator.mul),
                ('FloorDivide', operator.floordiv),
                ('TrueDivide', operator.truediv),
                ('Remainder', operator.mod),
                ('Divmod', divmod),
                ('Lshift', operator.lshift),
                ('Rshift', operator.rshift),
                ('And', operator.and_),
                ('Xor', operator.xor),
                ('Or', operator.or_),
                ]:
            mod = self.make_module("""
                HPyDef_METH(f, "f", f_impl, HPyFunc_VARARGS)
                static HPy f_impl(HPyContext ctx, HPy self,
                                  HPy *args, HPy_ssize_t nargs)
                {
                    HPy a, b;
                    if (!HPyArg_Parse(ctx, args, nargs, "OO", &a, &b))
                        return HPy_NULL;
                    return HPy_%s(ctx, a, b);
                }
                @EXPORT(f)
                @INIT
            """ % (c_name,), name='number_'+c_name)
            assert mod.f(5, 4) == op(5, 4)
            assert mod.f(6, 3) == op(6, 3) 
Example #22
Source File: ops.py    From hwt with MIT License 5 votes vote down vote up
def __rshift__(self, other):
        try:
            return self.naryOp(rshift, tv(self).__rshift__, other)
        except Exception as e:
            # simplification of previous exception traceback
            e_simplified = copy(e)
            raise e_simplified

    # cmp 
Example #23
Source File: test_gfpx.py    From mpyc with MIT License 5 votes vote down vote up
def _test_errors(self, poly):
        self.assertRaises(ValueError, poly.from_terms, 'x**2')
        self.assertRaises(TypeError, poly, 0.1)
        self.assertRaises(TypeError, poly, gfpx.GFpX(257)(0))
        self.assertRaises(ValueError, poly, [poly.p])
        self.assertRaises(TypeError, operator.add, poly(0), 0.1)
        self.assertRaises(TypeError, operator.iadd, poly(0), 0.1)
        self.assertRaises(TypeError, operator.sub, poly(0), 0.1)
        self.assertRaises(TypeError, operator.sub, 0.1, poly(0))
        self.assertRaises(TypeError, operator.isub, poly(0), 0.1)
        self.assertRaises(TypeError, operator.mul, poly(0), 0.1)
        self.assertRaises(TypeError, operator.imul, poly(0), 0.1)
        self.assertRaises(TypeError, operator.lshift, poly(0), 0.1)
        self.assertRaises(TypeError, operator.lshift, 0.1, poly(0))
        self.assertRaises(TypeError, operator.ilshift, poly(0), 0.1)
        self.assertRaises(TypeError, operator.rshift, poly(0), 0.1)
        self.assertRaises(TypeError, operator.rshift, 0.1, poly(0))
        self.assertRaises(TypeError, operator.irshift, poly(0), 0.1)
        self.assertRaises(TypeError, operator.floordiv, poly(0), 0.1)
        self.assertRaises(TypeError, operator.floordiv, 0.1, poly(0))
        self.assertRaises(TypeError, operator.ifloordiv, poly(0), 0.1)
        self.assertRaises(TypeError, operator.mod, poly(0), 0.1)
        self.assertRaises(TypeError, operator.mod, 0.1, poly(0))
        self.assertRaises(TypeError, operator.imod, poly(0), 0.1)
        self.assertRaises(TypeError, divmod, poly(0), 0.1)
        self.assertRaises(TypeError, divmod, 0.1, poly(0))
        self.assertRaises(TypeError, operator.lt, poly(0), 0.1)
        self.assertRaises(TypeError, operator.lt, 0.1, poly(0))  # NB: tests >
        self.assertRaises(TypeError, operator.le, poly(0), 0.1)
        self.assertRaises(TypeError, operator.le, 0.1, poly(0))  # NB: tests <
        self.assertRaises(ZeroDivisionError, poly.invert, poly(283), poly(0))
        self.assertRaises(ZeroDivisionError, poly.invert, poly(283), poly(283))
        self.assertRaises(ZeroDivisionError, poly.mod, poly(283), poly(0))
        self.assertRaises(ZeroDivisionError, poly.divmod, poly(283), poly(0))
        self.assertRaises(ValueError, operator.pow, poly(3), -16) 
Example #24
Source File: test_operator.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_rshift(self):
        self.assertRaises(TypeError, operator.rshift)
        self.assertRaises(TypeError, operator.rshift, None, 42)
        self.assertTrue(operator.rshift(5, 1) == 2)
        self.assertTrue(operator.rshift(5, 0) == 5)
        self.assertRaises(ValueError, operator.rshift, 2, -1) 
Example #25
Source File: test_util.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __rshift__(self, y):
    return NonStandardInteger(operator.rshift(self.val, y)) 
Example #26
Source File: test_util.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __rrshift__(self, y):
    return NonStandardInteger(operator.rshift(y, self.val)) 
Example #27
Source File: test_util.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __rshift__(self, y):
    return NonStandardInteger(operator.rshift(self.val, y)) 
Example #28
Source File: test_util.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __rrshift__(self, y):
    return NonStandardInteger(operator.rshift(y, self.val)) 
Example #29
Source File: test_util.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __rshift__(self, y):
    return NonStandardInteger(operator.rshift(self.val, y)) 
Example #30
Source File: test_util.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def __rshift__(self, y):
    return NonStandardInteger(operator.rshift(self.val, y))