Native Python types¶
-
pyasn1.codec.native.encoder.encode(asn1Value)¶ Turns ASN.1 object into a Python built-in type object(s).
Takes any ASN.1 object (e.g.
PyAsn1Itemderivative) walks all its components recursively and produces a Python built-in type or a tree of those.One exception is that instead of
dict, theOrderedDictcan be produced (whenever available) to preserve ordering of the components in ASN.1 SEQUENCE.Parameters: pyasn1 object to encode (or a tree of them) Returns: object– Python built-in type instance (or a tree of them)Raises: PyAsn1Error– On encoding errorsExamples
Encode ASN.1 value object into native Python types
>>> seq = SequenceOf(componentType=Integer()) >>> seq.extend([1, 2, 3]) >>> encode(seq) [1, 2, 3]
-
pyasn1.codec.native.decoder.decode(pyObject, asn1Spec)¶ Turns Python objects of built-in types into ASN.1 objects.
Takes Python objects of built-in types and turns them into a tree of ASN.1 objects (e.g.
PyAsn1Itemderivative) which may be a scalar or an arbitrary nested structure.Parameters: pyObject ( object) – A scalar or nested Python objectsKeyword Arguments: asn1Spec (any pyasn1 type object e.g. PyAsn1Itemderivative) – A pyasn1 type object to act as a template guiding the decoder. It is required for successful interpretation of Python objects mapping into their ASN.1 representations.Returns: PyAsn1Itemderivative – A scalar or constructed pyasn1 objectRaises: PyAsn1Error– On decoding errorsExamples
Decode native Python object into ASN.1 objects with ASN.1 schema
>>> seq = SequenceOf(componentType=Integer()) >>> s, _ = decode([1, 2, 3], asn1Spec=seq) >>> str(s) SequenceOf: 1 2 3