Package: blob-reader
blob_readerMain usage:fromblob_readerimportBlockfromdataclassesimportdataclass@dataclassclassMyObj(Block):_int:int='2i'_txt:bytes='10s'withopen('some_file.bin','rb')asfp:obj=MyObj.read(fp)API:Block.read# Native readingBlock.read_be# Big endianBlock.read_le# Little endianBlock.writeBlock.write_beBlock.write_le# AliasesBlock.read_big_endian=Block.read_beBlock.read_network=Block.read_beBlock.read_little_endian=Block.read_leBlock.write_big_endian=Block.write_beBlock.write_network=Block.write_beBlock.write_little_endian=Block.write_leStruct alignmentCharacterByte orderSizeAlignment@nativenativenative=nativestandardnone<little-endianstandardnone\>big-endianstandardnone!network (= big-endian)standardnoneIf the first character is not one of these,'@'is assumed.Struct charactersOfficial documentationFormatC TypePython typeStandard _sizeNotesxpad byteno value(7)ccharbytes of length 11bsigned charinteger1(1), (2)Bunsigned charinteger1(2)?_Boolbool1(1)hshortinteger2(2)Hunsigned shortinteger2(2)iintinteger4(2)Iunsigned intinteger4(2)llonginteger4(2)Lunsigned longinteger4(2)qlong longinteger8(2)Qunsigned long longinteger8(2)nssize_tinteger(3)Nsize_tinteger(3)e(6)float2(4)ffloatfloat4(4)ddoublefloat8(4)schar[]bytes(9)pchar[]bytes(8)Pvoid*integer(5)Notes:The'?'conversion code corresponds to the _Bool type defined by C99. If this type is not available, it is simulated using a char. In standard mode, it is always represented by one byte.When attempting to pack a non-integer using any of the integer conversion codes, if the non-integer has a__index__()method then that method is called to convert the argument to an integer before packing.Changed in version 3.2: Added use of the__index__()method for non-integers.The'n'and'N'conversion codes are only available for the native _size (selected as the default or with the'@'byte order character). For the standard _size, you can use whichever of the other integer formats fits your application.For the'f','d'and'e'conversion codes, the packed representation uses the IEEE 754 binary32, binary64 or binary16 format (for'f','d'or'e'respectively), regardless of the floating-point format used by the platform.The'P'format character is only available for the native byte ordering (selected as the default or with the'@'byte order character). The byte order character'='chooses to use little- or big-endian ordering based on the host system. The struct module does not interpret this as native ordering, so the'P'format is not available.The IEEE 754 binary16 "half precision" type was introduced in the 2008 revision of the IEEE 754 standard. It has a sign bit, a 5-bit exponent and 11-bit precision (with 10 bits explicitly stored), and can represent numbers between approximately6.1e-05and6.5e+04at full precision. This type is not widely supported by C compilers: on a typical machine, an unsigned short can be used for storage, but not for math operations. See the Wikipedia page on the half-precision floating-point format for more information.When packing,'x'inserts one NUL byte.The'p'format character encodes a "Pascal string", meaning a short variable-length string stored in a fixed number of bytes, given by the count. The first byte stored is the length of the string, or 255, whichever is smaller. The bytes of the string follow. If the string passed in to pack() is too long (longer than the count minus 1), only the leadingcount-1bytes of the string are stored. If the string is shorter thancount-1, it is padded with null bytes so that exactly count bytes in all are used. Note that for unpack(), the'p'format character consumescountbytes, but that the string returned can never contain more than 255 bytes.For the's'format character, the count is interpreted as the length of the bytes, not a repeat count like for the other format characters; for example,'10s'means a single 10-byte string mapping to or from a single Python byte string, while'10c'means 10 separate one byte character elements (e.g.,cccccccccc) mapping to or from ten different Python byte objects. (See Examples for a concrete demonstration of the difference.) If a count is not given, it defaults to 1. For packing, the string is truncated or padded with null bytes as appropriate to make it fit. For unpacking, the resulting bytes object always has exactly the specified number of bytes. As a special case,'0s'means a single, empty string (while'0c'means 0 characters).Gotchas2ssaves a byte sequence of 2 bytes (ieb'ab')2csaves a list of 2 times 1 byte (ie[b'a', b'b'])2psaves a pascal string of maximum 1 byte. As the "2" is including the 'length' byte, sob'ab'would be saved asb'\x01a'instead!ExtensionsDynamic field sizesfromblob_readerimportBlockfromdataclassesimportdataclassfromioimportBytesIO@dataclassclassMyObj(Block):_int:int='H'_txt:bytes='{_int}s'stream=BytesIO(b'\x02abc')obj=MyObj.read(stream)# obj = MyObj(_int=2, _txt=b'ab')The fieldsize should be known before the actual usage. This is enforced for both writing and reading. In writing this doesn't matter too much, but for consistency’s sake it is enforced as well.ExceptionsEOFErrorThis is raised when fewer bytes can be read than are actually needed.ValueErrorIf some error happened during (un)packing, more information will be given.