Reference¶
Module¶
███████╗███╗ ██╗██╗ ██╗███╗ ███╗
██╔════╝████╗ ██║██║ ██║████╗ ████║
█████╗ ██╔██╗ ██║██║ ██║██╔████╔██║
██╔══╝ ██║╚██╗██║██║ ██║██║╚██╔╝██║
███████╗██║ ╚████║╚██████╔╝██║ ╚═╝ ██║
╚══════╝╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝
██████╗ ██████╗ ██████╗ ██████╗ ███████╗██████╗ ████████╗██╗███████╗███████╗
██╔══██╗██╔══██╗██╔═══██╗██╔══██╗██╔════╝██╔══██╗╚══██╔══╝██║██╔════╝██╔════╝
██████╔╝██████╔╝██║ ██║██████╔╝█████╗ ██████╔╝ ██║ ██║█████╗ ███████╗
██╔═══╝ ██╔══██╗██║ ██║██╔═══╝ ██╔══╝ ██╔══██╗ ██║ ██║██╔══╝ ╚════██║
██║ ██║ ██║╚██████╔╝██║ ███████╗██║ ██║ ██║ ██║███████╗███████║
╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝
Metaprogramming and mixin tools that implement property tuple and method specialization support for python enumeration classes.
- class enum_properties.DecomposeMixin¶
Bases:
objectA mixin for Flag enumerations that decomposes composite enumeration values and allows us to treat composite enumeration values as iterables of activated flags.
- property flagged¶
Returns the list of flags that are active.
- class enum_properties.EnumProperties(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
SymmetricMixin,EnumUse this base class instead of
enum.Enumto enable enumeration properties. For example:class EnumType( EnumProperties, p('prop1'), s('prop1', case_fold=True) # case insensitive symmetric property ): VAL1 = 1, "prop1's value1", "prop2's value1" VAL2 = 2, "prop1's value2", "prop2's value2" VAL3 = 3, "prop1's value3", "prop2's value3"
This is a shortcut for:
class EnumType(SymmetricMixin, Enum, metaclass=EnumPropertiesMeta): ...
See How To for more details.
- __first_class_members__: list[str] = []¶
The list of first class members - this includes all members and aliases. May be overridden.
- _ep_coerce_types_: list[type[Any]] = []¶
On instantiation, if _missing_ is invoked a coercion attempt will be made to each of these types before failure.
- _ep_isymmetric_map_: dict[str, Enum] = {}¶
The case insensitive mapping of symmetric values to enumeration values.
- class enum_properties.EnumPropertiesMeta(classname, bases, classdict, **kwargs)¶
Bases:
EnumTypeA metaclass for creating enum choices with additional named properties for each value. An
enum.Enumcan be given property support simply by:import enum from enum_properties import EnumPropertiesMeta class MyEnum(enum.Enum, metaclass=EnumPropertiesMeta): ...
To support symmetrical properties, add the SymmetricMixin:
import enum from enum_properties import ( EnumPropertiesMeta, SymmetricMixin ) class MyEnum(SymmetricMixin, enum.Enum, metaclass=EnumPropertiesMeta): ...
All
enum.Enumfunctionality is compatible with the EnumPropertiesMeta metaclass. This class works by stripping out thep()ands()values during__prepare__and using their class name’s as expected property values to set the appropriate values to properties in__new__.- classmethod __prepare__(cls, bases, **kwds)¶
Strip properties out of inheritance and record them on our class dictionary for per-value based assignment in
__new__.- Raises:
ValueError – If a reserved name is present as either a property or a member, or if the number of specified properties does not match the number of listed property values in the value tuples.
- EXPECTED = ['_symmetric_builtins_']¶
- RESERVED = ['_properties_', '_num_sym_props_', '_ep_coerce_types_', '_ep_symmetric_map_', '_ep_isymmetric_map_']¶
- __call__(value, names=None, *, module=None, qualname=None, type=None, start=1, properties=None, **kwargs)¶
Overrides
enum.EnumMeta.__call__()to support the functional API with apropertiesargument. Whennamesis provided along withproperties, a new enum class is created with the given properties.propertiesmay be an iterable of:str– creates a non-symmetric property with that namep()ors()type – used directly (preserves case-fold / match-none options on symmetric properties)
Example:
AnEnum = EnumProperties( "AnEnum", {"A": ("a", True), "B": ("b", False)}, properties=("prop",), )
- Parameters:
value – Class name when using the functional API, otherwise the member value to look up.
names – Member definitions (dict, list of pairs, or string).
module – Module name for the new class.
qualname – Qualified name for the new class.
type – An optional mixin type for the new class.
start – Starting value for auto-generated member values.
properties – Property specifications for the functional API.
- class enum_properties.FlagProperties(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
DecomposeMixin,SymmetricMixin,FlagA Flag that supports properties
- __first_class_members__: list[str] = []¶
The list of first class members - this includes all members and aliases. May be overridden.
- _all_bits_ = 0¶
- _boundary_ = 'strict'¶
- _ep_coerce_types_: list[type[Any]] = []¶
On instantiation, if _missing_ is invoked a coercion attempt will be made to each of these types before failure.
- _ep_isymmetric_map_: dict[str, Enum] = {}¶
The case insensitive mapping of symmetric values to enumeration values.
- _ep_symmetric_map_: dict[Any, Enum] = {}¶
The case sensitive mapping of symmetric values to enumeration values.
- _flag_mask_ = 0¶
- static _generate_next_value_(name, start, count, last_values)¶
Intermixed property tuples can corrupt the last_values list with tuples. This method ensures only ints are present in last_values and delegates to the super class.
- _inverted_ = None¶
- _singles_mask_ = 0¶
- class enum_properties.IntEnumProperties(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
SymmetricMixin,IntEnumAn IntEnum that supports properties.
Note
Because SymmetricMixin implements __eq__ we also need an implementation of __hash__ on our class to keep it hashable We walk the mro to find the first implementation of __hash__ and use it - this keeps our implementation hash equivalent to Enum/IntEnum behavior
- __first_class_members__: list[str] = []¶
The list of first class members - this includes all members and aliases. May be overridden.
- _ep_coerce_types_: list[type[Any]] = []¶
On instantiation, if _missing_ is invoked a coercion attempt will be made to each of these types before failure.
- _ep_isymmetric_map_: dict[str, Enum] = {}¶
The case insensitive mapping of symmetric values to enumeration values.
- class enum_properties.IntFlagProperties(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
DecomposeMixin,SymmetricMixin,IntFlagAn IntFlag that supports properties.
- __first_class_members__: list[str] = []¶
The list of first class members - this includes all members and aliases. May be overridden.
- _all_bits_ = 0¶
- _boundary_ = 'keep'¶
- _ep_coerce_types_: list[type[Any]] = []¶
On instantiation, if _missing_ is invoked a coercion attempt will be made to each of these types before failure.
- _ep_isymmetric_map_: dict[str, Enum] = {}¶
The case insensitive mapping of symmetric values to enumeration values.
- _ep_symmetric_map_: dict[Any, Enum] = {}¶
The case sensitive mapping of symmetric values to enumeration values.
- _flag_mask_ = 0¶
- static _generate_next_value_(name, start, count, last_values)¶
Intermixed property tuples can corrupt the last_values list with tuples. This method ensures only ints are present in last_values and delegates to the super class.
- _inverted_ = None¶
- _singles_mask_ = 0¶
- class enum_properties.StrEnumProperties(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
SymmetricMixin,StrEnumAn StrEnum that supports properties.
- __first_class_members__: list[str] = []¶
The list of first class members - this includes all members and aliases. May be overridden.
- _ep_coerce_types_: list[type[Any]] = []¶
On instantiation, if _missing_ is invoked a coercion attempt will be made to each of these types before failure.
- _ep_isymmetric_map_: dict[str, Enum] = {}¶
The case insensitive mapping of symmetric values to enumeration values.
- _ep_symmetric_map_: dict[Any, Enum] = {}¶
The case sensitive mapping of symmetric values to enumeration values.
- static _generate_next_value_(name, start, count, last_values)¶
Return the lower-cased version of the member name.
- class enum_properties.Symmetric(case_fold: bool = False, match_none: bool = False)¶
Bases:
objectA dataclass to hold symmetric property options.
- class enum_properties.SymmetricMixin¶
Bases:
objectThis mixin enables symmetric
enum.Enumcreation from properties marked symmetric. It is included by default in theEnumPropertiesbase class, but can be disabled by overriding_missing_and explicitly skipping it.If an enumeration type inherits builtin properties (e.g. name), those properties can be made symmetric by supplying a
_symmetric_builtins_member containing a list of string property names ors()values. By default, thenameproperty will be a case sensitive symmetric property.- __first_class_members__: list[str]¶
The list of first class members - this includes all members and aliases. May be overridden.
- _ep_coerce_types_: list[type[Any]]¶
On instantiation, if _missing_ is invoked a coercion attempt will be made to each of these types before failure.
- _ep_isymmetric_map_: dict[str, Enum]¶
The case insensitive mapping of symmetric values to enumeration values.
- _ep_symmetric_map_: dict[Any, Enum]¶
The case sensitive mapping of symmetric values to enumeration values.
- classmethod _missing_(value: Any) Any¶
Arbitrary types can be mapped to enumeration values so long as they are hashable. Coercion to all possible types must be attempted on value, in priority order before failure.
- Parameters:
value – The value (possibly wrapped) to attempt coercion to our enumeration type.
- Raises:
ValueError – if no enumeration match can be found.
- Returns:
A valid instance of this enumeration
- enum_properties.p(prop_name: str) type[_Prop]¶
Add a property of the given name to the enumeration class by inheritance. Properties must be specified in the order in which they appear in the value tuple specification. This call works by constructing a new type with a classname that corresponds to the property name. The class inherits from str and can be instantiated as a string by calling its empty constructor.
- Parameters:
prop_name – The name of the property
- Returns:
a named property class
- enum_properties.s(prop_name: str, case_fold: bool = False, match_none: bool = False) type[_SProp]¶
Add a symmetric property. Enumeration values will be coercible from this property’s values.
- Parameters:
prop_name – The name of the property
case_fold – If False, symmetric lookup will be case sensitive (default)
match_none – If True, none values will be symmetric, if False (default), none values for symmetric properties will not map back to the enumeration value.
- Returns:
a named symmetric property class
- enum_properties.specialize(*values)¶
A decorator to specialize a method for a given enumeration value.
- Parameters:
values – The enumeration value(s) to specialize
- Returns:
A decorated specialized member method
- enum_properties.symmetric(case_fold: bool = False, match_none: bool = False)¶
A decorator that marks non-enum value members as symmetric. Plain functions are automatically wrapped with
property(). For example:class MyEnum(EnumProperties): ... @symmetric(case_fold=True) def name(self): return "value"
- Parameters:
case_fold – Pass True to make the property case insensitive
match_none – Pass True to make None values symmetric
- Returns:
A decorated specialized member method