Last active
September 12, 2017 19:40
-
-
Save jsatt/54db7dadc068a4a52910 to your computer and use it in GitHub Desktop.
A Python 3 Enum subclasses which allows for defining other attributes to the members.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import Enum, EnumMeta | |
class EnumeratedEnumMeta(EnumMeta): | |
def __new__(metacls, *args): | |
enum_class = super().__new__(metacls, *args) | |
enum_class._value2member_map_ = {m.value: m for v, m in enum_class._value2member_map_.items()} | |
return enum_class | |
class LabeledEnum(Enum, metaclass=EnumeratedEnumMeta): | |
def __init__(self, value, label): | |
self._value_ = value | |
self.label = label | |
# class Status(LabeledEnum): | |
# open = 1, 'Opened' | |
# review = 2, 'In Review' | |
# closed = 3, 'Closed' | |
class StructuredEnum(Enum, metaclass=EnumeratedEnumMeta): | |
def __init__(self, value, kwargs): | |
self._value_ = value | |
for k, v in kwargs.items(): | |
setattr(self, k, v) | |
# class StatusEnum(StructuredEnum): | |
# open = 1, {'label': 'Opened', 'stuff': 'things'} | |
# invoiced = 5, {'label': 'Invoiced'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment