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– Thereconcilemethod 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 theraiseskeyword argument has been set to or left asFalse(its default), the value assigned to thedefaultkeyword argument will be returned, which may beNoneif no default value has been specified; if theraisesargument has been set toTrueanEnumValueErrorexception 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 thereconcilemethod, and these will be validated and returned as-is.validate(value: object) -> bool– Thevalidatemethod takes the same range of input values as thereconcilemethod, and returnsTruewhen the provided value can be reconciled against an enumeration option, orFalseotherwise.options() -> list[Enum]– Theoptionsmethod 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cee5343ba0719ea91a9f464336ee578614f90f64f19bb3f31c6e2df85e3c674
|
|
| MD5 |
89ad6bac6a870e4aeee85748d7326176
|
|
| BLAKE2b-256 |
dddbe7a68a07835cc47bbdf1671694aecd7eb637a365c47e288416b7e98103ce
|
Provenance
The following attestation bundles were made for enumerific-1.0.1.tar.gz:
Publisher:
python-publish.yml on bluebinary/enumerific
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
enumerific-1.0.1.tar.gz -
Subject digest:
8cee5343ba0719ea91a9f464336ee578614f90f64f19bb3f31c6e2df85e3c674 - Sigstore transparency entry: 230115728
- Sigstore integration time:
-
Permalink:
bluebinary/enumerific@c50b2a8bfbb84951817acf156c00cbcc62a75e89 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/bluebinary
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c50b2a8bfbb84951817acf156c00cbcc62a75e89 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c55c9dd23030bbbdbcd0dd3fb443ed593a2acda821c260aa6398f95008878cef
|
|
| MD5 |
be874f9b777f48b40a0ebeb3f851615e
|
|
| BLAKE2b-256 |
a62a56fd5031f6202831a0cc49b3b0f9f3ec9b4de67df510c3799769410fbc19
|
Provenance
The following attestation bundles were made for enumerific-1.0.1-py3-none-any.whl:
Publisher:
python-publish.yml on bluebinary/enumerific
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
enumerific-1.0.1-py3-none-any.whl -
Subject digest:
c55c9dd23030bbbdbcd0dd3fb443ed593a2acda821c260aa6398f95008878cef - Sigstore transparency entry: 230115730
- Sigstore integration time:
-
Permalink:
bluebinary/enumerific@c50b2a8bfbb84951817acf156c00cbcc62a75e89 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/bluebinary
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@c50b2a8bfbb84951817acf156c00cbcc62a75e89 -
Trigger Event:
release
-
Statement type: