Enums, further in Python
auto, StrEnum and Flag cover most of what enums are for.
from enum import Flag, StrEnum, auto
class Theme(StrEnum):
DEFAULT = auto()
MONOKAI = auto()
class Mode(Flag):
EDITOR = auto()
TIMED = auto()
QUIET = auto()
print(Theme.MONOKAI, Theme.MONOKAI == "monokai")
combo = Mode.EDITOR | Mode.TIMED
print(combo, Mode.TIMED in combo, Mode.QUIET in combo)
print([m.name for m in Mode if m in combo])
How it works
auto()assigns the values so you cannot mistype one.StrEnummembers are strings, so they serialize directly.Flagmembers combine with the bitwise operators.
Keywords and builtins used here
ModeThemeclassforifprint
The run, in numbers
- Lines
- 18
- Characters to type
- 343
- Tokens
- 100
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 57 seconds.
Step 2 of 3 in Data & enums, step 46 of 53 in Pythonic Python.