0

我的代码需要帮助。我有两个结构,它们正在使用第三个。

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))

在酒吧课上(它正在工作)。但是我该如何应对这样的问题呢?任何帮助,将不胜感激。

编辑:这是 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()
4

0 回答 0