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 as Enum
from enum import auto
class Color(Enum):
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 type hints on the Enum class become properties on
# each value, matching the order in which they are specified
Color.RED.rgb is (1, 0, 0)
Color.GREEN.rgb is (0, 1, 0)
Color.BLUE.rgb is (0, 0, 1)
Color.RED.hex is 'ff0000'
Color.GREEN.hex is '00ff00'
Color.BLUE.hex is '0000ff'
Properties may also be symmetrically mapped to enumeration values using annotated type hints:
import typing as t
from enum_properties import EnumProperties as Enum, Symmetric
from enum import auto
class Color(Enum):
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)) is Color.RED
Color((0, 1, 0)) is Color.GREEN
Color((0, 0, 1)) is Color.BLUE
Color('ff0000') is Color.RED
Color('FF0000') is Color.RED # case_fold makes mapping case insensitive
Color('00ff00') is Color.GREEN
Color('00FF00') is Color.GREEN
Color('0000ff') is Color.BLUE
Color('0000FF') is 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 as Enum, specialize
class SpecializedEnum(Enum):
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
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
Built Distribution
File details
Details for the file enum_properties-2.0.1.tar.gz
.
File metadata
- Download URL: enum_properties-2.0.1.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.2 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a451a069d6cc817649b337d7ebc30970f8d745faff3f5898afa53d9fe8ede3f |
|
MD5 | a7c0e4877689b45dfe72f1c4c96cc191 |
|
BLAKE2b-256 | 3a1e29ed072a664fd33f4a117f1ba2f5e011a68dd6040625a9cfcab216dbf304 |
File details
Details for the file enum_properties-2.0.1-py3-none-any.whl
.
File metadata
- Download URL: enum_properties-2.0.1-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.2 Darwin/23.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28e50d9fc3567a38c55a0c82e9d38dec835654c0a4d26a4adbcec15592036335 |
|
MD5 | 7e4d93c23f7271991f25c056cc6ed3cc |
|
BLAKE2b-256 | c22ceca46697ea68257e2cc7d9c4d9989af4ce7a065ad405bce15268d2817849 |