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 Documentation Status Code Cov Test Status Lint Status

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 and type hints are optional for better dev experience:

    import typing as t
    from enum_properties import EnumProperties, s
    from enum import auto

    class Color(EnumProperties, s('rgb'), s('hex', case_fold=True)):

        rgb: t.Tuple[int, int, int]
        hex: str

        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'

Member functions may also be specialized to each enumeration value, using the @specialize decorator.

    from enum_properties import EnumProperties, specialize

    class SpecializedEnum(EnumProperties):

        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.

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

Uploaded Source

Built Distribution

enum_properties-1.8.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: enum_properties-1.8.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.4 Darwin/23.2.0

File hashes

Hashes for enum_properties-1.8.0.tar.gz
Algorithm Hash digest
SHA256 b028c8f98476f45470ecf8a48d0b47f9bd20dc229c02f6c66a0b6ad234498a46
MD5 80b8be6907dda5b938f6bea11e345117
BLAKE2b-256 89a3db52259b6a26d0ea3dd6080825bc6532c34e958b0267cc38325fcf0d2a02

See more details on using hashes here.

File details

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

File metadata

  • Download URL: enum_properties-1.8.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.4 Darwin/23.2.0

File hashes

Hashes for enum_properties-1.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1d8872fdc55d27dea144abfc74778d3c85ac3219d775366392a10e75f2896808
MD5 7f8a3db85abac3cad09cdf53e68a1902
BLAKE2b-256 52b5c0d1c8bb6a2afb36b989bee34ba076b25957f082295f3f05f0369ab2988e

See more details on using hashes here.

Supported by

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