Skip to main content

Add properties and method specializations to Python enumeration values with a simple declarative syntax.

Project description

Enum Properties

License: MIT Ruff PyPI version PyPI pyversions PyPI status PyPI - Types Documentation Status Code Cov Test Status Lint Status OpenSSF Scorecard OpenSSF Best Practices

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

  1. Clone enum-properties from GitHub or install a release off PyPI:
       pip install enum-properties

Project details


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-2.6.0.tar.gz (176.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

enum_properties-2.6.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file enum_properties-2.6.0.tar.gz.

File metadata

  • Download URL: enum_properties-2.6.0.tar.gz
  • Upload date:
  • Size: 176.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for enum_properties-2.6.0.tar.gz
Algorithm Hash digest
SHA256 825e2a63c8378a45b5d065c1384d5e6ea14428a194297396edf60b2cb267b153
MD5 64a181118f69db1576d2ab14617fcc34
BLAKE2b-256 00ad2d896ddfc4af35eb8671c312acd535ce2da6f6c8318ac826eefe2e32e1eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for enum_properties-2.6.0.tar.gz:

Publisher: release.yml on bckohan/enum-properties

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file enum_properties-2.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for enum_properties-2.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc3905f9c4eefe14c133fbace48e8a428b91e820a88996bbfc5b6e214be3d6ad
MD5 d31942f4cd498d5c59a7c7502f024cfd
BLAKE2b-256 da5e98ce62c8b3e4303acaaa5a2893353a2596fcbd2788457bc12d752186bb34

See more details on using hashes here.

Provenance

The following attestation bundles were made for enum_properties-2.6.0-py3-none-any.whl:

Publisher: release.yml on bckohan/enum-properties

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page