Skip to main content

Simplifies working with Python enums.

Project description

Enumerific Enums

The enumerific library provides several useful extensions to the Python built-in enums library.

Requirements

The Enumerific library has been tested with Python 3.10, 3.11, 3.12 and 3.13, and is not compatible with Python 3.9 or earlier.

Installation

Enumerific is available from the PyPI, so may be added to a project's dependencies via its requirements.txt file or similar by referencing the library's name, enumerific, or the library may be installed directly into your local runtime environment using pip by entering the following command, and following any prompts:

$ pip install enumerific

Usage

To use the Enumerific library, simply import the library and use it like you would the built-in enum library as a drop-in replacement:

from enumerific import Enum

class MyEnum(Enum):
  Option1 = "ABC"
  Option2 = "DEF"

val = MyEnum.Option1

Alternatively, to make use of the extra functionality for the standard library's Enum class, import the Enum class from the Enumerific library:

from enumerific import Enum

class MyEnum(Enum):
  Option1 = "ABC"
  Option2 = "DEF"

val = MyEnum.Option1

You can also import the Enum class directly from the enumerific library and use it directly:

from enumerific import Enum

class MyEnum(Enum):
  Option1 = "ABC"

The Enumerific library's own Enum class is a subclass of the built-in enum.Enum class, so all of the built-in functionality of enum.Enum is available, as well as several additional class methods:

  • reconcile(value: object, default: Enum = None, raises: bool = False) -> Enum – The reconcile method allows for an enumeration's value or an enumeration option's name to be reconciled against a matching enumeration option. If the provided value can be matched against one of the enumeration's available options, that option will be returned, otherwise there are two possible behaviours: if the raises keyword argument has been set to or left as False (its default), the value assigned to the default keyword argument will be returned, which may be None if no default value has been specified; if the raises argument has been set to True an EnumValueError exception will be raised as an alert that the provided value could not be matched. One can also provide an enumeration option as the input value to the reconcile method, and these will be validated and returned as-is.
  • validate(value: object) -> bool – The validate method takes the same range of input values as the reconcile method, and returns True when the provided value can be reconciled against an enumeration option, or False otherwise.
  • options() -> list[Enum] – The options method provides easy access to the list of the enumeration's available options.

The benefits of being able to validate and reconcile various input values against an enumeration, include allowing for a controlled vocabulary of options to be checked against, and the ability to convert enumeration values into their corresponding enumeration option. This can be especially useful when working with input data where you need to convert those values to their corresponding enumeration options, and to be able to do so without maintaining boilerplate code to perform the matching and assignment.

Some examples of use include the following code samples, where each make use of the example MyEnum class, defined as follows:

from enumerific import Enum

class MyEnum(Enum):
  Option1 = "ABC"
  Option2 = "DEF"

Example 1: Reconciling a Value

from enumerific import Enum

class MyEnum(Enum):
  Option1 = "ABC"
  Option2 = "DEF"

# Given a string value in this case
value = "ABC"

# Reconcile it to the associated enumeration option
value = MyEnum.reconcile(value)

assert value == MyEnum.Option1  # asserts successfully
assert value is MyEnum.Option1  # asserts successfully as enums are singletons

Example 2: Reconciling an Enumeration Option Name

from enumerific import Enum

class MyEnum(Enum):
  Option1 = "ABC"
  Option2 = "DEF"

# Given a string value in this case
value = "Option1"

# Reconcile it to the associated enumeration option
value = MyEnum.reconcile(value)

assert value == MyEnum.Option1  # asserts successfully
assert value is MyEnum.Option1  # asserts successfully as enums are singletons

Example 3: Validating a Value

from enumerific import Enum

class MyEnum(Enum):
  Option1 = "ABC"
  Option2 = "DEF"

# The value can be an enumeration option's name, its value, or the enumeration option
value = "Option1"
value = "ABC"
value = MyEnum.Option1

if MyEnum.validate(value) is True:
    # do something if the value could be validated
    pass
else:
    # do something else if the value could not be validated
    pass

Example 4: Iterating Over Enumeration Options

from enumerific import Enum

class MyEnum(Enum):
  Option1 = "ABC"
  Option2 = "DEF"

for option in MyEnum.options():
    # do something with each option
    print(option.name, option.value)

Unit Tests

The Enumerific library includes a suite of comprehensive unit tests which ensure that the library functionality operates as expected. The unit tests were developed with and are run via pytest.

To ensure that the unit tests are run within a predictable runtime environment where all of the necessary dependencies are available, a Docker image is created within which the tests are run. To run the unit tests, ensure Docker and Docker Compose is installed, and perform the following commands, which will build the Docker image via docker compose build and then run the tests via docker compose run – the output of running the tests will be displayed:

$ docker compose build
$ docker compose run tests

To run the unit tests with optional command line arguments being passed to pytest, append the relevant arguments to the docker compose run tests command, as follows, for example passing -vv to enable verbose output:

$ docker compose run tests -vv

See the documentation for PyTest regarding available optional command line arguments.

Copyright & License Information

Copyright © 2024–2025 Daniel Sissman; licensed under the MIT License.

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

enumerific-1.0.1.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

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

enumerific-1.0.1-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file enumerific-1.0.1.tar.gz.

File metadata

  • Download URL: enumerific-1.0.1.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for enumerific-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8cee5343ba0719ea91a9f464336ee578614f90f64f19bb3f31c6e2df85e3c674
MD5 89ad6bac6a870e4aeee85748d7326176
BLAKE2b-256 dddbe7a68a07835cc47bbdf1671694aecd7eb637a365c47e288416b7e98103ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for enumerific-1.0.1.tar.gz:

Publisher: python-publish.yml on bluebinary/enumerific

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

File details

Details for the file enumerific-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: enumerific-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for enumerific-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c55c9dd23030bbbdbcd0dd3fb443ed593a2acda821c260aa6398f95008878cef
MD5 be874f9b777f48b40a0ebeb3f851615e
BLAKE2b-256 a62a56fd5031f6202831a0cc49b3b0f9f3ec9b4de67df510c3799769410fbc19

See more details on using hashes here.

Provenance

The following attestation bundles were made for enumerific-1.0.1-py3-none-any.whl:

Publisher: python-publish.yml on bluebinary/enumerific

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