Name | enumerific JSON |
Version |
1.0.0
JSON |
| download |
home_page | None |
Summary | Simplifies working with Python enums. |
upload_time | 2025-01-17 06:48:21 |
maintainer | None |
docs_url | None |
author | Daniel Sissman |
requires_python | >=3.9 |
license | MIT License Copyright © 2024–2025 Daniel Sissman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
enum
enumeration
enumerations
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 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](https://www.docker.com) image is created within which the tests are run. To run the unit tests, ensure Docker and Docker Compose is [installed](https://docs.docker.com/engine/install/), 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](https://docs.pytest.org/en/latest/) regarding available optional command line arguments.
### Copyright & License Information
Copyright © 2024–2025 Daniel Sissman; Licensed under the MIT License.
Raw data
{
"_id": null,
"home_page": null,
"name": "enumerific",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "enum, enumeration, enumerations",
"author": "Daniel Sissman",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/27/5e/3fbee6069ee1667f5f5d5c4a102179781e96dd04858883d9101497ff8c9e/enumerific-1.0.0.tar.gz",
"platform": null,
"description": "# Enumerific Enums\n\nThe `enumerific` library provides several useful extensions to the Python built-in `enums` library.\n\n### Requirements\n\nThe 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.\n\n### Installation\n\nThe 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:\n\n\t$ pip install enumerific\n\n### Usage\n\nTo use the Enumerific library, simply import the library and use it like you would the built-in `enum` library as a drop-in replacement:\n\n```\nimport enumerific\n\nclass MyEnum(enumerific.Enum):\n Option1 = \"ABC\"\n Option2 = \"DEF\"\n\nval = MyEnum.Option1\n```\n\nYou can also import the `Enum` class directly from the `enumerific` library and use it directly:\n\n```\nfrom enumerific import Enum\n\nclass MyEnum(Enum):\n Option1 = \"ABC\"\n ...\n```\n\nThe 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:\n\n* `reconcile(value: object, default: Enum = None, raises: bool = False) -> Enum` \u2013 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.\n* `validate(value: object) -> bool` \u2013 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.\n* `options() -> list[Enum]` \u2013 The `options` method provides easy access to the list of the enumeration's available options.\n\nThe 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.\n\nSome examples of use include the following code samples, where each make use of the example `MyEnum` class, defined as follows:\n\n```\nfrom enumerific import Enum\n\nclass MyEnum(Enum):\n Option1 = \"ABC\"\n Option2 = \"DEF\"\n```\n\n#### Example 1: Reconciling a Value\n\n```\n# Given a string value in this case\nvalue = \"ABC\"\n\n# Reconcile it to the associated enumeration option\nvalue = MyEnum.reconcile(value)\n\nassert value == MyEnum.Option1 # asserts successfully\nassert value is MyEnum.Option1 # asserts successfully as enums are singletons\n```\n\n#### Example 2: Reconciling an Enumeration Option Name\n\n```\n# Given a string value in this case\nvalue = \"Option1\"\n\n# Reconcile it to the associated enumeration option\nvalue = MyEnum.reconcile(value)\n\nassert value == MyEnum.Option1 # asserts successfully\nassert value is MyEnum.Option1 # asserts successfully as enums are singletons\n```\n\n#### Example 3: Validating a Value\n\n```\n# The value can be an enumeration option's name, its value, or the enumeration option\nvalue = \"Option1\"\nvalue = \"ABC\"\nvalue = MyEnum.Option1\n\nif MyEnum.validate(value) is True:\n # do something if the value could be validated\nelse:\n # do something else if the value could not be validated\n```\n\n#### Example 4: Iterating Over Enumeration Options\n\n```\nfor option in MyEnum.options():\n # do something with each option\n print(option.name, option.value)\n```\n\n### Unit Tests\n\nThe 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`.\n\nTo ensure that the unit tests are run within a predictable runtime environment where all of the necessary dependencies are available, a [Docker](https://www.docker.com) image is created within which the tests are run. To run the unit tests, ensure Docker and Docker Compose is [installed](https://docs.docker.com/engine/install/), and perform the following commands, which will build the Docker image via `docker compose build` and then run the tests via `docker compose run` \u2013 the output of running the tests will be displayed:\n\n```\n$ docker compose build\n$ docker compose run tests\n```\n\nTo 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:\n\n```\n$ docker compose run tests -vv\n```\n\nSee the documentation for [PyTest](https://docs.pytest.org/en/latest/) regarding available optional command line arguments.\n\n### Copyright & License Information\n\nCopyright \u00a9 2024\u20132025 Daniel Sissman; Licensed under the MIT License.\n",
"bugtrack_url": null,
"license": "MIT License Copyright \u00a9 2024\u20132025 Daniel Sissman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
"summary": "Simplifies working with Python enums.",
"version": "1.0.0",
"project_urls": {
"Changelog": "https://github.com/bluebinary/enumerific/blob/main/CHANGELOG.md",
"Documentation": "https://github.com/bluebinary/enumerific/blob/main/README.md",
"Issues": "https://github.com/bluebinary/enumerific/issues",
"Repository": "https://github.com/bluebinary/enumerific"
},
"split_keywords": [
"enum",
" enumeration",
" enumerations"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "92cac19245022e9d3dcd7a2e5a277e98fb8d6b96a565c006fdc814343c92e3e6",
"md5": "effcc104b19c69d73535a8e8313e0d05",
"sha256": "92e81481a72fb398eb1e1571181d9c9f55dc76abf40b4ca3af263d9f4b9fc099"
},
"downloads": -1,
"filename": "enumerific-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "effcc104b19c69d73535a8e8313e0d05",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 6219,
"upload_time": "2025-01-17T06:48:19",
"upload_time_iso_8601": "2025-01-17T06:48:19.532581Z",
"url": "https://files.pythonhosted.org/packages/92/ca/c19245022e9d3dcd7a2e5a277e98fb8d6b96a565c006fdc814343c92e3e6/enumerific-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "275e3fbee6069ee1667f5f5d5c4a102179781e96dd04858883d9101497ff8c9e",
"md5": "f614c39ca7eac797bac2cf7e9f3e49ca",
"sha256": "1d703bbee69957bdd32594e162265da7a0c3d178f6d66aa9d2f47b1d5ff8e5b3"
},
"downloads": -1,
"filename": "enumerific-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "f614c39ca7eac797bac2cf7e9f3e49ca",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 7304,
"upload_time": "2025-01-17T06:48:21",
"upload_time_iso_8601": "2025-01-17T06:48:21.737988Z",
"url": "https://files.pythonhosted.org/packages/27/5e/3fbee6069ee1667f5f5d5c4a102179781e96dd04858883d9101497ff8c9e/enumerific-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-01-17 06:48:21",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bluebinary",
"github_project": "enumerific",
"github_not_found": true,
"lcname": "enumerific"
}