也许更多的是基于面向对象编程-
将通用TLV定义为
class MYTLV(Packet):
fields_desc = [
ByteEnumField("type", 0x1, defaultTLV_enum),
FieldLenField("length", None, fmt='B', length_of="value"),
StrLenField("value", "ABCDEF", length_from=lambda x:x.length)
]
我有许多相同形式的TLV,但有不同的类型。 我怎样才能有更好的代码来减少代码
^{pr2}$所以以后我可以用-
PacketListField('tlvlist', [], newTLV(fields.type=newTLV_enum))
除了类型字段的字典外,所有TLV都是相同的。在
class MYTLV1(Packet):
fields_desc = [
ByteEnumField("type", 0x1, TLV1_enum),
FieldLenField("length", None, fmt='B', length_of="value"),
StrLenField("value", "ABCDEF", length_from=lambda x:x.length)
]
class MYTLV2(Packet):
fields_desc = [
ByteEnumField("type", 0x1, TLV2_enum),
FieldLenField("length", None, fmt='B', length_of="value"),
StrLenField("value", "ABCDEF", length_from=lambda x:x.length)
]
你可以这样做:
相关问题 更多 >
编程相关推荐