plastid.util.io.binary module¶
Tools for reading values from binary files
See Also¶
structBinary data structures in Python
- class plastid.util.io.binary.BinaryParserFactory(name, fmt, fields)[source]¶
Bases:
objectParser factory for different types of binary records.
Creates parsers that unpack binary byte streams into dictionaries that match field names to values. These parsers are most useful as components of binary file readers.
- Parameters
- namestr
Name for parser
- fmtstr
String specifying binary format of data. See
struct- fieldslist
Ordered list of field names to bind to data unpacked from binary file
See also
structFor information on format strings
Examples
A binary RGB color parser:
>>> ColorParser = BinaryParserFactory("ColorParser","3Q",["r","g","b"]) >>> fh = open("some_binary_file_containing_colors.bin","rb") # 'b' is important in mode flag!! >>> fh.seek(byte_location_of_an_rgb_color) >>> rgb_dict = ColorParser(fh) # read and parse 3 8-bit integers from file >>> rgb_dict { "r" : 255, "g" : 0, "b" : 52 }
- Attributes
- namestr
Human-readable name for parser
- fmtstr
String specifying binary format of data, as specified in
struct- fieldslist
List of strings specifying variable names to bind to data when unpacked from a binary file, in same order as items in
fmt- nt
namedtuple A
namedtupleinstance that will provide names to the unpacked data
Methods
__call__(fh[, byte_order])Parse data from fh into a dictionary mapping field names to their values
calcsize([byte_order])Return calculated size, in bytes, of record
- plastid.util.io.binary.find_null_bytes(inp, null=b'\x00')[source]¶
Finds all null characters in a byte-formatted input string
- Parameters
- inpbytes (or str in Python 2.7)
byte string
- Returns
numpy.ndarraynumpy array of integers indexing where the null character ** was found