1# Inspired from https://construct.readthedocs.io/en/latest/intro.html#example 2import construct 3 4format = construct.Struct( 5 "signature" / construct.Const(b"BMP"), 6 "width" / construct.Int8ub, 7 "height" / construct.Int8ub, 8 "pixels" / construct.Array(construct.this.width * construct.this.height, construct.Byte), 9) 10a = format.build(dict(width=3,height=2,pixels=[7,8,9,11,12,13])) 11assert(a == b'BMP\x03\x02\x07\x08\t\x0b\x0c\r') 12b = format.parse(b'BMP\x03\x02\x07\x08\t\x0b\x0c\r') 13assert(b.signature == b'BMP') 14assert(b.width == 3) 15assert(b.height == 2) 16assert(b.pixels == [7, 8, 9, 11, 12, 13]) 17