Skip to content

Commit ae78eb8

Browse files
committed
docs/library/enum: Add Enum class.
Signed-off-by: Ihor Nehrutsa <[email protected]>
1 parent 69ffd2a commit ae78eb8

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

docs/library/enum.rst

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
:mod:`enum` -- MicroPython Enum class like in Python.
2+
========================================================
3+
4+
.. module:: enum
5+
:synopsis: MicroPython Enum class like in Python.
6+
7+
This module implements enumeration Enum class for MicroPython.
8+
Class Enum is based in dictonary `dict` class and provides a way to create enumeration
9+
objects with key-value pairs.
10+
11+
Usage examples::
12+
13+
from enum import Enum
14+
15+
class State(Enum):
16+
Stop = 10
17+
Run = 20
18+
Ready = 30
19+
20+
21+
state = State()
22+
print("state:", State())
23+
24+
current_state = state.Stop
25+
print("current_state:", current_state, state.key_from_value(current_state))
26+
if current_state == state.Stop:
27+
print(" Stop state")
28+
if current_state != state.Ready:
29+
print(" Not a Ready state")
30+
print(" Run!")
31+
current_state = state.Run
32+
print("current_state:", current_state, state.key_from_value(current_state))
33+
# some process
34+
i = -1
35+
while current_state != state.Ready:
36+
i += 1
37+
if state.is_value(i):
38+
if int(str(State(i))) == state.Ready:
39+
current_state = state.Ready
40+
print(".", end="")
41+
print()
42+
print("current_state:", current_state, state.key_from_value(current_state))
43+
print("Done!")
44+
45+
Output::
46+
47+
state: State({'Run':20, 'Stop':10, 'Ready':30})
48+
current_state: 10 State.Stop
49+
Stop state
50+
Not a Ready state
51+
Run!
52+
current_state: 20 State.Run
53+
...............................
54+
current_state: 30 State.Ready
55+
Done!
56+
57+
.. warning::
58+
59+
``enum`` module allows XXX.
60+
61+
>>> isinstance(State.Run, State)
62+
False
63+
>>> State(state.Ready) == state.Ready
64+
False
65+
>>> int(str(State(state.Ready))) == state.Ready
66+
True
67+
>>> type(State(State.Ready))
68+
<class 'State'>
69+
>>> type(state(State.Ready))
70+
<class 'int'>
71+
72+
.. note::
73+
74+
``enum`` module ZZZ.
75+
76+
.. seealso::
77+
78+
YYY.

docs/library/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ the following libraries.
107107
openamp.rst
108108
uctypes.rst
109109
vfs.rst
110+
enum.rst
110111

111112
The following libraries provide drivers for hardware components.
112113

0 commit comments

Comments
 (0)