Skip to main content

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

Project description

MIT license PyPI version fury.io PyPI pyversions PyPI status Documentation Status Code Cov Test Status

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

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

Uploaded Source

Built Distribution

enum_properties-1.3.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file enum-properties-1.3.1.tar.gz.

File metadata

  • Download URL: enum-properties-1.3.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.7.12 Darwin/21.4.0

File hashes

Hashes for enum-properties-1.3.1.tar.gz
Algorithm Hash digest
SHA256 41b4cd9c88810a069b209db409288839deec33cf54b8df0ef21ea0504613863a
MD5 92c82aad0e9d4f94c7a8a51617e2722d
BLAKE2b-256 37b5d84f3ad180b04acbb8f2f22227206a57987ba5a4c548cb0ae6f711054a7b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: enum_properties-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.7.12 Darwin/21.4.0

File hashes

Hashes for enum_properties-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98a292416ce34a268843081e6d8275a49b416a5554a738f4fc1b91e9c6057655
MD5 580295d986f2117488a43624106f2736
BLAKE2b-256 69dcdb4f01f37ebcd85ee8b3179164dbecc80131d653b7710ed3dcb70498d14e

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