python类to_bytes()的实例源码

hexlify_codec.py 文件源码 项目:android3dblendermouse 作者: sketchpunk 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def encode(self, input, final=False):
        state = self.state
        encoded = []
        for c in input.upper():
            if c in HEXDIGITS:
                z = HEXDIGITS.index(c)
                if state:
                    encoded.append(z + (state & 0xf0))
                    state = 0
                else:
                    state = 0x100 + (z << 4)
            elif c == ' ':      # allow spaces to separate values
                if state and self.errors == 'strict':
                    raise UnicodeError('odd number of hex digits')
                state = 0
            else:
                if self.errors == 'strict':
                    raise UnicodeError('non-hex digit found: %r' % c)
        self.state = state
        return serial.to_bytes(encoded)
hexlify_codec.py 文件源码 项目:microperi 作者: c0d3st0rm 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def encode(self, input, final=False):
        state = self.state
        encoded = []
        for c in input.upper():
            if c in HEXDIGITS:
                z = HEXDIGITS.index(c)
                if state:
                    encoded.append(z + (state & 0xf0))
                    state = 0
                else:
                    state = 0x100 + (z << 4)
            elif c == ' ':      # allow spaces to separate values
                if state and self.errors == 'strict':
                    raise UnicodeError('odd number of hex digits')
                state = 0
            else:
                if self.errors == 'strict':
                    raise UnicodeError('non-hex digit found: %r' % c)
        self.state = state
        return serial.to_bytes(encoded)
hexlify_codec.py 文件源码 项目:gcodeplot 作者: arpruss 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def encode(self, data, final=False):
        """\
        Incremental encode, keep track of digits and emit a byte when a pair
        of hex digits is found. The space is optional unless the error
        handling is defined to be 'strict'.
        """
        state = self.state
        encoded = []
        for c in data.upper():
            if c in HEXDIGITS:
                z = HEXDIGITS.index(c)
                if state:
                    encoded.append(z + (state & 0xf0))
                    state = 0
                else:
                    state = 0x100 + (z << 4)
            elif c == ' ':      # allow spaces to separate values
                if state and self.errors == 'strict':
                    raise UnicodeError('odd number of hex digits')
                state = 0
            else:
                if self.errors == 'strict':
                    raise UnicodeError('non-hex digit found: {!r}'.format(c))
        self.state = state
        return serial.to_bytes(encoded)
hexlify_codec.py 文件源码 项目:bitio 作者: whaleygeek 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def encode(self, data, final=False):
        """\
        Incremental encode, keep track of digits and emit a byte when a pair
        of hex digits is found. The space is optional unless the error
        handling is defined to be 'strict'.
        """
        state = self.state
        encoded = []
        for c in data.upper():
            if c in HEXDIGITS:
                z = HEXDIGITS.index(c)
                if state:
                    encoded.append(z + (state & 0xf0))
                    state = 0
                else:
                    state = 0x100 + (z << 4)
            elif c == ' ':      # allow spaces to separate values
                if state and self.errors == 'strict':
                    raise UnicodeError('odd number of hex digits')
                state = 0
            else:
                if self.errors == 'strict':
                    raise UnicodeError('non-hex digit found: {!r}'.format(c))
        self.state = state
        return serial.to_bytes(encoded)
hexlify_codec.py 文件源码 项目:microbit-serial 作者: martinohanlon 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def encode(self, input, final=False):
        state = self.state
        encoded = []
        for c in input.upper():
            if c in HEXDIGITS:
                z = HEXDIGITS.index(c)
                if state:
                    encoded.append(z + (state & 0xf0))
                    state = 0
                else:
                    state = 0x100 + (z << 4)
            elif c == ' ':      # allow spaces to separate values
                if state and self.errors == 'strict':
                    raise UnicodeError('odd number of hex digits')
                state = 0
            else:
                if self.errors == 'strict':
                    raise UnicodeError('non-hex digit found: %r' % c)
        self.state = state
        return serial.to_bytes(encoded)
hexlify_codec.py 文件源码 项目:ddt4all 作者: cedricp 项目源码 文件源码 阅读 44 收藏 0 点赞 0 评论 0
def encode(self, data, final=False):
        """\
        Incremental encode, keep track of digits and emit a byte when a pair
        of hex digits is found. The space is optional unless the error
        handling is defined to be 'strict'.
        """
        state = self.state
        encoded = []
        for c in data.upper():
            if c in HEXDIGITS:
                z = HEXDIGITS.index(c)
                if state:
                    encoded.append(z + (state & 0xf0))
                    state = 0
                else:
                    state = 0x100 + (z << 4)
            elif c == ' ':      # allow spaces to separate values
                if state and self.errors == 'strict':
                    raise UnicodeError('odd number of hex digits')
                state = 0
            else:
                if self.errors == 'strict':
                    raise UnicodeError('non-hex digit found: {!r}'.format(c))
        self.state = state
        return serial.to_bytes(encoded)
hexlify_codec.py 文件源码 项目:mt7687-serial-uploader 作者: will127534 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def encode(self, data, final=False):
        """\
        Incremental encode, keep track of digits and emit a byte when a pair
        of hex digits is found. The space is optional unless the error
        handling is defined to be 'strict'.
        """
        state = self.state
        encoded = []
        for c in data.upper():
            if c in HEXDIGITS:
                z = HEXDIGITS.index(c)
                if state:
                    encoded.append(z + (state & 0xf0))
                    state = 0
                else:
                    state = 0x100 + (z << 4)
            elif c == ' ':      # allow spaces to separate values
                if state and self.errors == 'strict':
                    raise UnicodeError('odd number of hex digits')
                state = 0
            else:
                if self.errors == 'strict':
                    raise UnicodeError('non-hex digit found: {!r}'.format(c))
        self.state = state
        return serial.to_bytes(encoded)
port_publisher.py 文件源码 项目:Jackal_Velodyne_Duke 作者: MengGuo 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def handle_socket_read(self):
        """Read from socket"""
        try:
            # read a chunk from the serial port
            data = self.socket.recv(1024)
            if data:
                # Process RFC 2217 stuff when enabled
                if self.rfc2217:
                    data = serial.to_bytes(self.rfc2217.filter(data))
                # add data to buffer
                self.buffer_net2ser += data
            else:
                # empty read indicates disconnection
                self.handle_disconnect()
        except socket.error:
            self.handle_socket_error()
hexlify_codec.py 文件源码 项目:Jackal_Velodyne_Duke 作者: MengGuo 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def encode(self, data, final=False):
        """\
        Incremental encode, keep track of digits and emit a byte when a pair
        of hex digits is found. The space is optional unless the error
        handling is defined to be 'strict'.
        """
        state = self.state
        encoded = []
        for c in data.upper():
            if c in HEXDIGITS:
                z = HEXDIGITS.index(c)
                if state:
                    encoded.append(z + (state & 0xf0))
                    state = 0
                else:
                    state = 0x100 + (z << 4)
            elif c == ' ':      # allow spaces to separate values
                if state and self.errors == 'strict':
                    raise UnicodeError('odd number of hex digits')
                state = 0
            else:
                if self.errors == 'strict':
                    raise UnicodeError('non-hex digit found: {!r}'.format(c))
        self.state = state
        return serial.to_bytes(encoded)
hexlify_codec.py 文件源码 项目:Jackal_Velodyne_Duke 作者: MengGuo 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def encode(self, data, final=False):
        """\
        Incremental encode, keep track of digits and emit a byte when a pair
        of hex digits is found. The space is optional unless the error
        handling is defined to be 'strict'.
        """
        state = self.state
        encoded = []
        for c in data.upper():
            if c in HEXDIGITS:
                z = HEXDIGITS.index(c)
                if state:
                    encoded.append(z + (state & 0xf0))
                    state = 0
                else:
                    state = 0x100 + (z << 4)
            elif c == ' ':      # allow spaces to separate values
                if state and self.errors == 'strict':
                    raise UnicodeError('odd number of hex digits')
                state = 0
            else:
                if self.errors == 'strict':
                    raise UnicodeError('non-hex digit found: {!r}'.format(c))
        self.state = state
        return serial.to_bytes(encoded)
rfc2217_server.py 文件源码 项目:btc-fpga-miner 作者: marsohod4you 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def reader(self):
        """loop forever and copy serial->socket"""
        self.log.debug('reader thread started')
        while self.alive:
            try:
                data = self.serial.read(1)              # read one, blocking
                n = self.serial.inWaiting()             # look if there is more
                if n:
                    data = data + self.serial.read(n)   # and get as much as possible
                if data:
                    # escape outgoing data when needed (Telnet IAC (0xff) character)
                    data = serial.to_bytes(self.rfc2217.escape(data))
                    self._write_lock.acquire()
                    try:
                        self.socket.sendall(data)       # send it over TCP
                    finally:
                        self._write_lock.release()
            except socket.error, msg:
                self.log.error('%s' % (msg,))
                # probably got disconnected
                break
        self.alive = False
        self.log.debug('reader thread terminated')
port_publisher.py 文件源码 项目:btc-fpga-miner 作者: marsohod4you 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def handle_socket_read(self):
        """Read from socket"""
        try:
            # read a chunk from the serial port
            data = self.socket.recv(1024)
            if data:
                # Process RFC 2217 stuff when enabled
                if self.rfc2217:
                    data = serial.to_bytes(self.rfc2217.filter(data))
                # add data to buffer
                self.buffer_net2ser += data
            else:
                # empty read indicates disconnection
                self.handle_disconnect()
        except socket.error:
            self.handle_socket_error()
hexlify_codec.py 文件源码 项目:android3dblendermouse 作者: sketchpunk 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def hex_encode(input, errors='strict'):
    return (serial.to_bytes([int(h, 16) for h in input.split()]), len(input))
hexlify_codec.py 文件源码 项目:android3dblendermouse 作者: sketchpunk 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def encode(self, input, errors='strict'):
        return serial.to_bytes([int(h, 16) for h in input.split()])
hexlify_codec.py 文件源码 项目:microperi 作者: c0d3st0rm 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def hex_encode(input, errors='strict'):
    return (serial.to_bytes([int(h, 16) for h in input.split()]), len(input))
hexlify_codec.py 文件源码 项目:microperi 作者: c0d3st0rm 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def encode(self, input, errors='strict'):
        return serial.to_bytes([int(h, 16) for h in input.split()])
hexlify_codec.py 文件源码 项目:gcodeplot 作者: arpruss 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def hex_encode(data, errors='strict'):
    """'40 41 42' -> b'@ab'"""
    return (serial.to_bytes([int(h, 16) for h in data.split()]), len(data))
hexlify_codec.py 文件源码 项目:gcodeplot 作者: arpruss 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def encode(self, data, errors='strict'):
        """'40 41 42' -> b'@ab'"""
        return serial.to_bytes([int(h, 16) for h in data.split()])
hexlify_codec.py 文件源码 项目:bitio 作者: whaleygeek 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def hex_encode(data, errors='strict'):
    """'40 41 42' -> b'@ab'"""
    return (serial.to_bytes([int(h, 16) for h in data.split()]), len(data))
hexlify_codec.py 文件源码 项目:bitio 作者: whaleygeek 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def encode(self, data, errors='strict'):
        """'40 41 42' -> b'@ab'"""
        return serial.to_bytes([int(h, 16) for h in data.split()])
hexlify_codec.py 文件源码 项目:microbit-serial 作者: martinohanlon 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def hex_encode(input, errors='strict'):
    return (serial.to_bytes([int(h, 16) for h in input.split()]), len(input))
hexlify_codec.py 文件源码 项目:microbit-serial 作者: martinohanlon 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def encode(self, input, errors='strict'):
        return serial.to_bytes([int(h, 16) for h in input.split()])
hexlify_codec.py 文件源码 项目:ddt4all 作者: cedricp 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def hex_encode(data, errors='strict'):
    """'40 41 42' -> b'@ab'"""
    return (serial.to_bytes([int(h, 16) for h in data.split()]), len(data))
hexlify_codec.py 文件源码 项目:ddt4all 作者: cedricp 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def encode(self, data, errors='strict'):
        """'40 41 42' -> b'@ab'"""
        return serial.to_bytes([int(h, 16) for h in data.split()])
hexlify_codec.py 文件源码 项目:mt7687-serial-uploader 作者: will127534 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def hex_encode(data, errors='strict'):
    """'40 41 42' -> b'@ab'"""
    return (serial.to_bytes([int(h, 16) for h in data.split()]), len(data))
hexlify_codec.py 文件源码 项目:mt7687-serial-uploader 作者: will127534 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def encode(self, data, errors='strict'):
        """'40 41 42' -> b'@ab'"""
        return serial.to_bytes([int(h, 16) for h in data.split()])
rfc2217_server.py 文件源码 项目:Jackal_Velodyne_Duke 作者: MengGuo 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def reader(self):
        """loop forever and copy serial->socket"""
        self.log.debug('reader thread started')
        while self.alive:
            try:
                data = self.serial.read(self.serial.in_waiting or 1)
                if data:
                    # escape outgoing data when needed (Telnet IAC (0xff) character)
                    self.write(serial.to_bytes(self.rfc2217.escape(data)))
            except socket.error as msg:
                self.log.error('{}'.format(msg))
                # probably got disconnected
                break
        self.alive = False
        self.log.debug('reader thread terminated')
rfc2217_server.py 文件源码 项目:Jackal_Velodyne_Duke 作者: MengGuo 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def writer(self):
        """loop forever and copy socket->serial"""
        while self.alive:
            try:
                data = self.socket.recv(1024)
                if not data:
                    break
                self.serial.write(serial.to_bytes(self.rfc2217.filter(data)))
            except socket.error as msg:
                self.log.error('{}'.format(msg))
                # probably got disconnected
                break
        self.stop()
port_publisher.py 文件源码 项目:Jackal_Velodyne_Duke 作者: MengGuo 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def handle_serial_read(self):
        """Reading from serial port"""
        try:
            data = os.read(self.serial.fileno(), 1024)
            if data:
                # store data in buffer if there is a client connected
                if self.socket is not None:
                    # escape outgoing data when needed (Telnet IAC (0xff) character)
                    if self.rfc2217:
                        data = serial.to_bytes(self.rfc2217.escape(data))
                    self.buffer_ser2net += data
            else:
                self.handle_serial_error()
        except Exception as msg:
            self.handle_serial_error(msg)
test_util.py 文件源码 项目:Jackal_Velodyne_Duke 作者: MengGuo 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def test_to_bytes(self):
        self.assertEqual(serial.to_bytes([1, 2, 3]), b'\x01\x02\x03')
        self.assertEqual(serial.to_bytes(b'\x01\x02\x03'), b'\x01\x02\x03')
        self.assertEqual(serial.to_bytes(bytearray([1,2,3])), b'\x01\x02\x03')
        # unicode is not supported test. use decode() instead of u'' syntax to be
        # compatible to Python 3.x < 3.4
        self.assertRaises(TypeError, serial.to_bytes, b'hello'.decode('utf-8'))


问题


面经


文章

微信
公众号

扫码关注公众号