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


Python FileIO.readinto方法代码示例

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


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

示例1: HidrawDS4Device

# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import readinto [as 别名]
class HidrawDS4Device(DS4Device):
    def __init__(self, name, addr, type, hidraw_device, event_device):
        try:
            self.report_fd = os.open(hidraw_device, os.O_RDWR | os.O_NONBLOCK)
            self.fd = FileIO(self.report_fd, "rb+", closefd=False)
            self.input_device = InputDevice(event_device)
            self.input_device.grab()
        except (OSError, IOError) as err:
            raise DeviceError(err)

        self.buf = bytearray(self.report_size)

        super(HidrawDS4Device, self).__init__(name, addr, type)

    def read_report(self):
        try:
            ret = self.fd.readinto(self.buf)
        except IOError:
            return

        # Disconnection
        if ret == 0:
            return

        # Invalid report size or id, just ignore it
        if ret < self.report_size or self.buf[0] != self.valid_report_id:
            return False

        if self.type == "bluetooth":
            # Cut off bluetooth data
            buf = zero_copy_slice(self.buf, 2)
        else:
            buf = self.buf

        return self.parse_report(buf)

    def read_feature_report(self, report_id, size):
        op = HIDIOCGFEATURE(size + 1)
        buf = bytearray(size + 1)
        buf[0] = report_id

        return fcntl.ioctl(self.fd, op, bytes(buf))

    def write_report(self, report_id, data):
        if self.type == "bluetooth":
            # TODO: Add a check for a kernel that supports writing
            # output reports when such a kernel has been released.
            return

        hid = bytearray((report_id,))
        self.fd.write(hid + data)

    def close(self):
        try:
            self.fd.close()
            self.input_device.ungrab()
        except IOError:
            pass
开发者ID:7hunderbug,项目名称:ds4drv,代码行数:60,代码来源:hidraw.py

示例2: _bench_file_read

# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import readinto [as 别名]
def _bench_file_read(hasher, expect):
    f = FileIO(tmpf.name, "r")
    b = bytearray(blksize)

    h = hasher()
    while 1:
        n = f.readinto(b)
        if n == 0:
            break

        h.update(xbuffer(b, 0, n))  # NOTE b[:n] does copy

    f.close()
    assert h.digest() == expect
开发者ID:Nexedi,项目名称:wendelin.core,代码行数:16,代码来源:bench_0virtmem.py

示例3: FileDataReader

# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import readinto [as 别名]
class FileDataReader(AbstractDataReader):
    """ A reader that can read data from a file
    """

    def __init__(self, filename):
        """
        :param filename: The file to read
        :type filename: str
        :raise spinnman.exceptions.SpinnmanIOException: If the file\
                    cannot found or opened for reading
        """
        try:
            self._fileio = FileIO(filename, "r")
        except IOError as e:
            raise SpinnmanIOException(str(e))

    def read(self, n_bytes):
        """ See :py:meth:`spinnman.data.abstract_data_reader.AbstractDataReader.read`
        """
        return bytearray(self._fileio.read(n_bytes))

    def readinto(self, data):
        """ See :py:meth:`spinnman.data.abstract_data_reader.AbstractDataReader.readinto`
        """
        return self._fileio.readinto(data)

    def readall(self):
        """ See :py:meth:`spinnman.data.abstract_data_reader.AbstractDataReader.readall`
        """
        return self._fileio.readall()

    def close(self):
        """ Closes the file

        :return: Nothing is returned:
        :rtype: None
        :raise spinnman.exceptions.SpinnmanIOException: If the file\
                    cannot be closed
        """
        try:
            self._fileio.close()
        except IOError as e:
            raise SpinnmanIOException(str(e))
开发者ID:Scaatis,项目名称:SpiNNMan,代码行数:45,代码来源:file_data_reader.py

示例4: _bench_file_readbig

# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import readinto [as 别名]
def _bench_file_readbig(hasher, expect):
    f = FileIO(tmpf.name, "r")
    # b = mmap(-1, filesize, MAP_SHARED | MAP_ANONYMOUS, PROT_READ | PROT_WRITE)
    b = bytearray(filesize)
    bm = memoryview(b)

    h = hasher()
    pos = 0
    while 1:
        n = f.readinto(bm[pos:])
        if n == 0:
            break

        h.update(xbuffer(b, pos, n))  # NOTE b[pos:n] does copy
        pos += n

    del bm
    del b
    f.close()
    assert h.digest() == expect
开发者ID:Nexedi,项目名称:wendelin.core,代码行数:22,代码来源:bench_0virtmem.py

示例5: File

# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import readinto [as 别名]

#.........这里部分代码省略.........
            self.cursor += len(result)

            if self.offset >= len(self.descriptor):
                if self.cursor >= self.info.directory_offset:
                    self.state = File.DIRECTORY
                    self.dir.seek(0)
                    self.stream_offset = None

                    if self.data:
                        self.data.close()
                        self.data = None

                else:
                    self.state = File.HEADER
                    self.offset = 0
                    self.stream_offset = self.stream.tell()
                    self._load_stream_item()

            # descriptor is optional (state will now be HEADER or DIRECTORY)
            if not result: return self.read(count)

            return result
        elif state == File.DIRECTORY:
            result = self.dir.read(count)
            self.cursor += len(result)

            return result
        else:
            raise RuntimeError('Invalid state: %r' % self.state)

    def readable(self):
        return True

    def readinto(self, b):
        count = len(b)
        if count == 0: return 0

        state = self.state
        if state == File.HEADER:
            header_len = len(self.zip_header)
            previous_offset = self.offset

            current_offset = self.offset = \
                    min(previous_offset + count, header_len)

            read = current_offset - previous_offset
            b[:read] = self.zip_header[previous_offset:current_offset]
            self.cursor += read

            if current_offset == header_len:
                self.state = File.DATA
                if not self.data: self._open_data_file()

            return read

        elif state == File.DATA:
            read = self.data.readinto(b)
            self.cursor += read

            if self.data.tell() >= self.data_len:
                self.state = File.DESCRIPTOR
                self.offset = 0

            # empty data file (state will now be DESCRIPTOR)
            if not read: return self.readinto(b)
开发者ID:terencehonles,项目名称:xzip,代码行数:69,代码来源:fs.py


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