Skip to content

Instantly share code, notes, and snippets.

@zopieux
Created March 26, 2015 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zopieux/2045aebb0ba3698bcdaa to your computer and use it in GitHub Desktop.
Save zopieux/2045aebb0ba3698bcdaa to your computer and use it in GitHub Desktop.
ChoiceEnum
import enum
class ChoiceEnum(enum.Enum):
@staticmethod
def tr(func):
label_for = classmethod(lambda cls, member: _(func(member.name)))
return type('ChoiceEnumTr', (), {'label_for': label_for})
@classmethod
def label_for(cls, member):
return _(member.name)
@classmethod
def choices(cls, empty_label=None):
choices = tuple((m.value, cls.label_for(m)) for m in cls)
if empty_label:
choices = ((None, empty_label),) + choices
return choices
class Type(ChoiceEnum.tr(str.title), ChoiceEnum):
manager = 0
contact = 1
# ugettext_noop("Manager")
# ugettext_noop("Contact")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment