|
| 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 dictionary `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. |
0 commit comments