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.0.2.tar.gz
(8.2 kB
view details)
Built Distribution
File details
Details for the file enum-properties-1.0.2.tar.gz
.
File metadata
- Download URL: enum-properties-1.0.2.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.7.12 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e04fefb679c971548dbd0cbfb3758b2b1fef9814efa5292c85307e2c8d521462 |
|
MD5 | cf6331e8f70c5e6cafc1eb8032062d1e |
|
BLAKE2b-256 | 28f283973f41e328b4a4efe7c2dd8764ac356fd59f06d51161343d1a937e1448 |
File details
Details for the file enum_properties-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: enum_properties-1.0.2-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.7.12 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8184a5f2ce8e03de7148190f6837f2f729890f0ab3af231fcbfee246177e622 |
|
MD5 | 881116fe6503fb747cdbb3a563009980 |
|
BLAKE2b-256 | fb96d0551bac62c485aafb560c7553b3794a55d6e4776190b8a8f663ac26aa5f |