summaryrefslogtreecommitdiff
path: root/docs/source/pyasn1/type/namedval/contents.rst
blob: 73f86c75de16e6b85c8e3ebe88810c60190c3bd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

.. _type.namedval:

Enumerating numbers
-------------------

Some ASN.1 types such as :ref:`Integer <univ.Integer>`,
:ref:`Enumerated <univ.Enumerated>` and :ref:`BitString <univ.BitString>`
may enumerate their otherwise numeric values associating them with
human-friendly labels.

.. code-block:: python

   class ErrorStatus(Integer):
   """
   ASN.1 specification:

   error-status
                 INTEGER {
                     noError(0),
                     tooBig(1),
                     noSuchName(2),
                     ...
                  }
   """
   namedValues = NamedValues(
       ('noError', 0), ('tooBig', 1), ('noSuchName', 2)
   )

The enumerated types behave exactly like the non-enumerated ones but,
additionally, values can be referred by labels.

.. code-block:: python

   errorStatus = ErrorStatus('tooBig')

   assert errorStatus == 1


.. toctree::
   :maxdepth: 2

   /pyasn1/type/namedval/namedval