Skip to content

Commit 3d91216

Browse files
committed
docs/library/enum.rst: Add Enum class.
Signed-off-by: Ihor Nehrutsa <[email protected]>
1 parent 4d034f8 commit 3d91216

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

docs/library/enum.rst

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
:mod:`enum` -- MicroPython Enum class like in the Python
2+
========================================================
3+
4+
.. module:: enum
5+
:synopsis: MicroPython Enum class like in the Python
6+
7+
This module implements enumeration Enum class for the MicroPython.
8+
9+
class Enum -- enumeration
10+
-------------------------
11+
12+
Class Enum is based in dictionary `dict` class and provides a way to create enumeration
13+
objects with key-value pairs.
14+
15+
Constructor
16+
-----------
17+
18+
.. class:: Enum(arg)
19+
20+
Construct and return a new Enum object using the following parameter:
21+
22+
- *arg* is the dictionary of key-value pairs.
23+
24+
Usage example::
25+
26+
from enum import Enum
27+
e = Enum({"X": 1, "Y": 2}) # create Enum object from dictionary of key-value pairs
28+
print(e)
29+
e.A = 'a' # add new key-value pair
30+
e.B = 'b'
31+
print(e)
32+
del e.B # delete key-value pair
33+
print(e)
34+
print(e.X) # get value from key
35+
print(e.key_from_value(2)) # get key from value
36+
print(e.B) # raise an exception due to no such a key attribute
37+
38+
Output::
39+
40+
Enum({'X':1, 'Y':2})
41+
Enum({'A':'a', 'B':'b', 'X':1, 'Y':2})
42+
Enum({'A':'a', 'X':1, 'Y':2})
43+
1
44+
Y
45+
KeyError: No such attribute: B
46+
47+
Methods
48+
-------
49+
50+
.. _Enum_append:
51+
.. method:: Enum.append(*arg, **enums):
52+
53+
Append key-value pairs with the following parameters:
54+
55+
- *arg* is the dictionary or tuple of dictionaryes or list of dictionaryes: [dict | tuple(dict1, dict2, ...) | list(dict1, dict2, ...)]
56+
- *kw_args* are key1=value1, key2=value2, ...
57+
58+
.. method:: Enum.is_value(value)
59+
60+
Check if value is in the Enum object.
61+
Return True if value is in the Enum object, otherwise False.
62+
63+
.. method:: Enum.key_from_value(value)
64+
65+
Get key from value. If value is not in the Enum object, raise an exception.
66+
67+
Usage example::
68+
69+
from enum import Enum
70+
71+
class State(Enum):
72+
Stop = 10
73+
Run = 20
74+
Ready = 30
75+
76+
77+
state = State()
78+
print("state:", State())
79+
80+
current_state = state.Stop
81+
print("current_state:", current_state, state.key_from_value(current_state))
82+
if current_state == state.Stop:
83+
print(" Stop state")
84+
if current_state != state.Ready:
85+
print(" Not a Ready state")
86+
print(" Run!")
87+
current_state = state.Run
88+
print("current_state:", current_state, state.key_from_value(current_state))
89+
# some process
90+
i = -1
91+
while current_state != state.Ready:
92+
i += 1
93+
if state.is_value(i):
94+
if int(str(State(i))) == state.Ready:
95+
current_state = state.Ready
96+
print(".", end="")
97+
print()
98+
print("current_state:", current_state, state.key_from_value(current_state))
99+
print("Done!")
100+
101+
Output::
102+
103+
state: State({'Run':20, 'Stop':10, 'Ready':30})
104+
current_state: 10 State.Stop
105+
Stop state
106+
Not a Ready state
107+
Run!
108+
current_state: 20 State.Run
109+
...............................
110+
current_state: 30 State.Ready
111+
Done!
112+
113+
.. warning::
114+
115+
There is the trouble with the isinstance()/type().
116+
117+
>>> isinstance(State.Run, State)
118+
False
119+
>>> State(State.Ready) == State.Ready
120+
False
121+
>>> int(str(State(State.Ready))) == State.Ready
122+
True
123+
>>> type(State(State.Ready))
124+
<class 'State'>
125+
>>> type(state(State.Ready))
126+
<class 'int'>
127+
>>> state(State.Ready) == State.Ready
128+
True
129+
130+
Function
131+
--------
132+
133+
.. function:: enum(**kw_args)
134+
135+
Return Enum object using the following parameters: see :ref:`Enum.append <Enum_append>` method.
136+
137+
Usage examples::
138+
139+
from enum import Enum
140+
e = enum()
141+
e
142+
e = enum(X=1, Y=2)
143+
e
144+
e = enum({'X':1, 'Y':2})
145+
e
146+
e = enum(({'X':1, 'Y':2}, {'A':'a', 'B':'b'}, ))
147+
e
148+
e = enum([{'X':1, 'Y':2}, {'A':'a', 'B':'b'}, ])
149+
e
150+
151+
Output::
152+
153+
Enum()
154+
Enum({'X':1, 'Y':2})
155+
Enum({'X':1, 'Y':2})
156+
Enum({ 'A':'a', 'B':'b', 'X':1, 'Y':2})
157+
Enum({ 'A':'a', 'B':'b', 'X':1, 'Y':2})
158+
159+
.. note::
160+
161+
ZZZ.
162+
163+
.. seealso::
164+
165+
..
166+
:ref:`enum_test.py <enum_test.py>`
167+
168+
:ref:`enum.py <enum.py>`

docs/library/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ library.
6262
builtins.rst
6363
cmath.rst
6464
collections.rst
65+
enum.rst
6566
errno.rst
6667
gc.rst
6768
gzip.rst

0 commit comments

Comments
 (0)