Skip to main content

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.9, 3.10, 3.11, 3.12 and 3.13 but may work with some earlier versions such as 3.8, but has not been tested against this version or any earlier. The library is not compatible with Python 2.* or earlier.

Installation

The Enumerific library is available from PyPi, so may be added to a project's dependencies via its requirements.txt file or similar by referencing the Enumerific library's name, enumerific, or the library may be installed directly into your local runtime environment using pip install 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:

import enumerific

class MyEnum(enumerific.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

# 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

# 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

# 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
else:
    # do something else if the value could not be validated

Example 4: Iterating Over Enumeration Options

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.

Release files for enumerific 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for enumerific 1.0.0
File Size Uploaded
enumerific-1.0.0.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for enumerific 1.0.0
File Interpreter ABI Platform
enumerific-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.5 kB

Release files / enumerific-1.0.0.tar.gz

Download URL enumerific-1.0.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1d703bbee69957bdd32594e162265da7a0c3d178f6d66aa9d2f47b1d5ff8e5b3
BLAKE2b-256 checksum
How to use checksums
275e3fbee6069ee1667f5f5d5c4a102179781e96dd04858883d9101497ff8c9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 17, 2025.

Transparency log

Release files / enumerific-1.0.0-py3-none-any.whl

Download URL enumerific-1.0.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
92e81481a72fb398eb1e1571181d9c9f55dc76abf40b4ca3af263d9f4b9fc099
BLAKE2b-256 checksum
How to use checksums
92cac19245022e9d3dcd7a2e5a277e98fb8d6b96a565c006fdc814343c92e3e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 17, 2025.

Transparency log

Release history Release notifications | RSS feed

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page