我的代码需要帮助。我有两个结构,它们使用第三个结构。
class Bar(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.NamedType('first',univ.Integer().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,0))),
namedtype.NamedType('second',univ.Integer().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
)
class Foo(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.OptionalNamedType('test', Bar().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,3))),
)
class Foo1(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.OptionalNamedType('test', Bar().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,2))),
)
因此,在类Foo中,Bar有一个标签[3]并且在类Foo1中,例如[2]。如果我只有一节课我会做
tagSet = tagBaseSet = tag.initTagSet(tag.Tag(tag.tagClassContext,tag.tagFormatSimple,3))
bar类中的(它正在工作)。但是我该如何应对这种问题呢? 任何帮助将不胜感激。
编辑: 这是asn1表示:
Bar ::= SEQUENCE {
first [0] INTEGER,
second [1] INTEGER
}
Foo ::= SEQUENCE {
first [0] INTEGER,
second [1] INTEGER OPTIONAL,
third [2] INTEGER OPTIONAL,
bar [3] Bar
}
Foo1 ::= SEQUENCE {
first [0] INTEGER,
second [1] INTEGER OPTIONAL,
bar [2] Bar
}
EDIT2:代码:运行然后删除注释(我得到不兼容的标签。在Bar中没有tagSet,在这两种情况下也是如此):
from pyasn1.type import univ,namedtype,tag
class Bar(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.NamedType('first',univ.Integer().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,0))),
namedtype.NamedType('second',univ.Integer().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
)
tagSet = tagBaseSet = tag.initTagSet(tag.Tag(tag.tagClassContext,tag.tagFormatConstructed,3))
class Foo(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.OptionalNamedType('test', Bar().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,3))),
)
class Foo1(univ.Sequence):
componentType = namedtype.NamedTypes(
namedtype.OptionalNamedType('test', Bar().subtype(implicitTag=tag.Tag(tag.tagClassContext,tag.tagFormatSimple,2))),
)
bar = Bar()
bar.setComponentByName('first',1)
bar.setComponentByName('second',2)
fo = Foo()
fo.setComponentByName('test',bar)
# fo1 = Foo1()
# fo1.setComponentByName('test',bar)
print fo.prettyPrint()
# print fo1.prettyPrint()