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 type hints on the Enum class become properties on
# each value, matching the order in which they are specified
assert Color.RED.rgb == (1, 0, 0)
assert Color.GREEN.rgb == (0, 1, 0)
assert Color.BLUE.rgb == (0, 0, 1)
assert Color.RED.hex == 'ff0000'
assert Color.GREEN.hex == '00ff00'
assert 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
assert Color((1, 0, 0)) is Color.RED
assert Color((0, 1, 0)) is Color.GREEN
assert Color((0, 0, 1)) is Color.BLUE
assert Color('ff0000') is Color.RED
assert Color('FF0000') is Color.RED # case_fold makes mapping case insensitive
assert Color('00ff00') is Color.GREEN
assert Color('00FF00') is Color.GREEN
assert Color('0000ff') is Color.BLUE
assert Color('0000FF') is Color.BLUE
assert 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()'
assert SpecializedEnum.ONE.method() == 'method_one()'
assert SpecializedEnum.TWO.method() == 'method_two()'
assert 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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file enum_properties-2.4.1.tar.gz.
File metadata
- Download URL: enum_properties-2.4.1.tar.gz
- Upload date:
- Size: 157.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa1249752b16f385e42ed766254188a7663dc02db263dc74fe42c6d53feee815
|
|
| MD5 |
22f552f6ae587da6dd0d52ab09731f13
|
|
| BLAKE2b-256 |
670dab3faa0f4ae9db7495f25f5bae516f9283acf4c71572c6ac2fb54dafd835
|
Provenance
The following attestation bundles were made for enum_properties-2.4.1.tar.gz:
Publisher:
release.yml on bckohan/enum-properties
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
enum_properties-2.4.1.tar.gz -
Subject digest:
fa1249752b16f385e42ed766254188a7663dc02db263dc74fe42c6d53feee815 - Sigstore transparency entry: 686071321
- Sigstore integration time:
-
Permalink:
bckohan/enum-properties@c5dddb3c0ab86b67f07611441d8441de4184bf53 -
Branch / Tag:
refs/tags/v2.4.1 - Owner: https://github.com/bckohan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c5dddb3c0ab86b67f07611441d8441de4184bf53 -
Trigger Event:
push
-
Statement type:
File details
Details for the file enum_properties-2.4.1-py3-none-any.whl.
File metadata
- Download URL: enum_properties-2.4.1-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
314ce061f24137378d6dce11f1050176e117f76e9c16b2af0240269a1dd470c8
|
|
| MD5 |
595ccf71c05d2e46f33496fa7072096a
|
|
| BLAKE2b-256 |
dfef5518246f0a60be8b04af14d999b12a77a77266834f2a4e97771b9d98e932
|
Provenance
The following attestation bundles were made for enum_properties-2.4.1-py3-none-any.whl:
Publisher:
release.yml on bckohan/enum-properties
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
enum_properties-2.4.1-py3-none-any.whl -
Subject digest:
314ce061f24137378d6dce11f1050176e117f76e9c16b2af0240269a1dd470c8 - Sigstore transparency entry: 686071322
- Sigstore integration time:
-
Permalink:
bckohan/enum-properties@c5dddb3c0ab86b67f07611441d8441de4184bf53 -
Branch / Tag:
refs/tags/v2.4.1 - Owner: https://github.com/bckohan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c5dddb3c0ab86b67f07611441d8441de4184bf53 -
Trigger Event:
push
-
Statement type: