Encode this stream as a packed 16-bit unsigned integer.

        Returns:
            int: The packed encoded stream
2966
def encode(self):
        

        match_id = self.match_id
        if match_id is None:
            match_id = (1 << 11) - 1

        return (self.match_type << 12) | DataStreamSelector.SpecifierEncodings[self.match_spec] | match_id
729633
def _flush_bits_to_stream(self):
        
        if len(self._bits) == 0:
            return 0

        bits = list(self._bits)

        diff = 8 - (len(bits) % 8)
        padding = [0] * diff

        bits = bits + padding

        self._stream.write(bits_to_bytes(bits))

        self._bits.clear()
445440
def apply_34_encoding(self, inbits):
        
        def popcnt(b):
            """ Return number of "1" bits set in 'b' """
            return len([x for x in bin(b) if x == "1"])

        outbits = b""
        while len(inbits) > 0:  # process in chunks of 6 bytes
            bits = inbits[0:6]
            inbits = inbits[6:]
            xor_res = 0
            mul_res = 0
            index = 1
            for b in struct.unpack("B" * 6, bits):
                xor_res ^= b
                mul_res += index * popcnt(b)
                index += 1
            outbits += bits
            outbits += struct.pack("BB", xor_res, mul_res)
        return struct.unpack("<" + "I" * (len(outbits) // 4), outbits)
1045008
def encodeLength(length):
        
        if length < 128:
            ored_length = length
            offset = -1
        elif length < 16384:
            ored_length = length | 0x8000
            offset = -2
        elif length < 2097152:
            ored_length = length | 0xC00000
            offset = -3
        elif length < 268435456:
            ored_length = length | 0xE0000000
            offset = -4
        else:
            raise ConnectionError('Unable to encode length of {}'.format(length))

        return pack('!I', ored_length)[offset:]
296992
def encode_streaming(self, data):
        
        # Buffer value and size
        buffer = 0
        size = 0
        for s in data:
            b, v = self._table[s]
            # Shift new bits in the buffer
            buffer = (buffer << b) + v
            size += b
            while size >= 8:
                byte = buffer >> (size - 8)
                yield to_byte(byte)
                buffer = buffer - (byte << (size - 8))
                size -= 8

        # Handling of the final sub-byte chunk.
        # The end of the encoded bit stream does not align necessarily with byte boundaries,
        # so we need an "end of file" indicator symbol (_EOF) to guard against decoding
        # the non-data trailing bits of the last byte.
        # As an optimization however, while encoding _EOF, it is only necessary to encode up to
        # the end of the current byte and cut off there.
        # No new byte has to be started for the remainder, saving us one (or more) output bytes.
        if size > 0:
            b, v = self._table[_EOF]
            buffer = (buffer << b) + v
            size += b
            if size >= 8:
                byte = buffer >> (size - 8)
            else:
                byte = buffer << (8 - size)
            yield to_byte(byte)
244806
def unpack_float16(src):
    
    bc = BitConsumer(src)
    sign = bc.u_get(1)
    exponent = bc.u_get(5)
    mantissa = bc.u_get(10)
    exponent -= 16
    mantissa /= 2 ** 10
    num = (-1 ** sign) * mantissa * (10 ** exponent)
    return num
276321
def _encode(data):
    
    if not isinstance(data, bytes_types):
        data = six.b(str(data))
    return base64.b64encode(data).decode("utf-8")
675831
def decode16bit data, length
      data.split('').in_groups_of(4).collect()
      double_octets = data.split('').in_groups_of(4).map(&:join).map{|x| x.to_i(16) }[0, length / 2] # integer values
      double_octets.collect do |o|
        [o].pack('S')
      end.join.force_encoding('utf-16le').encode('utf-8')
    end
539266
def _decode16(self, offset):
        
        str_len, skip = self._decode_length(offset, 2)
        offset += skip

        # The len is the string len in utf-16 units
        encoded_bytes = str_len * 2

        data = self.m_charbuff[offset: offset + encoded_bytes]

        assert self.m_charbuff[offset + encoded_bytes:offset + encoded_bytes + 2] == b"\x00\x00", \
            "UTF-16 String is not null terminated! At offset={}".format(offset)

        return self._decode_bytes(data, 'utf-16', str_len)
499556
def bytes(self):
        
        result = []
        op = self.opcode.split(' ')
        argi = 0

        while op:
            q = op.pop(0)

            if q == 'XX':
                for k in range(self.argbytes[argi] - 1):
                    op.pop(0)

                result.extend(num2bytes(self.argval()[argi], self.argbytes[argi]))
                argi += 1
            else:
                result.append(int(q, 16))  # Add opcode

        if len(result) != self.size:
            raise InternalMismatchSizeError(len(result), self)

        return result
238181
def hex_to_int(value):
    
    if version_info.major >= 3:
        return int.from_bytes(value, "big")
    return int(value.encode("hex"), 16)
794870
def _decode(value):
        
        if value.isdigit():
            return int(value)
        if isinstance(value, bytes):
            return value.decode('utf-8')
        else:
            return value
1163630
def _as_chunk(self):
        

        extra_bits = int_from_bytes(self.contents[0:1])
        bit_string = '{0:b}'.format(int_from_bytes(self.contents[1:]))

        # Ensure we have leading zeros since these chunks may be concatenated together
        mod_bit_len = len(bit_string) % 8
        if mod_bit_len != 0:
            bit_string = ('0' * (8 - mod_bit_len)) + bit_string

        if extra_bits > 0:
            return bit_string[0:0 - extra_bits]

        return bit_string
752534
def int_to_var_bytes(x):
    
    if x < 253:
        return intbytes.to_bytes(x, 1)
    elif x < 65536:
        return bytearray([253]) + intbytes.to_bytes(x, 2)[::-1]
    elif x < 4294967296:
        return bytearray([254]) + intbytes.to_bytes(x, 4)[::-1]
    else:
        return bytearray([255]) + intbytes.to_bytes(x, 8)[::-1]
1306401
def as_unsigned(self):
        
        return self._cast(self._value, self._min(False), self._max(False), False)
99863
def read_length_encoded_integer(self):
        
        c = self.read_uint8()
        if c == NULL_COLUMN:
            return None
        if c < UNSIGNED_CHAR_COLUMN:
            return c
        elif c == UNSIGNED_SHORT_COLUMN:
            return self.read_uint16()
        elif c == UNSIGNED_INT24_COLUMN:
            return self.read_uint24()
        elif c == UNSIGNED_INT64_COLUMN:
            return self.read_uint64()
751341
def pack(self, value=None):
        
        if isinstance(value, type(self)):
            return value.pack()
        if value is None:
            value = self._value
        return struct.pack('!8B', *[int(v, 16) for v in value.split(':')])