当前位置: 首页>>代码示例>>Python>>正文


Python BitArray.__new__方法代码示例

本文整理汇总了Python中bitstring.BitArray.__new__方法的典型用法代码示例。如果您正苦于以下问题:Python BitArray.__new__方法的具体用法?Python BitArray.__new__怎么用?Python BitArray.__new__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bitstring.BitArray的用法示例。


在下文中一共展示了BitArray.__new__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __new__

# 需要导入模块: from bitstring import BitArray [as 别名]
# 或者: from bitstring.BitArray import __new__ [as 别名]
    def __new__(class_type, input_data=None, length=None, signed=False):

        if input_data is None:
            return BitArray.__new__(class_type)

        if not isinstance(input_data, (int, str, class_type)):

            raise TypeError(
                "Input must be given as integer or binary strings or BinPyBits object. The given type was : " +
                str(type(input_data)))

        if isinstance(input_data, int):
            if input_data < 0 or signed:
                if length is None:
                    length = int(floor(log(abs(input_data), 2)) + 1)

                input_int = input_data
                signed = True
                # Sign is taken care by the sign of input_data
            else:
                input_int = abs(input_data)
                if length is None:
                    if input_data == 0:
                        length = 1
                    else:
                        length = int(floor(log(input_int, 2)) + 1)

        elif isinstance(input_data, str):
            # Sign is decided by the "signed" parameter or - in the input_data
            # string

            # Signed binary with 0b is understood by default
            # to be unsigned unless other wise prepended by -
            # DEV NOTE: This is so that input_data = bin(7), singed=True
            #           is not misinterpreted as -1
            #           But as a consequence -1 cannot be given as 0b111, signed = True
            #           It can be given as only as :
            #                            *     -0b0001 or -0001, signed = True / False
            #                            *      1111, signed = True
            if "0b" in input_data:
                input_data = input_data.replace("0b", "")

            if len(input_data) == 0:
                input_int = 0
                if length is None:
                    length = 0

            # First priority to - in the string "-0b111" ( -7 ) or a signed
            # binary number starting with 0
            elif "-" in input_data or ((input_data[0] == "0") and signed):
                input_int = int(input_data, 2)

            # Next priority to 2s complement signed binary explicitly mentined
            # as signed and whose string does not contain 0b
            elif signed:
                # and input_data[0] == "1" ( this check was redundant, hence removed.
                # Included as comment to improve code clarity )
                input_int = BitArray(bin=input_data).int
                # This is equivalent to 2's complement of input_data

            # Unsigned
            else:
                input_int = int(input_data, 2)

        # Copying the value from the input_data BinPyBits object
        else:
            input_int = input_data.int if signed else input_data.uint

            if length is None:
                length = input_data.length

        if length is None:
            length = len(input_data)

        # Initializating the super class
        if signed:
            return BitArray.__new__(class_type, int=input_int, length=length)

        else:
            return BitArray.__new__(class_type, uint=input_int, length=length)
开发者ID:Famguy,项目名称:BinPy,代码行数:82,代码来源:bittools.py


注:本文中的bitstring.BitArray.__new__方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。