util.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:pytoshop 作者: mdboom 项目源码 文件源码
def read_value(fd, fmt, endian='>'):
    # type: (BinaryIO, unicode, unicode) -> Any
    """
    Read a values from a file-like object.

    Parameters
    ----------
    fd : file-like object
        Must be opened for reading, in binary mode.

    fmt : str
        A `struct` module `format character
        <https://docs.python.org/2/library/struct.html#format-characters>`__
        string.

    endian : str
        The endianness. Must be ``>`` or ``<``.  Default: ``>``.

    Returns
    -------
    value : any
        The value(s) read from the file.

        If a single value, it is returned alone.  If multiple values,
        a tuple is returned.
    """
    fmt = endian + fmt
    size = struct.calcsize(fmt)  # type: ignore
    result = struct.unpack(fmt, fd.read(size))  # type: ignore
    if len(result) == 1:
        return result[0]
    else:
        return result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号