# Enum Properties
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![PyPI version](https://badge.fury.io/py/enum-properties.svg)](https://pypi.python.org/pypi/enum-properties/)
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/enum-properties.svg)](https://pypi.python.org/pypi/enum-properties/)
[![PyPI status](https://img.shields.io/pypi/status/enum-properties.svg)](https://pypi.python.org/pypi/enum-properties)
[![Documentation Status](https://readthedocs.org/projects/enum-properties/badge/?version=latest)](http://enum-properties.readthedocs.io/?badge=latest/)
[![Code Cov](https://codecov.io/gh/bckohan/enum-properties/branch/main/graph/badge.svg?token=0IZOKN2DYL)](https://codecov.io/gh/bckohan/enum-properties)
[![Test Status](https://github.com/bckohan/enum-properties/workflows/test/badge.svg)](https://github.com/bckohan/enum-properties/actions/workflows/test.yml)
[![Lint Status](https://github.com/bckohan/enum-properties/workflows/lint/badge.svg)](https://github.com/bckohan/enum-properties/actions/workflows/lint.yml)
Add properties to Python enumeration values with a simple declarative syntax. [Enum Properties](https://enum-properties.readthedocs.io/en/latest) is a lightweight extension to [Python's Enum class](https://docs.python.org/3/library/enum.html). Example:
```python
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:
```python
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.
```python
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](https://github.com/bckohan/enum-properties/issues).
[Contributions](https://github.com/bckohan/enum-properties/blob/main/CONTRIBUTING.rst) are encouraged!
[Full documentation at read the docs.](https://enum-properties.readthedocs.io/en/latest)
## Installation
1. Clone enum-properties from [GitHub](https://github.com/bckohan/enum-properties) or install a release off [PyPI](https://pypi.org/project/enum-properties/):
```bash
pip install enum-properties
```
Raw data
{
"_id": null,
"home_page": "https://enum-properties.readthedocs.io",
"name": "enum-properties",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": "enum, properties, defines, field, dataclass, dataclasses",
"author": "Brian Kohan",
"author_email": "bckohan@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/3a/1e/29ed072a664fd33f4a117f1ba2f5e011a68dd6040625a9cfcab216dbf304/enum_properties-2.0.1.tar.gz",
"platform": null,
"description": "# Enum Properties\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![PyPI version](https://badge.fury.io/py/enum-properties.svg)](https://pypi.python.org/pypi/enum-properties/)\n[![PyPI pyversions](https://img.shields.io/pypi/pyversions/enum-properties.svg)](https://pypi.python.org/pypi/enum-properties/)\n[![PyPI status](https://img.shields.io/pypi/status/enum-properties.svg)](https://pypi.python.org/pypi/enum-properties)\n[![Documentation Status](https://readthedocs.org/projects/enum-properties/badge/?version=latest)](http://enum-properties.readthedocs.io/?badge=latest/)\n[![Code Cov](https://codecov.io/gh/bckohan/enum-properties/branch/main/graph/badge.svg?token=0IZOKN2DYL)](https://codecov.io/gh/bckohan/enum-properties)\n[![Test Status](https://github.com/bckohan/enum-properties/workflows/test/badge.svg)](https://github.com/bckohan/enum-properties/actions/workflows/test.yml)\n[![Lint Status](https://github.com/bckohan/enum-properties/workflows/lint/badge.svg)](https://github.com/bckohan/enum-properties/actions/workflows/lint.yml)\n\nAdd properties to Python enumeration values with a simple declarative syntax. [Enum Properties](https://enum-properties.readthedocs.io/en/latest) is a lightweight extension to [Python's Enum class](https://docs.python.org/3/library/enum.html). Example:\n\n```python\n\n import typing as t\n from enum_properties import EnumProperties as Enum\n from enum import auto\n\n class Color(Enum):\n\n rgb: t.Tuple[int, int, int]\n hex: str\n\n # name value rgb hex\n RED = auto(), (1, 0, 0), 'ff0000'\n GREEN = auto(), (0, 1, 0), '00ff00'\n BLUE = auto(), (0, 0, 1), '0000ff'\n\n # the type hints on the Enum class become properties on\n # each value, matching the order in which they are specified\n\n Color.RED.rgb is (1, 0, 0)\n Color.GREEN.rgb is (0, 1, 0)\n Color.BLUE.rgb is (0, 0, 1)\n\n Color.RED.hex is 'ff0000'\n Color.GREEN.hex is '00ff00'\n Color.BLUE.hex is '0000ff'\n\n```\n\nProperties may also be symmetrically mapped to enumeration values using annotated type hints:\n\n```python\n\n import typing as t\n from enum_properties import EnumProperties as Enum, Symmetric\n from enum import auto\n\n class Color(Enum):\n\n rgb: t.Annotated[t.Tuple[int, int, int], Symmetric()]\n hex: t.Annotated[str, Symmetric(case_fold=True)]\n\n RED = auto(), (1, 0, 0), 'ff0000'\n GREEN = auto(), (0, 1, 0), '00ff00'\n BLUE = auto(), (0, 0, 1), '0000ff'\n\n # Enumeration instances may be instantiated from any Symmetric property\n # values. Use case_fold for case insensitive matching\n\n Color((1, 0, 0)) is Color.RED\n Color((0, 1, 0)) is Color.GREEN\n Color((0, 0, 1)) is Color.BLUE\n\n Color('ff0000') is Color.RED\n Color('FF0000') is Color.RED # case_fold makes mapping case insensitive\n Color('00ff00') is Color.GREEN\n Color('00FF00') is Color.GREEN\n Color('0000ff') is Color.BLUE\n Color('0000FF') is Color.BLUE\n\n Color.RED.hex == 'ff0000'\n\n```\n\nMember functions may also be specialized to each enumeration value, using the ``@specialize`` decorator.\n\n```python\n\n from enum_properties import EnumProperties as Enum, specialize\n\n class SpecializedEnum(Enum):\n\n ONE = 1\n TWO = 2\n THREE = 3\n\n @specialize(ONE)\n def method(self):\n return 'method_one()'\n\n @specialize(TWO)\n def method(self):\n return 'method_two()'\n\n @specialize(THREE)\n def method(self):\n return 'method_three()'\n\n SpecializedEnum.ONE.method() == 'method_one()'\n SpecializedEnum.TWO.method() == 'method_two()'\n SpecializedEnum.THREE.method() == 'method_three()'\n\n```\n\nPlease report bugs and discuss features on the [issues page](https://github.com/bckohan/enum-properties/issues).\n\n[Contributions](https://github.com/bckohan/enum-properties/blob/main/CONTRIBUTING.rst) are encouraged!\n\n[Full documentation at read the docs.](https://enum-properties.readthedocs.io/en/latest)\n\n## Installation\n\n1. Clone enum-properties from [GitHub](https://github.com/bckohan/enum-properties) or install a release off [PyPI](https://pypi.org/project/enum-properties/):\n\n```bash\n pip install enum-properties\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Add properties and method specializations to Python enumeration values with a simple declarative syntax.",
"version": "2.0.1",
"project_urls": {
"Homepage": "https://enum-properties.readthedocs.io",
"Repository": "https://github.com/bckohan/enum-properties"
},
"split_keywords": [
"enum",
" properties",
" defines",
" field",
" dataclass",
" dataclasses"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c22ceca46697ea68257e2cc7d9c4d9989af4ce7a065ad405bce15268d2817849",
"md5": "7e4d93c23f7271991f25c056cc6ed3cc",
"sha256": "28e50d9fc3567a38c55a0c82e9d38dec835654c0a4d26a4adbcec15592036335"
},
"downloads": -1,
"filename": "enum_properties-2.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7e4d93c23f7271991f25c056cc6ed3cc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 10608,
"upload_time": "2024-09-03T19:48:04",
"upload_time_iso_8601": "2024-09-03T19:48:04.747030Z",
"url": "https://files.pythonhosted.org/packages/c2/2c/eca46697ea68257e2cc7d9c4d9989af4ce7a065ad405bce15268d2817849/enum_properties-2.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3a1e29ed072a664fd33f4a117f1ba2f5e011a68dd6040625a9cfcab216dbf304",
"md5": "a7c0e4877689b45dfe72f1c4c96cc191",
"sha256": "0a451a069d6cc817649b337d7ebc30970f8d745faff3f5898afa53d9fe8ede3f"
},
"downloads": -1,
"filename": "enum_properties-2.0.1.tar.gz",
"has_sig": false,
"md5_digest": "a7c0e4877689b45dfe72f1c4c96cc191",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 11724,
"upload_time": "2024-09-03T19:48:06",
"upload_time_iso_8601": "2024-09-03T19:48:06.424619Z",
"url": "https://files.pythonhosted.org/packages/3a/1e/29ed072a664fd33f4a117f1ba2f5e011a68dd6040625a9cfcab216dbf304/enum_properties-2.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-03 19:48:06",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bckohan",
"github_project": "enum-properties",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "enum-properties"
}