Skip to content

Commit a6ec2fa

Browse files
committed
docs/library/enum.rst: Add Enum class.
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
1 parent 4d034f8 commit a6ec2fa

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

docs/library/enum.rst

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
print("add new key-value pair")
30+
e.A = 'A' # add new key-value pair
31+
e.B = 'B'
32+
print(e)
33+
print("e.X:", e.X) # get value from key
34+
print("e.X.value:", e.X.value) # get value from key
35+
print("e(e.X):", e(e.X)) # get value from key
36+
print("e.key_from_value(1):", e.key_from_value(1)) # get key from value
37+
print("e.is_value(e.B):", e.is_value(e.B))
38+
print("del e.B")
39+
del e.B # delete key-value pair
40+
print(e)
41+
print("e.is_value('B'):", e.is_value('B')) # check if the value is in the Enum object
42+
print("e.B: will raise the KeyError exception")
43+
print(e.B) # raise an exception due to no such a key attribute
44+
45+
Output is::
46+
47+
Enum({'Y': 2, 'X': 1})
48+
add new key-value pair
49+
Enum({'A': 'A', 'B': 'B', 'Y': 2, 'X': 1})
50+
e.X: 1
51+
e.X.value: 1
52+
e(e.X): 1
53+
e.key_from_value(1): Enum.X
54+
e.is_value(e.B): True
55+
del e.B
56+
Enum({'A': 'A', 'Y': 2, 'X': 1})
57+
e.is_value('B'): False
58+
e.B: will raise the KeyError exception
59+
Traceback (most recent call last):
60+
File "<stdin>", line 234, in <module>
61+
File "<stdin>", line 126, in __getattr__
62+
KeyError: no such attribute: B
63+
64+
Methods
65+
-------
66+
67+
.. _Enum_append:
68+
.. method:: Enum.append(arg, **kw_args):
69+
70+
Append key-value pairs with the following parameters:
71+
72+
- *arg* is the dictionary of key-value pairs.
73+
- *kw_args* are key1=value1, key2=value2, ...
74+
75+
.. method:: Enum.is_value(value)
76+
77+
Check if the value is in the Enum object.
78+
Returns True if the value is in the Enum object, and False otherwise.
79+
80+
.. method:: Enum.key_from_value(value)
81+
82+
Returns the key from the value. If a value is not in the Enum object, it raises an exception.
83+
84+
Usage example::
85+
86+
from enum import Enum
87+
88+
class State(Enum):
89+
Stop = 10
90+
Run = 20
91+
Ready = 30
92+
93+
state = State()
94+
print("state:", State())
95+
96+
current_state = state.Stop
97+
print("current_state:", current_state, state.key_from_value(current_state))
98+
if current_state == state.Stop:
99+
print(" Stop state")
100+
if current_state != state.Ready:
101+
print(" Not a Ready state")
102+
print(" Run!")
103+
current_state = state.Run
104+
print("current_state:", current_state, state.key_from_value(current_state))
105+
# some process
106+
i = -1
107+
while current_state != state.Ready:
108+
i += 1
109+
if state.is_value(i):
110+
if state(i) == state.Ready:
111+
current_state = state.Ready
112+
print(".", end="")
113+
print()
114+
print("current_state:", current_state, state.key_from_value(current_state))
115+
print("Done!")
116+
117+
Output is::
118+
119+
state: State({'Ready': 30, 'Stop': 10, 'Run': 20})
120+
current_state: 10 State.Stop
121+
Stop state
122+
Not a Ready state
123+
Run!
124+
current_state: 20 State.Run
125+
...............................
126+
current_state: 30 State.Ready
127+
Done!
128+
129+
.. warning::
130+
131+
There is the trouble with the isinstance()/type().
132+
133+
>>> isinstance(State.Run, State)
134+
False
135+
>>> State(State.Ready) == State.Ready
136+
False
137+
>>> int(str(State(State.Ready))) == State.Ready
138+
True
139+
>>> type(State(State.Ready))
140+
<class 'State'>
141+
>>> type(state(State.Ready))
142+
<class 'int'>
143+
>>> state(State.Ready) == State.Ready
144+
True
145+
146+
Function
147+
--------
148+
149+
.. function:: enum(**kw_args)
150+
151+
Returns an Enum object from the following parameters:
152+
153+
- *kw_args* are key1=value1, key2=value2, ...
154+
155+
Usage examples::
156+
157+
from enum import enum
158+
e = enum()
159+
repr(e)
160+
str(e)
161+
e = enum(X=1, Y=2.0, A='A', ON=True)
162+
repr(e)
163+
str(e)
164+
e.X
165+
e.Y
166+
e.A
167+
e.ON
168+
169+
Output::
170+
171+
'{}'
172+
'Enum({})'
173+
"{'A': 'A', 'ON': True, 'Y': 2.0, 'X': 1}"
174+
"Enum({'A': 'A', 'ON': True, 'Y': 2.0, 'X': 1})"
175+
1
176+
2.0
177+
'A'
178+
True
179+
180+
.. note::
181+
182+
ZZZ.
183+
184+
.. seealso::
185+
186+
..
187+
:ref:`enum_test.py <enum_test.py>`
188+
:ref:`enum.py <enum.py>`

docs/library/index.rst

Lines changed: 1 addition & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy