6.12 读取嵌套和可变长二进制数据

    模块可被用来编码/解码几乎所有类型的二进制的数据结构。为了解释清楚这种数据,假设你用下面的Python数据结构来表示一个组成一系列多边形的点的集合:

    现在假设这个数据被编码到一个以下列头部开始的二进制文件中去了:

    1. +------+--------+------------------------------------+
    2. |Byte | Type | Description |
    3. +======+========+====================================+
    4. |0 | int | 文件代码(0x1234,小端) |
    5. +------+--------+------------------------------------+
    6. |4 | double | x 的最小值(小端) |
    7. +------+--------+------------------------------------+
    8. |12 | double | y 的最小值(小端) |
    9. +------+--------+------------------------------------+
    10. |20 | double | x 的最大值(小端) |
    11. +------+--------+------------------------------------+
    12. |28 | double | y 的最大值(小端) |
    13. +------+--------+------------------------------------+
    14. |36 | int | 三角形数量(小端) |
    15. +------+--------+------------------------------------+

    紧跟着头部是一系列的多边形记录,编码格式如下:

    1. +------+--------+-------------------------------------------+
    2. |Byte | Type | Description |
    3. +======+========+===========================================+
    4. |0 | int | 记录长度(N字节) |
    5. +------+--------+-------------------------------------------+
    6. |4-N | Points | (X,Y) 坐标,以浮点数表示 |
    7. +------+--------+-------------------------------------------+

    为了写这样的文件,你可以使用如下的Python代码:

    1. import struct
    2. import itertools
    3.  
    4. def write_polys(filename, polys):
    5. # Determine bounding box
    6. flattened = list(itertools.chain(*polys))
    7. min_x = min(x for x, y in flattened)
    8. max_x = max(x for x, y in flattened)
    9. min_y = min(y for x, y in flattened)
    10. max_y = max(y for x, y in flattened)
    11. with open(filename, 'wb') as f:
    12. f.write(struct.pack('<iddddi', 0x1234,
    13. min_x, min_y,
    14. max_x, max_y,
    15. len(polys)))
    16. for poly in polys:
    17. size = len(poly) * struct.calcsize('<dd')
    18. f.write(struct.pack('<i', size + 4))
    19. for pt in poly:
    20. f.write(struct.pack('<dd', *pt))

    将数据读取回来的时候,可以利用函数 struct.unpack() ,代码很相似,基本就是上面写操作的逆序。如下:

    1. def read_polys(filename):
    2. with open(filename, 'rb') as f:
    3. # Read the header
    4. header = f.read(40)
    5. file_code, min_x, min_y, max_x, max_y, num_polys = \
    6. struct.unpack('<iddddi', header)
    7. polys = []
    8. for n in range(num_polys):
    9. pbytes, = struct.unpack('<i', f.read(4))
    10. poly = []
    11. for m in range(pbytes // 16):
    12. pt = struct.unpack('<dd', f.read(16))
    13. poly.append(pt)
    14. polys.append(poly)
    15. return polys

    尽管这个代码可以工作,但是里面混杂了很多读取、解包数据结构和其他细节的代码。如果用这样的代码来处理真实的数据文件,那未免也太繁杂了点。因此很显然应该有另一种解决方法可以简化这些步骤,让程序员只关注自最重要的事情。

    在本小节接下来的部分,我会逐步演示一个更加优秀的解析字节数据的方案。目标是可以给程序员提供一个高级的文件格式化方法,并简化读取和解包数据的细节。但是我要先提醒你,本小节接下来的部分代码应该是整本书中最复杂最高级的例子,使用了大量的面向对象编程和元编程技术。一定要仔细的阅读我们的讨论部分,另外也要参考下其他章节内容。

    首先,当读取字节数据的时候,通常在文件开始部分会包含文件头和其他的数据结构。尽管struct模块可以解包这些数据到一个元组中去,另外一种表示这种信息的方式就是使用一个类。就像下面这样:

    1. import struct
    2.  
    3. class StructField:
    4. '''
    5. Descriptor representing a simple structure field
    6. '''
    7. def __init__(self, format, offset):
    8. self.format = format
    9. self.offset = offset
    10. def __get__(self, instance, cls):
    11. if instance is None:
    12. return self
    13. else:
    14. r = struct.unpack_from(self.format, instance._buffer, self.offset)
    15. return r[0] if len(r) == 1 else r
    16.  
    17. class Structure:
    18. def __init__(self, bytedata):
    19. self._buffer = memoryview(bytedata)

    这里我们使用了一个描述器来表示每个结构字段,每个描述器包含一个结构兼容格式的代码以及一个字节偏移量,存储在内部的内存缓冲中。在 get() 方法中,struct.unpack_from()函数被用来从缓冲中解包一个值,省去了额外的分片或复制操作步骤。

    Structure 类就是一个基础类,接受字节数据并存储在内部的内存缓冲中,并被 StructField 描述器使用。这里使用了 memoryview() ,我们会在后面详细讲解它是用来干嘛的。

    下面的例子利用这个类来读取之前我们写入的多边形数据的头部数据:

    1. >>> f = open('polys.bin', 'rb')
    2. >>> phead = PolyHeader(f.read(40))
    3. >>> phead.file_code == 0x1234
    4. True
    5. >>> phead.min_x
    6. 0.5
    7. >>> phead.min_y
    8. 0.5
    9. >>> phead.max_x
    10. 7.0
    11. >>> phead.max_y
    12. 9.2
    13. >>> phead.num_polys
    14. 3
    15. >>>

    这个很有趣,不过这种方式还是有一些烦人的地方。首先,尽管你获得了一个类接口的便利,但是这个代码还是有点臃肿,还需要使用者指定很多底层的细节(比如重复使用 StructField ,指定偏移量等)。另外,返回的结果类同样确实一些便利的方法来计算结构的总数。

    任何时候只要你遇到了像这样冗余的类定义,你应该考虑下使用类装饰器或元类。元类有一个特性就是它能够被用来填充许多低层的实现细节,从而释放使用者的负担。下面我来举个例子,使用元类稍微改造下我们的 Structure 类:

    1. class StructureMeta(type):
    2. '''
    3. Metaclass that automatically creates StructField descriptors
    4. '''
    5. fields = getattr(self, '_fields_', [])
    6. byte_order = ''
    7. offset = 0
    8. for format, fieldname in fields:
    9. if format.startswith(('<','>','!','@')):
    10. byte_order = format[0]
    11. format = format[1:]
    12. format = byte_order + format
    13. setattr(self, fieldname, StructField(format, offset))
    14. offset += struct.calcsize(format)
    15. setattr(self, 'struct_size', offset)
    16.  
    17. class Structure(metaclass=StructureMeta):
    18. def __init__(self, bytedata):
    19. self._buffer = bytedata
    20.  
    21. @classmethod
    22. def from_file(cls, f):
    23. return cls(f.read(cls.struct_size))

    使用新的 类,你可以像下面这样定义一个结构:

    1. class PolyHeader(Structure):
    2. _fields_ = [
    3. ('<i', 'file_code'),
    4. ('d', 'min_x'),
    5. ('d', 'min_y'),
    6. ('d', 'max_x'),
    7. ('d', 'max_y'),
    8. ('i', 'num_polys')
    9. ]

    正如你所见,这样写就简单多了。我们添加的类方法 from_file()让我们在不需要知道任何数据的大小和结构的情况下就能轻松的从文件中读取数据。比如:

    1. >>> f = open('polys.bin', 'rb')
    2. >>> phead = PolyHeader.from_file(f)
    3. >>> phead.file_code == 0x1234
    4. True
    5. >>> phead.min_x
    6. 0.5
    7. >>> phead.min_y
    8. 0.5
    9. >>> phead.max_x
    10. 7.0
    11. >>> phead.max_y
    12. 9.2
    13. >>> phead.num_polys
    14. 3
    15. >>>

    一旦你开始使用了元类,你就可以让它变得更加智能。例如,假设你还想支持嵌套的字节结构,下面是对前面元类的一个小的改进,提供了一个新的辅助描述器来达到想要的效果:

    1. class NestedStruct:
    2. '''
    3. Descriptor representing a nested structure
    4. '''
    5. def __init__(self, name, struct_type, offset):
    6. self.name = name
    7. self.struct_type = struct_type
    8. self.offset = offset
    9.  
    10. def __get__(self, instance, cls):
    11. if instance is None:
    12. return self
    13. else:
    14. data = instance._buffer[self.offset:
    15. self.offset+self.struct_type.struct_size]
    16. result = self.struct_type(data)
    17. # Save resulting structure back on instance to avoid
    18. # further recomputation of this step
    19. setattr(instance, self.name, result)
    20. return result
    21.  
    22. class StructureMeta(type):
    23. '''
    24. Metaclass that automatically creates StructField descriptors
    25. '''
    26. def __init__(self, clsname, bases, clsdict):
    27. fields = getattr(self, '_fields_', [])
    28. byte_order = ''
    29. offset = 0
    30. for format, fieldname in fields:
    31. if isinstance(format, StructureMeta):
    32. setattr(self, fieldname,
    33. NestedStruct(fieldname, format, offset))
    34. offset += format.struct_size
    35. else:
    36. if format.startswith(('<','>','!','@')):
    37. byte_order = format[0]
    38. format = format[1:]
    39. format = byte_order + format
    40. setattr(self, fieldname, StructField(format, offset))
    41. offset += struct.calcsize(format)
    42. setattr(self, 'struct_size', offset)

    在这段代码中,NestedStruct 描述器被用来叠加另外一个定义在某个内存区域上的结构。它通过将原始内存缓冲进行切片操作后实例化给定的结构类型。由于底层的内存缓冲区是通过一个内存视图初始化的,所以这种切片操作不会引发任何的额外的内存复制。相反,它仅仅就是之前的内存的一个叠加而已。另外,为了防止重复实例化,通过使用和8.10小节同样的技术,描述器保存了该实例中的内部结构对象。

    使用这个新的修正版,你就可以像下面这样编写:

    令人惊讶的是,它也能按照预期的正常工作,我们实际操作下:

    1. >>> f = open('polys.bin', 'rb')
    2. >>> phead = PolyHeader.from_file(f)
    3. >>> phead.file_code == 0x1234
    4. True
    5. >>> phead.min # Nested structure
    6. <__main__.Point object at 0x1006a48d0>
    7. >>> phead.min.x
    8. 0.5
    9. >>> phead.min.y
    10. 0.5
    11. >>> phead.max.x
    12. 7.0
    13. >>> phead.max.y
    14. >>> phead.num_polys
    15. 3
    16. >>>

    到目前为止,一个处理定长记录的框架已经写好了。但是如果组件记录是变长的呢?比如,多边形文件包含变长的部分。

    1. class SizedRecord:
    2. def __init__(self, bytedata):
    3. self._buffer = memoryview(bytedata)
    4.  
    5. @classmethod
    6. def from_file(cls, f, size_fmt, includes_size=True):
    7. sz_nbytes = struct.calcsize(size_fmt)
    8. sz_bytes = f.read(sz_nbytes)
    9. sz, = struct.unpack(size_fmt, sz_bytes)
    10. buf = f.read(sz - includes_size * sz_nbytes)
    11. return cls(buf)
    12.  
    13. def iter_as(self, code):
    14. if isinstance(code, str):
    15. s = struct.Struct(code)
    16. for off in range(0, len(self._buffer), s.size):
    17. yield s.unpack_from(self._buffer, off)
    18. elif isinstance(code, StructureMeta):
    19. size = code.struct_size
    20. for off in range(0, len(self._buffer), size):
    21. data = self._buffer[off:off+size]
    22. yield code(data)

    类方法 SizedRecord.from_file() 是一个工具,用来从一个文件中读取带大小前缀的数据块,这也是很多文件格式常用的方式。作为输入,它接受一个包含大小编码的结构格式编码,并且也是自己形式。可选的 includes_size 参数指定了字节数是否包含头部大小。下面是一个例子教你怎样使用从多边形文件中读取单独的多边形数据:

    1. >>> f = open('polys.bin', 'rb')
    2. >>> phead = PolyHeader.from_file(f)
    3. >>> phead.num_polys
    4. 3
    5. >>> polydata = [ SizedRecord.from_file(f, '<i')
    6. ... for n in range(phead.num_polys) ]
    7. >>> polydata
    8. [<__main__.SizedRecord object at 0x1006a4d50>,
    9. <__main__.SizedRecord object at 0x1006a4f50>,
    10. <__main__.SizedRecord object at 0x10070da90>]
    11. >>>

    可以看出,SizedRecord 实例的内容还没有被解析出来。可以使用 iter_as() 方法来达到目的,这个方法接受一个结构格式化编码或者是 Structure 类作为输入。这样子可以很灵活的去解析数据,例如:

    1. >>> for n, poly in enumerate(polydata):
    2. ... print('Polygon', n)
    3. ... for p in poly.iter_as('<dd'):
    4. ... print(p)
    5. ...
    6. Polygon 0
    7. (1.0, 2.5)
    8. (3.5, 4.0)
    9. (2.5, 1.5)
    10. Polygon 1
    11. (7.0, 1.2)
    12. (5.1, 3.0)
    13. (0.5, 7.5)
    14. (0.8, 9.0)
    15. Polygon 2
    16. (3.4, 6.3)
    17. (1.2, 0.5)
    18. (4.6, 9.2)
    19. >>>
    20.  
    21. >>> for n, poly in enumerate(polydata):
    22. ... print('Polygon', n)
    23. ... for p in poly.iter_as(Point):
    24. ... print(p.x, p.y)
    25. ...
    26. Polygon 0
    27. 1.0 2.5
    28. 3.5 4.0
    29. 2.5 1.5
    30. Polygon 1
    31. 7.0 1.2
    32. 5.1 3.0
    33. 0.5 7.5
    34. 0.8 9.0
    35. Polygon 2
    36. 3.4 6.3
    37. 1.2 0.5
    38. 4.6 9.2
    39. >>>

    将所有这些结合起来,下面是一个 read_polys() 函数的另外一个修正版:

    1. class Point(Structure):
    2. _fields_ = [
    3. ('<d', 'x'),
    4. ('d', 'y')
    5. ]
    6.  
    7. class PolyHeader(Structure):
    8. _fields_ = [
    9. ('<i', 'file_code'),
    10. (Point, 'min'),
    11. (Point, 'max'),
    12. ('i', 'num_polys')
    13. ]
    14.  
    15. def read_polys(filename):
    16. polys = []
    17. with open(filename, 'rb') as f:
    18. phead = PolyHeader.from_file(f)
    19. for n in range(phead.num_polys):
    20. rec = SizedRecord.from_file(f, '<i')
    21. poly = [ (p.x, p.y) for p in rec.iter_as(Point) ]
    22. polys.append(poly)
    23. return polys

    这一节向你展示了许多高级的编程技术,包括描述器,延迟计算,元类,类变量和内存视图。然而,它们都为了同一个特定的目标服务。

    上面的实现的一个主要特征是它是基于懒解包的思想。当一个 实例被创建时,init() 仅仅只是创建一个字节数据的内存视图,没有做其他任何事。特别的,这时候并没有任何的解包或者其他与结构相关的操作发生。这样做的一个动机是你可能仅仅只对一个字节记录的某一小部分感兴趣。我们只需要解包你需要访问的部分,而不是整个文件。

    为了实现懒解包和打包,需要使用 StructField 描述器类。用户在 fields 中列出来的每个属性都会被转化成一个 StructField 描述器,它将相关结构格式码和偏移值保存到存储缓存中。元类 StructureMeta 在多个结构类被定义时自动创建了这些描述器。我们使用元类的一个主要原因是它使得用户非常方便的通过一个高层描述就能指定结构格式,而无需考虑低层的细节问题。

    StructureMeta 的一个很微妙的地方就是它会固定字节数据顺序。也就是说,如果任意的属性指定了一个字节顺序(<表示低位优先 或者 >表示高位优先),那后面所有字段的顺序都以这个顺序为准。这么做可以帮助避免额外输入,但是在定义的中间我们仍然可能切换顺序的。比如,你可能有一些比较复杂的结构,就像下面这样:

    之前我们提到过,memoryview() 的使用可以帮助我们避免内存的复制。当结构存在嵌套的时候,memoryviews 可以叠加同一内存区域上定义的机构的不同部分。这个特性比较微妙,但是它关注的是内存视图与普通字节数组的切片操作行为。如果你在一个字节字符串或字节数组上执行切片操作,你通常会得到一个数据的拷贝。而内存视图切片不是这样的,它仅仅是在已存在的内存上面叠加而已。因此,这种方式更加高效。

    还有很多相关的章节可以帮助我们扩展这里讨论的方案。参考8.13小节使用描述器构建一个类型系统。8.10小节有更多关于延迟计算属性值的讨论,并且跟NestedStruct描述器的实现也有关。9.19小节有一个使用元类来初始化类成员的例子,和 类非常相似。Python的 ctypes 源码同样也很有趣,它提供了对定义数据结构、数据结构嵌套这些相似功能的支持。

    原文: