Add properties and method specializations to Python enumeration values with a simple declarative syntax.
Project description
Enum Properties
Add properties to Python enumeration values with a simple declarative syntax. Enum Properties is a lightweight extension to Python's Enum class. Example:
import typing as t
from enum_properties import EnumProperties
from enum import auto
class Color(EnumProperties):
rgb: t.Tuple[int, int, int]
hex: str
# name value rgb hex
RED = auto(), (1, 0, 0), 'ff0000'
GREEN = auto(), (0, 1, 0), '00ff00'
BLUE = auto(), (0, 0, 1), '0000ff'
# the named p() values in the Enum's inheritance become properties on
# each value, matching the order in which they are specified
Color.RED.rgb == (1, 0, 0)
Color.GREEN.rgb == (0, 1, 0)
Color.BLUE.rgb == (0, 0, 1)
Color.RED.hex == 'ff0000'
Color.GREEN.hex == '00ff00'
Color.BLUE.hex == '0000ff'
Properties may also be symmetrically mapped to enumeration values using annotated type hints:
import typing as t
from enum_properties import EnumProperties, Symmetric
from enum import auto
class Color(EnumProperties):
rgb: t.Annotated[t.Tuple[int, int, int], Symmetric()]
hex: t.Annotated[str, Symmetric(case_fold=True)]
RED = auto(), (1, 0, 0), 'ff0000'
GREEN = auto(), (0, 1, 0), '00ff00'
BLUE = auto(), (0, 0, 1), '0000ff'
# Enumeration instances may be instantiated from any Symmetric property
# values. Use case_fold for case insensitive matching
Color((1, 0, 0)) == Color.RED
Color((0, 1, 0)) == Color.GREEN
Color((0, 0, 1)) == Color.BLUE
Color('ff0000') == Color.RED
Color('FF0000') == Color.RED # case_fold makes mapping case insensitive
Color('00ff00') == Color.GREEN
Color('00FF00') == Color.GREEN
Color('0000ff') == Color.BLUE
Color('0000FF') == Color.BLUE
Color.RED.hex == 'ff0000'
Member functions may also be specialized to each enumeration value, using the @specialize
decorator.
from enum_properties import EnumProperties, specialize
class SpecializedEnum(EnumProperties):
ONE = 1
TWO = 2
THREE = 3
@specialize(ONE)
def method(self):
return 'method_one()'
@specialize(TWO)
def method(self):
return 'method_two()'
@specialize(THREE)
def method(self):
return 'method_three()'
SpecializedEnum.ONE.method() == 'method_one()'
SpecializedEnum.TWO.method() == 'method_two()'
SpecializedEnum.THREE.method() == 'method_three()'
Please report bugs and discuss features on the issues page.
Contributions are encouraged!
Full documentation at read the docs.
Installation
- Clone enum-properties from GitHub_ or install a release off PyPI_ :
pip install enum-properties
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
enum_properties-2.0.0.tar.gz
(11.7 kB
view hashes)
Built Distribution
Close
Hashes for enum_properties-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 765f2f67a8d348d56b299b5d77619d36541c14990d36e27627f27ac5c355cd47 |
|
MD5 | 518225757b40cacd19c2ab2f213bf433 |
|
BLAKE2b-256 | 7dfa824f4a925d034fcace8bcd3da19848f8973975640a4eb90c0823d37c9984 |