enumerific 0.0.0__py3-none-any.whl → 1.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
enumerific/logging.py ADDED
@@ -0,0 +1,5 @@
1
+ import logging
2
+
3
+ logger = logging.getLogger(__name__)
4
+
5
+ logging.basicConfig(level=logging.WARNING)
enumerific/standard.py ADDED
@@ -0,0 +1,85 @@
1
+ from __future__ import annotations
2
+
3
+ from enum import Enum
4
+
5
+ from enumerific.logging import logger
6
+
7
+ from enumerific.exceptions import EnumValueError
8
+
9
+
10
+ logger = logger.getChild(__name__)
11
+
12
+
13
+ class Enum(Enum):
14
+ """An extended Enum class that provides support for validating an Enum value and
15
+ accepting either enumeration class properties as enumeration values or their string
16
+ names or values, and providing straightforward access to the enumeration values an
17
+ Enum class holds."""
18
+
19
+ @classmethod
20
+ def validate(cls, value: Enum | str | int | object) -> bool:
21
+ """Determine if an enum value name or enum value is valid or not"""
22
+
23
+ try:
24
+ return cls.reconcile(value=value, default=None) is not None
25
+ except EnumValueError as exception:
26
+ return False
27
+
28
+ @classmethod
29
+ def reconcile(
30
+ cls,
31
+ value: Enum | str | int | object,
32
+ default: Enum = None,
33
+ raises: bool = False,
34
+ ) -> Enum | None:
35
+ """Reconcile enum values and enum names to their corresponding enum option, as
36
+ well as allowing valid enum options to be returned unmodified; if the provided
37
+ enum option, enum value or enum name cannot be reconciled and if a default value
38
+ has been provided, the default value will be returned instead and a warning
39
+ message will be logged, otherwise an EnumValueError exception will be raised."""
40
+
41
+ if isinstance(value, str):
42
+ for prop, enumeration in cls.__members__.items():
43
+ if enumeration.name.casefold() == value.casefold():
44
+ return enumeration
45
+ elif (
46
+ isinstance(enumeration.value, str)
47
+ and enumeration.value.casefold() == value.casefold()
48
+ ):
49
+ return enumeration
50
+ elif isinstance(value, int) and not isinstance(value, bool):
51
+ for prop, enumeration in cls.__members__.items():
52
+ if isinstance(enumeration.value, int) and enumeration.value == value:
53
+ return enumeration
54
+ elif isinstance(value, bool):
55
+ for prop, enumeration in cls.__members__.items():
56
+ if enumeration.value is value:
57
+ return enumeration
58
+ elif isinstance(value, cls):
59
+ if value in cls:
60
+ return value
61
+ elif not value is None:
62
+ for prop, enumeration in cls.__members__.items():
63
+ if enumeration.value == value:
64
+ return enumeration
65
+
66
+ if value is not None:
67
+ if raises is True:
68
+ raise EnumValueError(
69
+ "The provided value, %r, is invalid and does not correspond with this enumeration's options!"
70
+ % (value)
71
+ )
72
+ else:
73
+ logger.debug(
74
+ "The provided value, %r, is invalid, but a default, %r, has been provided, and will be returned instead!",
75
+ value,
76
+ default,
77
+ )
78
+
79
+ return default
80
+
81
+ @classmethod
82
+ def options(cls) -> list[Enum]:
83
+ """Provide straightforward access to the list of enumeration options"""
84
+
85
+ return cls.__members__.values()
enumerific/version.txt ADDED
@@ -0,0 +1 @@
1
+ 1.0.1
@@ -0,0 +1,198 @@
1
+ Metadata-Version: 2.4
2
+ Name: enumerific
3
+ Version: 1.0.1
4
+ Summary: Simplifies working with Python enums.
5
+ Author: Daniel Sissman
6
+ License-Expression: MIT
7
+ Project-URL: documentation, https://github.com/bluebinary/enumerific/blob/main/README.md
8
+ Project-URL: changelog, https://github.com/bluebinary/enumerific/blob/main/CHANGELOG.md
9
+ Project-URL: repository, https://github.com/bluebinary/enumerific
10
+ Project-URL: issues, https://github.com/bluebinary/enumerific/issues
11
+ Project-URL: homepage, https://github.com/bluebinary/enumerific
12
+ Keywords: enum,enumeration,enumerations
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Requires-Python: >=3.10
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE.md
21
+ Provides-Extra: development
22
+ Requires-Dist: black==24.10.*; extra == "development"
23
+ Requires-Dist: pytest==8.3.*; extra == "development"
24
+ Requires-Dist: pytest-codeblocks==0.17.0; extra == "development"
25
+ Provides-Extra: distribution
26
+ Requires-Dist: build; extra == "distribution"
27
+ Requires-Dist: twine; extra == "distribution"
28
+ Requires-Dist: wheel; extra == "distribution"
29
+ Dynamic: license-file
30
+
31
+ # Enumerific Enums
32
+
33
+ The `enumerific` library provides several useful extensions to the Python built-in `enums` library.
34
+
35
+ ### Requirements
36
+
37
+ The Enumerific library has been tested with Python 3.10, 3.11, 3.12 and 3.13, and is not
38
+ compatible with Python 3.9 or earlier.
39
+
40
+ ### Installation
41
+
42
+ Enumerific is available from the PyPI, so may be added to a project's dependencies via
43
+ its `requirements.txt` file or similar by referencing the library's name, `enumerific`,
44
+ or the library may be installed directly into your local runtime environment using `pip`
45
+ by entering the following command, and following any prompts:
46
+
47
+ $ pip install enumerific
48
+
49
+ ### Usage
50
+
51
+ 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:
52
+
53
+ ```python
54
+ from enumerific import Enum
55
+
56
+ class MyEnum(Enum):
57
+ Option1 = "ABC"
58
+ Option2 = "DEF"
59
+
60
+ val = MyEnum.Option1
61
+ ```
62
+
63
+ Alternatively, to make use of the extra functionality for the standard library's `Enum`
64
+ class, import the `Enum` class from the Enumerific library:
65
+
66
+ ```python
67
+ from enumerific import Enum
68
+
69
+ class MyEnum(Enum):
70
+ Option1 = "ABC"
71
+ Option2 = "DEF"
72
+
73
+ val = MyEnum.Option1
74
+ ```
75
+
76
+ You can also import the `Enum` class directly from the `enumerific` library and use it directly:
77
+
78
+ ```python
79
+ from enumerific import Enum
80
+
81
+ class MyEnum(Enum):
82
+ Option1 = "ABC"
83
+ ```
84
+
85
+ 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:
86
+
87
+ * `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.
88
+ * `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.
89
+ * `options() -> list[Enum]` – The `options` method provides easy access to the list of the enumeration's available options.
90
+
91
+ 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.
92
+
93
+ Some examples of use include the following code samples, where each make use of the example `MyEnum` class, defined as follows:
94
+
95
+ ```python
96
+ from enumerific import Enum
97
+
98
+ class MyEnum(Enum):
99
+ Option1 = "ABC"
100
+ Option2 = "DEF"
101
+ ```
102
+
103
+ #### Example 1: Reconciling a Value
104
+
105
+ ```python
106
+ from enumerific import Enum
107
+
108
+ class MyEnum(Enum):
109
+ Option1 = "ABC"
110
+ Option2 = "DEF"
111
+
112
+ # Given a string value in this case
113
+ value = "ABC"
114
+
115
+ # Reconcile it to the associated enumeration option
116
+ value = MyEnum.reconcile(value)
117
+
118
+ assert value == MyEnum.Option1 # asserts successfully
119
+ assert value is MyEnum.Option1 # asserts successfully as enums are singletons
120
+ ```
121
+
122
+ #### Example 2: Reconciling an Enumeration Option Name
123
+
124
+ ```python
125
+ from enumerific import Enum
126
+
127
+ class MyEnum(Enum):
128
+ Option1 = "ABC"
129
+ Option2 = "DEF"
130
+
131
+ # Given a string value in this case
132
+ value = "Option1"
133
+
134
+ # Reconcile it to the associated enumeration option
135
+ value = MyEnum.reconcile(value)
136
+
137
+ assert value == MyEnum.Option1 # asserts successfully
138
+ assert value is MyEnum.Option1 # asserts successfully as enums are singletons
139
+ ```
140
+
141
+ #### Example 3: Validating a Value
142
+
143
+ ```python
144
+ from enumerific import Enum
145
+
146
+ class MyEnum(Enum):
147
+ Option1 = "ABC"
148
+ Option2 = "DEF"
149
+
150
+ # The value can be an enumeration option's name, its value, or the enumeration option
151
+ value = "Option1"
152
+ value = "ABC"
153
+ value = MyEnum.Option1
154
+
155
+ if MyEnum.validate(value) is True:
156
+ # do something if the value could be validated
157
+ pass
158
+ else:
159
+ # do something else if the value could not be validated
160
+ pass
161
+ ```
162
+
163
+ #### Example 4: Iterating Over Enumeration Options
164
+
165
+ ```python
166
+ from enumerific import Enum
167
+
168
+ class MyEnum(Enum):
169
+ Option1 = "ABC"
170
+ Option2 = "DEF"
171
+
172
+ for option in MyEnum.options():
173
+ # do something with each option
174
+ print(option.name, option.value)
175
+ ```
176
+
177
+ ### Unit Tests
178
+
179
+ 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`.
180
+
181
+ 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:
182
+
183
+ ```shell
184
+ $ docker compose build
185
+ $ docker compose run tests
186
+ ```
187
+
188
+ 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:
189
+
190
+ ```shell
191
+ $ docker compose run tests -vv
192
+ ```
193
+
194
+ See the documentation for [PyTest](https://docs.pytest.org/en/latest/) regarding available optional command line arguments.
195
+
196
+ ### Copyright & License Information
197
+
198
+ Copyright © 2024–2025 Daniel Sissman; licensed under the MIT License.
@@ -0,0 +1,12 @@
1
+ enumerific/__init__.py,sha256=YUGW74YNx4ZNtjmtRAJS8zTjn9z0zM1QCAa985kTAjk,419
2
+ enumerific/exceptions.py,sha256=u0-efY2ufY__DZPs56L_SblvhnFZjUb2AcMjL_AxtKw,293
3
+ enumerific/extensible.py,sha256=QCud2izNd5EEvjJWM2GO_U7QHiuDDkGU-5M7s3igpaY,68493
4
+ enumerific/logging.py,sha256=zz1Phnot1BFWMoxwvZ0FlZDsiYZZYhz-_S4IzgPYc40,97
5
+ enumerific/standard.py,sha256=xQhhwlcYZ6-8DmgscbV38g2Ol5Z8_vvBwonz-Ww0I40,3254
6
+ enumerific/version.txt,sha256=1R5uyUBYVUqEVYpbQC7m71_fVFXjXJAv7aYc2odSlDo,5
7
+ enumerific-1.0.1.dist-info/licenses/LICENSE.md,sha256=j1XidOCGUhPx7CyXA31uC0XGKDRnvUcZpMp161qHI6g,1077
8
+ enumerific-1.0.1.dist-info/METADATA,sha256=_fsZpLq6PjDyaBNRMToy8bOJqCbgwAOF3j6oBoHde3Q,7721
9
+ enumerific-1.0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
10
+ enumerific-1.0.1.dist-info/top_level.txt,sha256=hyemsMgPYZgSx71XHmFRF-gvc_2Y4rDAESR8e0hbYHU,11
11
+ enumerific-1.0.1.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
12
+ enumerific-1.0.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.8.0)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,4 +0,0 @@
1
- Metadata-Version: 2.2
2
- Name: enumerific
3
- Version: 0.0.0
4
- License-File: LICENSE.md
@@ -1,7 +0,0 @@
1
- enumerific/__init__.py,sha256=GWEZBAT_ml4VXTQLjUKLnkQtf3AUljF0vokd8-cINk8,3227
2
- enumerific-0.0.0.dist-info/LICENSE.md,sha256=j1XidOCGUhPx7CyXA31uC0XGKDRnvUcZpMp161qHI6g,1077
3
- enumerific-0.0.0.dist-info/METADATA,sha256=xTLu0rdVXG3rz6ct3SytxKUsA4ZJaRbgyyDOSguvEKs,79
4
- enumerific-0.0.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
5
- enumerific-0.0.0.dist-info/top_level.txt,sha256=hyemsMgPYZgSx71XHmFRF-gvc_2Y4rDAESR8e0hbYHU,11
6
- enumerific-0.0.0.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
7
- enumerific-0.0.0.dist-info/RECORD,,