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
评论列表
文章目录