typestar

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

  1. auto() assigns the values so you cannot mistype one.
  2. StrEnum members are strings, so they serialize directly.
  3. Flag members combine with the bitwise operators.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Data & enums, step 46 of 53 in Pythonic Python.

← Previous Next →