Created
March 26, 2015 22:35
-
-
Save zopieux/2045aebb0ba3698bcdaa to your computer and use it in GitHub Desktop.
ChoiceEnum
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
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