Ghi byte vào tệp python

60 ví dụ mã Python được tìm thấy liên quan đến " write bytes". Bạn có thể bỏ phiếu cho những cái bạn thích hoặc bỏ phiếu cho những cái bạn không thích và chuyển đến dự án gốc hoặc tệp nguồn bằng cách nhấp vào các liên kết phía trên mỗi ví dụ

def write_bytes(self, address, data):
        address = int(address)
        if not self.isProcessOpen:
            raise ProcessException("Can't write_bytes(%s, %s), process %s is not open" % (address, data, self.pid))
        buffer = create_string_buffer(data)
        sizeWriten = c_size_t(0)
        bufferSize = sizeof(buffer) - 1
        _address = address
        _length = bufferSize + 1
        try:
            old_protect = self.VirtualProtectEx(_address, _length, PAGE_EXECUTE_READWRITE)
        except:
            pass

        res = kernel32.WriteProcessMemory(self.h_process, address, buffer, bufferSize, byref(sizeWriten))
        try:
            self.VirtualProtectEx(_address, _length, old_protect)
        except:
            pass

        return res 

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 

def WriteBytes(self, value, unhex=True):
        """
        Write a `bytes` type to the stream.

        Args:
            value (bytes): array of bytes to write to the stream.
            unhex (bool): (Default) True. Set to unhexlify the stream. Use when the bytes are not raw bytes; i.e. b'aabb'

        Returns:
            int: the number of bytes written.
        """
        if unhex:
            try:
                value = binascii.unhexlify(value)
            except binascii.Error:
                pass
        return self.stream.write(value) 

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 

def write_bytes(self, value, nbytes=None):
        import bitstring
        # TODO: strings are utf-8 from json reading
        if isinstance(value, six.text_type):
            value = value.encode('latin-1')

        value_len = len(value)

        # Ensure the string is under the required data width
        if nbytes is None:
            nbytes = value_len
        else:
            if value_len > nbytes:
                value = value[:nbytes]
            elif value_len < nbytes:
                value += b' ' * (nbytes - value_len)

        # Cannot use string format shortcut, i.e. 'bytes:{}={}' due to the
        # automatic whitespace trimming by bitstring.
        self.bit_stream += bitstring.Bits(bytes=value)
        return value 

def write_short_bytes(b):
    """
    Encode a Kafka short string which contains arbitrary bytes. A short string
    is limited to 32767 bytes in length by the signed 16-bit length prefix.
    A length prefix of -1 indicates ``null``, represented as ``None`` in
    Python.

    :param bytes b:
        No more than 32767 bytes, or ``None`` for the null encoding.
    :return: length-prefixed `bytes`
    :raises:
        `struct.error` for strings longer than 32767 characters
    """
    if b is None:
        return _NULL_SHORT_STRING
    if not isinstance(b, bytes):
        raise TypeError('{!r} is not bytes'.format(b))
    elif len(b) > 32767:
        raise struct.error(len(b))
    else:
        return struct.pack('>h', len(b)) + b 

def write_bytes(
        self, path: AnyPath, data: bytes, offset: int = 0, truncate: bool = True
    ) -> int:
        """
        The offset value is used to determine the index of the writing operation.
        If the offset is negative, we append the new bytes to the current content.
        If the truncate argument is set to True, the offset argument is also used to resize the file.
        Raises:
            FSError
        """
        path = FsPath(path)
        _, fd = await self.transactions.file_open(path, "w")
        try:
            if offset >= 0 and truncate:
                await self.transactions.fd_resize(fd, offset)
            return await self.transactions.fd_write(fd, data, offset)
        finally:
            await self.transactions.fd_close(fd)

    # Shutil-like interface 

________số 8_______

def createCounterWriteBytes(self, account, stat):
        return PerfCounter(counterType = PerfCounterType.COUNTER_TYPE_LARGE,
                           category = "storage",
                           name = "Storage Write Bytes",
                           instance = account,
                           value = stat.getWriteBytes(),
                           unit = 'byte',
                           refreshInterval = 60) 

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
0

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
1

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
2

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
3

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
4

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
5

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
6

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
7

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
8

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
9

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
0

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
1

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
2

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
3

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
4

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
5

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
6

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
7

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
8

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
9

def WriteBytes(self, value, unhex=True):
        """
        Write a `bytes` type to the stream.

        Args:
            value (bytes): array of bytes to write to the stream.
            unhex (bool): (Default) True. Set to unhexlify the stream. Use when the bytes are not raw bytes; i.e. b'aabb'

        Returns:
            int: the number of bytes written.
        """
        if unhex:
            try:
                value = binascii.unhexlify(value)
            except binascii.Error:
                pass
        return self.stream.write(value) 
0

def WriteBytes(self, value, unhex=True):
        """
        Write a `bytes` type to the stream.

        Args:
            value (bytes): array of bytes to write to the stream.
            unhex (bool): (Default) True. Set to unhexlify the stream. Use when the bytes are not raw bytes; i.e. b'aabb'

        Returns:
            int: the number of bytes written.
        """
        if unhex:
            try:
                value = binascii.unhexlify(value)
            except binascii.Error:
                pass
        return self.stream.write(value) 
1

def WriteBytes(self, value, unhex=True):
        """
        Write a `bytes` type to the stream.

        Args:
            value (bytes): array of bytes to write to the stream.
            unhex (bool): (Default) True. Set to unhexlify the stream. Use when the bytes are not raw bytes; i.e. b'aabb'

        Returns:
            int: the number of bytes written.
        """
        if unhex:
            try:
                value = binascii.unhexlify(value)
            except binascii.Error:
                pass
        return self.stream.write(value) 
2

def WriteVarBytes(self, value, endian="<"):
        """
        Write an integer value in a space saving way to the stream.
        Read more about variable size encoding here: http://docs.neo.org/en-us/node/network-protocol.html#convention

        Args:
            value (bytes):
            endian (str): specify the endianness. (Default) Little endian ('<'). Use '>' for big endian.

        Returns:
            int: the number of bytes written.
        """
        length = len(value)
        self.WriteVarInt(length, endian)

        return self.WriteBytes(value, unhex=False) 
6

def WriteBytes(self, value, unhex=True):
        """
        Write a `bytes` type to the stream.

        Args:
            value (bytes): array of bytes to write to the stream.
            unhex (bool): (Default) True. Set to unhexlify the stream. Use when the bytes are not raw bytes; i.e. b'aabb'

        Returns:
            int: the number of bytes written.
        """
        if unhex:
            try:
                value = binascii.unhexlify(value)
            except binascii.Error:
                pass
        return self.stream.write(value) 
4

def WriteBytes(self, value, unhex=True):
        """
        Write a `bytes` type to the stream.

        Args:
            value (bytes): array of bytes to write to the stream.
            unhex (bool): (Default) True. Set to unhexlify the stream. Use when the bytes are not raw bytes; i.e. b'aabb'

        Returns:
            int: the number of bytes written.
        """
        if unhex:
            try:
                value = binascii.unhexlify(value)
            except binascii.Error:
                pass
        return self.stream.write(value) 
5

def WriteBytes(self, value, unhex=True):
        """
        Write a `bytes` type to the stream.

        Args:
            value (bytes): array of bytes to write to the stream.
            unhex (bool): (Default) True. Set to unhexlify the stream. Use when the bytes are not raw bytes; i.e. b'aabb'

        Returns:
            int: the number of bytes written.
        """
        if unhex:
            try:
                value = binascii.unhexlify(value)
            except binascii.Error:
                pass
        return self.stream.write(value) 
6

def getNetworkWriteBytes(self, adapterId):
        net = psutil.net_io_counters(pernic=True)
        if net[adapterId] != None:
            bytes_sent1 = net[adapterId][0]
            time1 = time.time()
            
            time.sleep(0.2)
            
            net = psutil.net_io_counters(pernic=True)
            bytes_sent2 = net[adapterId][0]
            time2 = time.time()
            
            interval = (time2 - time1)
            
            return (bytes_sent2 - bytes_sent1) / interval
        else:
            return 0 
3

def WriteBytes(self, value, unhex=True):
        """
        Write a `bytes` type to the stream.

        Args:
            value (bytes): array of bytes to write to the stream.
            unhex (bool): (Default) True. Set to unhexlify the stream. Use when the bytes are not raw bytes; i.e. b'aabb'

        Returns:
            int: the number of bytes written.
        """
        if unhex:
            try:
                value = binascii.unhexlify(value)
            except binascii.Error:
                pass
        return self.stream.write(value) 
8

def WriteBytes(self, value, unhex=True):
        """
        Write a `bytes` type to the stream.

        Args:
            value (bytes): array of bytes to write to the stream.
            unhex (bool): (Default) True. Set to unhexlify the stream. Use when the bytes are not raw bytes; i.e. b'aabb'

        Returns:
            int: the number of bytes written.
        """
        if unhex:
            try:
                value = binascii.unhexlify(value)
            except binascii.Error:
                pass
        return self.stream.write(value) 
9

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 
0

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 
1

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 
2

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 
3

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 
4

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 
5

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 
6

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 
7

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 
8

def write_bytes(out_data):
    """Write Python2 and Python3 compatible byte stream.

    This is a bit compliated :-(

    It basically ensures that the unicode in your program is always encoded into an UTF-8 byte
    stream in the proper way (when bytes are written out to the network).

    Or worded another way, Unicode in the program is in the idealized unicode code points, and
    when you write it out to the network it needs represented a certain way (encoded).
    """
    if sys.version_info[0] >= 3:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(b'')):
            return out_data
    else:
        if isinstance(out_data, type(u'')):
            return out_data.encode('utf-8')
        elif isinstance(out_data, type(str(''))):
            return out_data
    msg = "Invalid value for out_data neither unicode nor byte string: {}".format(out_data)
    raise ValueError(msg) 
9

def write_bytes(self, value, nbytes=None):
        import bitstring
        # TODO: strings are utf-8 from json reading
        if isinstance(value, six.text_type):
            value = value.encode('latin-1')

        value_len = len(value)

        # Ensure the string is under the required data width
        if nbytes is None:
            nbytes = value_len
        else:
            if value_len > nbytes:
                value = value[:nbytes]
            elif value_len < nbytes:
                value += b' ' * (nbytes - value_len)

        # Cannot use string format shortcut, i.e. 'bytes:{}={}' due to the
        # automatic whitespace trimming by bitstring.
        self.bit_stream += bitstring.Bits(bytes=value)
        return value 
0

def write_bytes(self, value, nbytes=None):
        import bitstring
        # TODO: strings are utf-8 from json reading
        if isinstance(value, six.text_type):
            value = value.encode('latin-1')

        value_len = len(value)

        # Ensure the string is under the required data width
        if nbytes is None:
            nbytes = value_len
        else:
            if value_len > nbytes:
                value = value[:nbytes]
            elif value_len < nbytes:
                value += b' ' * (nbytes - value_len)

        # Cannot use string format shortcut, i.e. 'bytes:{}={}' due to the
        # automatic whitespace trimming by bitstring.
        self.bit_stream += bitstring.Bits(bytes=value)
        return value 
1

def write_bytes(self, value, nbytes=None):
        import bitstring
        # TODO: strings are utf-8 from json reading
        if isinstance(value, six.text_type):
            value = value.encode('latin-1')

        value_len = len(value)

        # Ensure the string is under the required data width
        if nbytes is None:
            nbytes = value_len
        else:
            if value_len > nbytes:
                value = value[:nbytes]
            elif value_len < nbytes:
                value += b' ' * (nbytes - value_len)

        # Cannot use string format shortcut, i.e. 'bytes:{}={}' due to the
        # automatic whitespace trimming by bitstring.
        self.bit_stream += bitstring.Bits(bytes=value)
        return value 
2

def write_bytes(self, value, nbytes=None):
        import bitstring
        # TODO: strings are utf-8 from json reading
        if isinstance(value, six.text_type):
            value = value.encode('latin-1')

        value_len = len(value)

        # Ensure the string is under the required data width
        if nbytes is None:
            nbytes = value_len
        else:
            if value_len > nbytes:
                value = value[:nbytes]
            elif value_len < nbytes:
                value += b' ' * (nbytes - value_len)

        # Cannot use string format shortcut, i.e. 'bytes:{}={}' due to the
        # automatic whitespace trimming by bitstring.
        self.bit_stream += bitstring.Bits(bytes=value)
        return value 
3