Add properties 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:
from enum_properties import EnumProperties, p
from enum import auto
class Color(EnumProperties, p('rgb'), p('hex')):
# 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 s() values:
from enum_properties import EnumProperties, s
from enum import auto
class Color(EnumProperties, s('rgb'), s('hex', case_fold=True)):
RED = auto(), (1, 0, 0), 'ff0000'
GREEN = auto(), (0, 1, 0), '00ff00'
BLUE = auto(), (0, 0, 1), '0000ff'
# any named s() values in the Enum's inheritance become properties on
# each value, and the enumeration value may be instantiated from the
# property's value
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'
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
enum_properties-1.3.3.tar.gz
(9.2 kB
view details)
Built Distribution
File details
Details for the file enum_properties-1.3.3.tar.gz
.
File metadata
- Download URL: enum_properties-1.3.3.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.7.12 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b27f1a6bb20214f503b5d0987aaf2cf18bd435c532f8cb48c938eed70ec6c6c3 |
|
MD5 | 20a9c2880ab48c3fb8232162c6d29902 |
|
BLAKE2b-256 | ca82a89cc99df047b5051ea92150369ca0e32443a7c71b4703baeea7995815d0 |
File details
Details for the file enum_properties-1.3.3-py3-none-any.whl
.
File metadata
- Download URL: enum_properties-1.3.3-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.7.12 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b965706176e3d1313e35700026d5e99253ecc35de8d1d38b743acb3cbd97d5f |
|
MD5 | d8e5bf613580d37f7066cf016af869c2 |
|
BLAKE2b-256 | 301583cfefcc4d74726906591f057c8389b954791aec6298c07af5e46d2b48f2 |