这篇文章是关于Python的Construct库
代码
以下是我的构造的定义:from construct import *
AttributeHandleValuePair = "attribute_handle_value_pair" / Struct(
"handle" / Int16ul,
"value" / Bytes(this._.length - 2)
)
AttReadByTypeResponse = "read_by_type_response" / Struct(
"length" / Int8ul, # The size in bytes of each handle/value pair
"attribute_data_list" / AttributeHandleValuePair[2]
)
问题
正在尝试运行以下命令:
AttReadByTypeResponse.sizeof(dict(length=4, attribute_data_list=[dict(handle=1, value=2), dict(handle=3, value=4)])
我收到以下错误:
^{pr2}$
我发现的
每个attribute_handle_value_pair的value字段的大小是从其父字段的length派生出来的。我认为sizeof()方法试图先计算attribute_handle_value_pair的大小,而read_by_type_response的length字段仍未定义,因此无法计算其大小。在
我尝试将value字段的长度改为静态值,效果很好。在
我的问题
如何计算依赖于其父构造的构造的sizeof()?在
我应该重新设计这个协议的建模方式吗?如果是,那怎么办?在