Choice type¶
-
class
pyasn1.type.univ.
Choice
(componentType=None, tagSet=tagSet(), subtypeSpec=ConstraintsIntersection(), sizeSpec=ConstraintsIntersection())¶ Create Choice type.
Choice objects are mutable and duck-type Python
dict
objects.Keyword Arguments: - componentType (
NamedType
) – Object holding named ASN.1 types allowed within this collection - tagSet (
TagSet
) – Object representing non-default ASN.1 tag(s) - subtypeSpec (
ConstraintsIntersection
) – Object representing non-default ASN.1 subtype constraint(s) - sizeSpec (
ConstraintsIntersection
) – Object representing collection size constraint
Examples
class Afters(Choice): ''' ASN.1 specification: Afters ::= CHOICE { cheese [0] IA5String, dessert [1] IA5String } ''' componentType = NamedTypes( NamedType('cheese', IA5String().subtype( implicitTag=Tag(tagClassContext, tagFormatSimple, 0) ), NamedType('dessert', IA5String().subtype( implicitTag=Tag(tagClassContext, tagFormatSimple, 1) ) ) afters = Afters() afters['cheese'] = 'Mascarpone'
Note
The Choice type can only hold a single component at a time belonging to the list of allowed types.
-
clone
(componentType=None, tagSet=tagSet(), subtypeSpec=ConstraintsIntersection())¶ Create a modified version of Choice schema object.
The clone() method accepts the same set arguments as Choice class takes on instantiation except that all arguments of the clone() method are optional.
Whatever arguments are supplied, they are used to create a copy of self taking precedence over the ones used to instantiate self.
Possible values of self are never copied over thus clone() can only create a new schema object.
Returns: new instance of Choice type/value Note
Due to the mutable nature of the Choice object, even if no arguments are supplied, new Choice object will always be created as a shallow copy of self.
-
subtype
(componentType=None, implicitTag=Tag(), explicitTag=Tag(), subtypeSpec=ConstraintsIntersection())¶ Create a specialization of Choice schema object.
The subtype() method accepts the same set arguments as Choice class takes on instantiation except that all parameters of the subtype() method are optional.
With the exception of the arguments described below, the rest of supplied arguments they are used to create a copy of self taking precedence over the ones used to instantiate self.
The following arguments to subtype() create a ASN.1 subtype out of Choice type.
Other Parameters: - implicitTag (
Tag
) – Implicitly apply given ASN.1 tag object to self’sTagSet
, then use the result as new object’s ASN.1 tag(s). - explicitTag (
Tag
) – Explicitly apply given ASN.1 tag object to self’sTagSet
, then use the result as new object’s ASN.1 tag(s). - subtypeSpec (
ConstraintsIntersection
) – Add ASN.1 constraints object to one of the self’s, then use the result as new object’s ASN.1 constraints.
Returns: new instance of Choice type/value
Note
Due to the immutable nature of the Choice object, if no arguments are supplied, no new Choice object will be created and self will be returned instead.
- implicitTag (
-
tagSet
= <TagSet object at 0x7fcd8f440b50 untagged>¶ Set (on class, not on instance) or return a
TagSet
object representing ASN.1 tag(s) associated with Choice type.
-
componentType
= <NamedTypes object at 0x7fcd8f440bd0 types >¶ Default collection of ASN.1 types of component (e.g.
NamedType
) object representing ASN.1 type allowed within Choice type
-
subtypeSpec
= <ConstraintsIntersection object>¶ Set (on class, not on instance) or return a
ConstraintsIntersection
object imposing constraints on Choice type initialization values.
-
sizeSpec
= <ConstraintsIntersection object at 0x7fcd8f440d90 consts <ValueSizeConstraint object at 0x7fcd8f440d50 consts 1, 1>>¶ Default
ConstraintsIntersection
object imposing size constraint on Choice objects
-
getComponentByPosition
(idx, default=<NoValue object at 140520848870480>, instantiate=True)¶ Returns Choice type component by index.
Equivalent to Python sequence subscription operation (e.g. []).
Parameters: idx (
int
) – Component index (zero-based). Must either refer to an existing component or (if componentType is set) new ASN.1 schema object gets instantiated.Keyword Arguments: - default (
object
) – If set and requested component is a schema object, return the default object instead of the requested component. - instantiate (
bool
) – If True (default), inner component will be automatically instantiated. If ‘False’ either existing component or the noValue object will be returned.
Returns: PyAsn1Item
– a PyASN1 objectExamples
# can also be Set class MySequence(Sequence): componentType = NamedTypes( NamedType('id', OctetString()) ) s = MySequence() # returns component #0 with `.isValue` property False s.getComponentByPosition(0) # returns None s.getComponentByPosition(0, default=None) s.clear() # returns noValue s.getComponentByPosition(0, instantiate=False) # sets component #0 to OctetString() ASN.1 schema # object and returns it s.getComponentByPosition(0, instantiate=True) # sets component #0 to ASN.1 value object s.setComponentByPosition(0, 'ABCD') # returns OctetString('ABCD') value object s.getComponentByPosition(0, instantiate=False) s.clear() # returns noValue s.getComponentByPosition(0, instantiate=False)
- default (
-
setComponentByPosition
(idx, value=<NoValue object at 140520848870480>, verifyConstraints=True, matchTags=True, matchConstraints=True)¶ Assign Choice type component by position.
Equivalent to Python sequence item assignment operation (e.g. []).
Parameters: idx (
int
) – Component index (zero-based). Must either refer to existing component or to N+1 component. In the latter case a new component type gets instantiated (if componentType is set, or given ASN.1 object is taken otherwise) and appended to the Choice sequence.Keyword Arguments: - value (
object
orPyAsn1Item
derivative) – A Python value to initialize Choice component with (if componentType is set) or ASN.1 value object to assign to Choice component. Once a new value is set to idx component, previous value is dropped. - verifyConstraints (
bool
) – If False, skip constraints validation - matchTags (
bool
) – If False, skip component tags matching - matchConstraints (
bool
) – If False, skip component constraints matching
Returns: self
- value (
-
effectiveTagSet
¶ Return a
TagSet
object of the currently initialized component or self (if Choice is tagged).
-
getComponent
(innerFlag=False)¶ Return currently assigned component of the Choice object.
Returns: PyAsn1Item
– a PyASN1 object
-
getComponentByName
(name, default=<NoValue object at 140520848870480>, instantiate=True)¶ Returns Choice type component by name.
Equivalent to Python
dict
subscription operation (e.g. []).Parameters: name (
str
) – Choice type component nameKeyword Arguments: - default (
object
) – If set and requested component is a schema object, return the default object instead of the requested component. - instantiate (
bool
) – If True (default), inner component will be automatically instantiated. If ‘False’ either existing component or the noValue object will be returned.
Returns: PyAsn1Item
– Instantiate Choice component type or return existing component value- default (
-
getComponentByType
(tagSet, default=<NoValue object at 140520848870480>, instantiate=True, innerFlag=False)¶ Returns Choice type component by ASN.1 tag.
Parameters: tagSet (
TagSet
) – Object representing ASN.1 tags to identify one of Choice object componentKeyword Arguments: - default (
object
) – If set and requested component is a schema object, return the default object instead of the requested component. - instantiate (
bool
) – If True (default), inner component will be automatically instantiated. If ‘False’ either existing component or the noValue object will be returned.
Returns: PyAsn1Item
– a pyasn1 object- default (
-
getName
(innerFlag=False)¶ Return the name of currently assigned component of the Choice object.
Returns: str
– Choice component name
-
isSameTypeWith
(other, matchTags=True, matchConstraints=True)¶ Examine Choice type for equality with other ASN.1 type.
ASN.1 tags (
tag
) and constraints (constraint
) are examined when carrying out ASN.1 types comparison.Python class inheritance relationship is NOT considered.
Parameters: other (a pyasn1 type object) – Class instance representing ASN.1 type. Returns: bool
–True
if other is Choice type,False
otherwise.
-
isSuperTypeOf
(other, matchTags=True, matchConstraints=True)¶ Examine Choice type for subtype relationship with other ASN.1 type.
ASN.1 tags (
tag
) and constraints (constraint
) are examined when carrying out ASN.1 types comparison.Python class inheritance relationship is NOT considered.
Parameters: other (a pyasn1 type object) – Class instance representing ASN.1 type. Returns: bool
–True
if other is a subtype of Choice type,False
otherwise.
-
setComponentByName
(name, value=<NoValue object at 140520848870480>, verifyConstraints=True, matchTags=True, matchConstraints=True)¶ Assign Choice type component by name.
Equivalent to Python
dict
item assignment operation (e.g. []).Parameters: name (
str
) – Choice type component nameKeyword Arguments: - value (
object
orPyAsn1Item
derivative) – A Python value to initialize Choice component with (if componentType is set) or ASN.1 value object to assign to Choice component. - verifyConstraints (
bool
) – If False, skip constraints validation - matchTags (
bool
) – If False, skip component tags matching - matchConstraints (
bool
) – If False, skip component constraints matching
Returns: self
- value (
-
setComponentByType
(tagSet, value=<NoValue object at 140520848870480>, verifyConstraints=True, matchTags=True, matchConstraints=True, innerFlag=False)¶ Assign Choice type component by ASN.1 tag.
Parameters: tagSet (
TagSet
) – Object representing ASN.1 tags to identify one of Choice object componentKeyword Arguments: - value (
object
orPyAsn1Item
derivative) – A Python value to initialize Choice component with (if componentType is set) or ASN.1 value object to assign to Choice component. - verifyConstraints (
bool
) – If False, skip constraints validation - matchTags (
bool
) – If False, skip component tags matching - matchConstraints (
bool
) – If False, skip component constraints matching - innerFlag (
bool
) – If True, search for matching tagSet recursively.
Returns: self
- value (
-
isValue
¶ Indicate that Choice object represents ASN.1 value.
If isValue is False then this object represents just ASN.1 schema.
If isValue is True then, in addition to its ASN.1 schema features, this object can also be used like a Python built-in object (e.g. int, str, dict etc.).
Returns: bool
–False
if object represents just ASN.1 schema.True
if object represents ASN.1 schema and can be used as a normal value.Note
There is an important distinction between PyASN1 schema and value objects. The PyASN1 schema objects can only participate in ASN.1 schema-related operations (e.g. defining or testing the structure of the data). Most obvious uses of ASN.1 schema is to guide serialisation codecs whilst encoding/decoding serialised ASN.1 contents.
The PyASN1 value objects can additionally participate in many operations involving regular Python objects (e.g. arithmetic, comprehension etc).
- componentType (