From c34f53e7ee787577482f9e1b67ea507299dd3be3 Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Tue, 14 Nov 2017 11:12:09 +0100 Subject: added example code snippets to the docstrings (#101) --- pyasn1/codec/ber/decoder.py | 22 ++++++++++++++++++++++ pyasn1/codec/ber/encoder.py | 20 ++++++++++++++++++++ pyasn1/codec/cer/decoder.py | 22 ++++++++++++++++++++++ pyasn1/codec/cer/encoder.py | 20 ++++++++++++++++++++ pyasn1/codec/der/decoder.py | 22 ++++++++++++++++++++++ pyasn1/codec/der/encoder.py | 20 ++++++++++++++++++++ pyasn1/codec/native/decoder.py | 13 +++++++++++++ pyasn1/codec/native/encoder.py | 12 ++++++++++++ 8 files changed, 151 insertions(+) (limited to 'pyasn1/codec') diff --git a/pyasn1/codec/ber/decoder.py b/pyasn1/codec/ber/decoder.py index 725eaa3..3737742 100644 --- a/pyasn1/codec/ber/decoder.py +++ b/pyasn1/codec/ber/decoder.py @@ -1355,6 +1355,28 @@ class Decoder(object): #: ------ #: :py:class:`~pyasn1.error.PyAsn1Error` #: On decoding errors +#: +#: Examples +#: -------- +#: Decode BER serialisation without ASN.1 schema +#: +#: .. code-block:: pycon +#: +#: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03') +#: >>> print(s.prettyPrint()) +#: SequenceOf: +#: 1 2 3 +#: +#: Decode BER serialisation with ASN.1 schema +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03', asn1Spec=seq) +#: >>> print(s.prettyPrint()) +#: SequenceOf: +#: 1 2 3 +#: decode = Decoder(tagMap, typeMap) # XXX diff --git a/pyasn1/codec/ber/encoder.py b/pyasn1/codec/ber/encoder.py index c8eac34..a915f52 100644 --- a/pyasn1/codec/ber/encoder.py +++ b/pyasn1/codec/ber/encoder.py @@ -666,4 +666,24 @@ class Encoder(object): #: ------ #: :py:class:`~pyasn1.error.PyAsn1Error` #: On encoding errors +#: +#: Examples +#: -------- +#: Encode Python value into BER with ASN.1 schema +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> encode([1, 2, 3], asn1Spec=seq) +#: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03' +#: +#: Encode ASN.1 value object into BER +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> seq.extend([1, 2, 3]) +#: >>> encode(seq) +#: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03' +#: encode = Encoder(tagMap, typeMap) diff --git a/pyasn1/codec/cer/decoder.py b/pyasn1/codec/cer/decoder.py index 3cc29c7..2a9d94f 100644 --- a/pyasn1/codec/cer/decoder.py +++ b/pyasn1/codec/cer/decoder.py @@ -89,4 +89,26 @@ class Decoder(decoder.Decoder): #: ------ #: :py:class:`~pyasn1.error.PyAsn1Error` #: On decoding errors +#: +#: Examples +#: -------- +#: Decode CER serialisation without ASN.1 schema +#: +#: .. code-block:: pycon +#: +#: >>> s, _ = decode(b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00') +#: >>> print(s.prettyPrint()) +#: SequenceOf: +#: 1 2 3 +#: +#: Decode CER serialisation with ASN.1 schema +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> s, _ = decode(b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00', asn1Spec=seq) +#: >>> print(s.prettyPrint()) +#: SequenceOf: +#: 1 2 3 +#: decode = Decoder(tagMap, decoder.typeMap) diff --git a/pyasn1/codec/cer/encoder.py b/pyasn1/codec/cer/encoder.py index b4c68ec..80cdc35 100644 --- a/pyasn1/codec/cer/encoder.py +++ b/pyasn1/codec/cer/encoder.py @@ -271,6 +271,26 @@ class Encoder(encoder.Encoder): #: ------ #: :py:class:`~pyasn1.error.PyAsn1Error` #: On encoding errors +#: +#: Examples +#: -------- +#: Encode Python value into CER with ASN.1 schema +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> encode([1, 2, 3], asn1Spec=seq) +#: b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00' +#: +#: Encode ASN.1 value object into CER +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> seq.extend([1, 2, 3]) +#: >>> encode(seq) +#: b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00' +#: encode = Encoder(tagMap, typeMap) # EncoderFactory queries class instance and builds a map of tags -> encoders diff --git a/pyasn1/codec/der/decoder.py b/pyasn1/codec/der/decoder.py index 1da37a0..e7956a4 100644 --- a/pyasn1/codec/der/decoder.py +++ b/pyasn1/codec/der/decoder.py @@ -69,4 +69,26 @@ class Decoder(decoder.Decoder): #: ------ #: :py:class:`~pyasn1.error.PyAsn1Error` #: On decoding errors +#: +#: Examples +#: -------- +#: Decode DER serialisation without ASN.1 schema +#: +#: .. code-block:: pycon +#: +#: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03') +#: >>> print(s.prettyPrint()) +#: SequenceOf: +#: 1 2 3 +#: +#: Decode DER serialisation with ASN.1 schema +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03', asn1Spec=seq) +#: >>> print(s.prettyPrint()) +#: SequenceOf: +#: 1 2 3 +#: decode = Decoder(tagMap, typeMap) diff --git a/pyasn1/codec/der/encoder.py b/pyasn1/codec/der/encoder.py index 8496252..5d6c6c0 100644 --- a/pyasn1/codec/der/encoder.py +++ b/pyasn1/codec/der/encoder.py @@ -84,4 +84,24 @@ class Encoder(encoder.Encoder): #: ------ #: :py:class:`~pyasn1.error.PyAsn1Error` #: On encoding errors +#: +#: Examples +#: -------- +#: Encode Python value into DER with ASN.1 schema +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> encode([1, 2, 3], asn1Spec=seq) +#: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03' +#: +#: Encode ASN.1 value object into DER +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> seq.extend([1, 2, 3]) +#: >>> encode(seq) +#: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03' +#: encode = Encoder(tagMap, typeMap) diff --git a/pyasn1/codec/native/decoder.py b/pyasn1/codec/native/decoder.py index cd60649..55ccc3c 100644 --- a/pyasn1/codec/native/decoder.py +++ b/pyasn1/codec/native/decoder.py @@ -193,4 +193,17 @@ class Decoder(object): #: ------ #: :py:class:`~pyasn1.error.PyAsn1Error` #: On decoding errors +#: +#: Examples +#: -------- +#: Decode native Python object into ASN.1 objects with ASN.1 schema +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> s, _ = decode([1, 2, 3], asn1Spec=seq) +#: >>> print(s.prettyPrint()) +#: SequenceOf: +#: 1 2 3 +#: decode = Decoder(tagMap, typeMap) diff --git a/pyasn1/codec/native/encoder.py b/pyasn1/codec/native/encoder.py index 53d16fc..e99e1df 100644 --- a/pyasn1/codec/native/encoder.py +++ b/pyasn1/codec/native/encoder.py @@ -209,4 +209,16 @@ class Encoder(object): #: ------ #: :py:class:`~pyasn1.error.PyAsn1Error` #: On encoding errors +#: +#: Examples +#: -------- +#: Encode ASN.1 value object into native Python types +#: +#: .. code-block:: pycon +#: +#: >>> seq = SequenceOf(componentType=Integer()) +#: >>> seq.extend([1, 2, 3]) +#: >>> encode(seq) +#: [1, 2, 3] +#: encode = Encoder(tagMap, typeMap) -- cgit v1.2.1