Skip to content

Commit

Permalink
docs/library/enum: Add Enum class.
Browse files Browse the repository at this point in the history
Signed-off-by: Ihor Nehrutsa <[email protected]>
  • Loading branch information
IhorNehrutsa committed Mar 1, 2025
1 parent 69ffd2a commit 11c8de3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
74 changes: 74 additions & 0 deletions docs/library/enum.rst
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.
1 change: 1 addition & 0 deletions docs/library/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ the following libraries.
openamp.rst
uctypes.rst
vfs.rst
enum.rst

The following libraries provide drivers for hardware components.

Expand Down

0 comments on commit 11c8de3

Please sign in to comment.