forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ihor Nehrutsa <[email protected]>
- Loading branch information
1 parent
69ffd2a
commit 11c8de3
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
:mod:`enum` -- MicroPython Enum class like in Python. | ||
======================================================== | ||
|
||
.. module:: enum | ||
:synopsis: MicroPython Enum class like in Python. | ||
|
||
This module implements enumeration Enum class for MicroPython. | ||
Class Enum is based in dictonary `dict` class and provides a way to create enumeration | ||
objects with key-value pairs. | ||
|
||
Usage examples:: | ||
|
||
from enum import Enum | ||
|
||
class State(Enum): | ||
Stop = 10 | ||
Run = 20 | ||
Ready = 30 | ||
|
||
|
||
state = State() | ||
print("state:", State()) | ||
|
||
current_state = state.Stop | ||
print("current_state:", current_state, state.key_from_value(current_state)) | ||
if current_state == state.Stop: | ||
print(" Stop state") | ||
if current_state != state.Ready: | ||
print(" Not a Ready state") | ||
print(" Run!") | ||
current_state = state.Run | ||
print("current_state:", current_state, state.key_from_value(current_state)) | ||
# some process | ||
i = -1 | ||
while current_state != state.Ready: | ||
i += 1 | ||
if state.is_value(i): | ||
if int(str(State(i))) == state.Ready: | ||
current_state = state.Ready | ||
print(".", end="") | ||
print() | ||
print("current_state:", current_state, state.key_from_value(current_state)) | ||
print("Done!") | ||
|
||
Output:: | ||
|
||
state: State({'Run':20, 'Stop':10, 'Ready':30}) | ||
current_state: 10 State.Stop | ||
Stop state | ||
Not a Ready state | ||
Run! | ||
current_state: 20 State.Run | ||
............................... | ||
current_state: 30 State.Ready | ||
Done! | ||
|
||
.. warning:: | ||
|
||
``enum`` module allows XXX. | ||
|
||
>>> isinstance(State.Run, State) | ||
False | ||
>>> State(state.Ready) == state.Ready | ||
False | ||
>>> int(str(State(state.Ready))) == state.Ready | ||
True | ||
|
||
.. note:: | ||
|
||
``enum`` module ZZZ. | ||
|
||
.. seealso:: | ||
|
||
YYY. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters