Skip to content

Commit 018909b

Browse files
committed
enum: Version 1.3.0.
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
1 parent c3c61fc commit 018909b

4 files changed

Lines changed: 101 additions & 182 deletions

File tree

python-stdlib/enum/enum.md

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,17 @@ class Color(Enum):
3030
# Initialize the enum to process attributes
3131
c = Color()
3232

33-
print(c.RED) # Output: RED: red
34-
print(c.RED.name) # Output: RED
35-
print(c.RED.value) # Output: red
36-
print(c.RED()) # Output: red
33+
print(c.RED) # Output: RED: red
34+
print(c.RED.name) # Output: RED
35+
print(c.RED.value) # Output: red
36+
print(c.RED()) # Output: red
37+
print(c.is_value("RED")) # Output: true
38+
print(c.is_value(Color.RED)) # Output: true
39+
print(c.is_value('red')) # Output: true
40+
print(c.list()) # Output: [Color.RED: red, Color.GREEN: green]
41+
print([m for m in c]) # Output: [Color.RED: red, Color.GREEN: green]
42+
print([m.name for m in c]) # Output: ['RED', 'GREEN']
43+
print([m.value for m in c]) # Output: ['red', 'green']
3744
```
3845

3946

@@ -48,14 +55,16 @@ class Status(Enum):
4855
# Method A: Via Class (Simulates interpreting hardware/network bytes)
4956
# Uses __new__ logic to return the correct EnumValue
5057
current_status = Status(1)
51-
print(current_status.name) # Output: RUNNING
52-
print(current_status) # Output: RUNNING: 1
53-
print(current_status()) # Output: 1
58+
print(current_status.name) # Output: RUNNING
59+
print(current_status.value) # Output: 1
60+
print(current_status) # Output: Status.RUNNING: 1
61+
print(current_status()) # Output: 1
5462

5563
# Method B: Via Instance Call
5664
s = Status()
5765
print(s(0).name) # Output: IDLE
58-
print(s(0)) # Output: IDLE: 0
66+
print(s(0).value) # Output: 0
67+
print(s(0)) # Output: Status.IDLE: 0
5968
print(s(0)()) # Output: 0
6069
```
6170

@@ -67,25 +76,38 @@ If you need to create an Enum from external data (like a JSON config), use the f
6776
# Create a dynamic Enum instance
6877
State = Enum(name='State', names={'ON': 1, 'OFF': 2})
6978

70-
print(State.ON) # Output: ON: 1
71-
assert State.ON == 1 # Comparison with raw value
79+
print(State) # Output: Enum(name='State', names={'ON': 1, 'OFF': 2})
80+
print(State.ON) # Output: State.ON: 1
81+
print(State.ON.name) # Output: ON
82+
print(State.ON.value) # Output: 1
83+
print(State.ON()) # Output: 1
84+
assert State.ON == 1 # Comparison
85+
assert State.ON() == 1 #
86+
assert State.ON.value == 1 #
87+
assert State.ON.name == "ON" #
7288
```
7389

7490

7591
### 4. Serialization (Repr / Eval)
7692
The library ensures that the string representation can be used to perfectly reconstruct the object.
7793

7894
```python
95+
from enum import Enum
96+
97+
class Color(Enum):
98+
RED = 'red'
99+
GREEN = 'green'
100+
BLUE = 3
101+
79102
colors = Color()
80103
# Get serialized string
81104
serialized = repr(colors)
82105
# Reconstruct object
83106
restored_colors = eval(serialized)
84107

85-
print(f"Original: {colors}") # Output: Original: Color(names={'ON': 1, 'OFF': 2, 'GREEN': 'green', 'RED': 'red'})
86-
print(f"Restored: {restored_colors}") # Output: Restored: Color(names={'ON': 1, 'OFF': 2, 'GREEN': 'green', 'RED': 'red'})
108+
print(f"Original: {colors}") # Output: Original: Enum(name='Color', names={'BLUE': 3, 'RED': 'red', 'GREEN': 'green'})
109+
print(f"Restored: {restored_colors}") # Output: Restored: Enum(name='Color', names={'BLUE': 3, 'RED': 'red', 'GREEN': 'green'})
87110
print(colors == restored_colors) # Output: True
88-
89111
```
90112

91113

@@ -170,10 +192,10 @@ print(Color.GREEN.value, type(Color.GREEN.value))
170192

171193
| MicroPython v1.28.0 | Python 3.12.10 |
172194
| :--- | :--- |
173-
| [RED: 1, GREEN: 2, BLUE: 3] | [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 3>] |
174-
| GREEN: 2 <class 'EnumValue'> | Color.GREEN <enum 'Color'> |
175-
| GREEN: 2 | Color.GREEN |
176-
| GREEN: 2 | Color.GREEN |
195+
| [Color.RED: 1, Color.GREEN: 2, Color.BLUE: 3] | [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 3>] |
196+
| Color.GREEN: 2 <class 'EnumValue'> | Color.GREEN <enum 'Color'> |
197+
| Color.GREEN: 2 | Color.GREEN |
198+
| Color.GREEN: 2 | Color.GREEN |
177199
| GREEN <class 'str'> | GREEN <class 'str'> |
178200
| 2 <class 'int'> | 2 <class 'int'> |
179201

0 commit comments

Comments
 (0)