Enumerations
Aliases (like simple strucs)
# Instead of this...
def process_data(data: dict[str, list[int]]) -> None:
# Do this:
DataDict = Dict[str, List[int]]
def process_data(data: DataDict) -> None:
Tools
Declaring Enums
from enum import Enum
class DaysOfWeek(Enum):
SUNDAY = 1
MONDAY = 2
TUESDAY = 3
You can declare multiple keys to have the same value as well:
from enum import Enum
class HttpCodes(Enum):
OK = 2
CREATED = 2
ACCEPTED = 2
ERROR = 4
UNAUTHORIZED = 4
FORBIDDEN = 4
Using Enums
print(DaysOfWeek.MONDAY.name) ---> "Monday" print(DaysOfWeek.MONDAY.value) ---> "2" print(DaysOfWeek['Monday']) --> DaysOfWeek.MONDAY DaysOfWeek(2) = DaysOfWeek.MONDAY
Iterating Over Enums
for day in DaysOfWeek:
print(day)
Enum Decorator
@enum.unique - Ensures two different enums don't have the same value auto() - sets values, starting at 1 and creating a sequence