Buckets:
ktongue/docker_container / simsite /venv /lib /python3.14 /site-packages /django /db /models /enums.py
| import enum | |
| from enum import EnumType, IntEnum, StrEnum | |
| from enum import property as enum_property | |
| from django.utils.functional import Promise | |
| __all__ = ["Choices", "IntegerChoices", "TextChoices"] | |
| class ChoicesType(EnumType): | |
| """A metaclass for creating a enum choices.""" | |
| def __new__(metacls, classname, bases, classdict, **kwds): | |
| labels = [] | |
| for key in classdict._member_names: | |
| value = classdict[key] | |
| if ( | |
| isinstance(value, (list, tuple)) | |
| and len(value) > 1 | |
| and isinstance(value[-1], (Promise, str)) | |
| ): | |
| *value, label = value | |
| value = tuple(value) | |
| else: | |
| label = key.replace("_", " ").title() | |
| labels.append(label) | |
| # Use dict.__setitem__() to suppress defenses against double | |
| # assignment in enum's classdict. | |
| dict.__setitem__(classdict, key, value) | |
| cls = super().__new__(metacls, classname, bases, classdict, **kwds) | |
| for member, label in zip(cls.__members__.values(), labels): | |
| member._label_ = label | |
| return enum.unique(cls) | |
| def names(cls): | |
| empty = ["__empty__"] if hasattr(cls, "__empty__") else [] | |
| return empty + [member.name for member in cls] | |
| def choices(cls): | |
| empty = [(None, cls.__empty__)] if hasattr(cls, "__empty__") else [] | |
| return empty + [(member.value, member.label) for member in cls] | |
| def labels(cls): | |
| return [label for _, label in cls.choices] | |
| def values(cls): | |
| return [value for value, _ in cls.choices] | |
| class Choices(enum.Enum, metaclass=ChoicesType): | |
| """Class for creating enumerated choices.""" | |
| do_not_call_in_templates = enum.nonmember(True) | |
| def label(self): | |
| return self._label_ | |
| # A similar format was proposed for Python 3.10. | |
| def __repr__(self): | |
| return f"{self.__class__.__qualname__}.{self._name_}" | |
| class IntegerChoices(Choices, IntEnum): | |
| """Class for creating enumerated integer choices.""" | |
| pass | |
| class TextChoices(Choices, StrEnum): | |
| """Class for creating enumerated string choices.""" | |
| def _generate_next_value_(name, start, count, last_values): | |
| return name | |
Xet Storage Details
- Size:
- 2.37 kB
- Xet hash:
- 43980463a579ff16d1c8e0dec54f3c7c08e40dfab8f11e5a4414caa6c418fd21
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.