enumerific 1.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/__init__.py +21 -85
- enumerific/exceptions.py +18 -0
- enumerific/extensible.py +1912 -0
- enumerific/logging.py +5 -0
- enumerific/standard.py +85 -0
- enumerific/version.txt +1 -1
- {enumerific-1.0.0.dist-info → enumerific-1.0.1.dist-info}/METADATA +74 -44
- enumerific-1.0.1.dist-info/RECORD +12 -0
- {enumerific-1.0.0.dist-info → enumerific-1.0.1.dist-info}/WHEEL +1 -1
- enumerific-1.0.0.dist-info/RECORD +0 -8
- {enumerific-1.0.0.dist-info → enumerific-1.0.1.dist-info/licenses}/LICENSE.md +0 -0
- {enumerific-1.0.0.dist-info → enumerific-1.0.1.dist-info}/top_level.txt +0 -0
- {enumerific-1.0.0.dist-info → enumerific-1.0.1.dist-info}/zip-safe +0 -0
enumerific/logging.py
ADDED
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
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
1.0.
|
|
1
|
+
1.0.1
|
|
@@ -1,44 +1,32 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: enumerific
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.1
|
|
4
4
|
Summary: Simplifies working with Python enums.
|
|
5
5
|
Author: Daniel Sissman
|
|
6
|
-
License: MIT
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in all
|
|
18
|
-
copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
-
SOFTWARE.
|
|
27
|
-
Project-URL: Documentation, https://github.com/bluebinary/enumerific/blob/main/README.md
|
|
28
|
-
Project-URL: Changelog, https://github.com/bluebinary/enumerific/blob/main/CHANGELOG.md
|
|
29
|
-
Project-URL: Repository, https://github.com/bluebinary/enumerific
|
|
30
|
-
Project-URL: Issues, https://github.com/bluebinary/enumerific/issues
|
|
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
|
|
31
12
|
Keywords: enum,enumeration,enumerations
|
|
32
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
33
13
|
Classifier: Programming Language :: Python :: 3
|
|
34
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
35
14
|
Classifier: Programming Language :: Python :: 3.10
|
|
36
15
|
Classifier: Programming Language :: Python :: 3.11
|
|
37
16
|
Classifier: Programming Language :: Python :: 3.12
|
|
38
17
|
Classifier: Programming Language :: Python :: 3.13
|
|
39
|
-
Requires-Python: >=3.
|
|
18
|
+
Requires-Python: >=3.10
|
|
40
19
|
Description-Content-Type: text/markdown
|
|
41
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
|
|
42
30
|
|
|
43
31
|
# Enumerific Enums
|
|
44
32
|
|
|
@@ -46,11 +34,15 @@ The `enumerific` library provides several useful extensions to the Python built-
|
|
|
46
34
|
|
|
47
35
|
### Requirements
|
|
48
36
|
|
|
49
|
-
The Enumerific library has been tested with Python 3.
|
|
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.
|
|
50
39
|
|
|
51
40
|
### Installation
|
|
52
41
|
|
|
53
|
-
|
|
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:
|
|
54
46
|
|
|
55
47
|
$ pip install enumerific
|
|
56
48
|
|
|
@@ -58,10 +50,23 @@ The Enumerific library is available from PyPi, so may be added to a project's de
|
|
|
58
50
|
|
|
59
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:
|
|
60
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
61
|
```
|
|
62
|
-
import enumerific
|
|
63
62
|
|
|
64
|
-
|
|
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):
|
|
65
70
|
Option1 = "ABC"
|
|
66
71
|
Option2 = "DEF"
|
|
67
72
|
|
|
@@ -70,12 +75,11 @@ val = MyEnum.Option1
|
|
|
70
75
|
|
|
71
76
|
You can also import the `Enum` class directly from the `enumerific` library and use it directly:
|
|
72
77
|
|
|
73
|
-
```
|
|
78
|
+
```python
|
|
74
79
|
from enumerific import Enum
|
|
75
80
|
|
|
76
81
|
class MyEnum(Enum):
|
|
77
82
|
Option1 = "ABC"
|
|
78
|
-
...
|
|
79
83
|
```
|
|
80
84
|
|
|
81
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:
|
|
@@ -88,7 +92,7 @@ The benefits of being able to validate and reconcile various input values agains
|
|
|
88
92
|
|
|
89
93
|
Some examples of use include the following code samples, where each make use of the example `MyEnum` class, defined as follows:
|
|
90
94
|
|
|
91
|
-
```
|
|
95
|
+
```python
|
|
92
96
|
from enumerific import Enum
|
|
93
97
|
|
|
94
98
|
class MyEnum(Enum):
|
|
@@ -98,7 +102,13 @@ class MyEnum(Enum):
|
|
|
98
102
|
|
|
99
103
|
#### Example 1: Reconciling a Value
|
|
100
104
|
|
|
101
|
-
```
|
|
105
|
+
```python
|
|
106
|
+
from enumerific import Enum
|
|
107
|
+
|
|
108
|
+
class MyEnum(Enum):
|
|
109
|
+
Option1 = "ABC"
|
|
110
|
+
Option2 = "DEF"
|
|
111
|
+
|
|
102
112
|
# Given a string value in this case
|
|
103
113
|
value = "ABC"
|
|
104
114
|
|
|
@@ -111,7 +121,13 @@ assert value is MyEnum.Option1 # asserts successfully as enums are singletons
|
|
|
111
121
|
|
|
112
122
|
#### Example 2: Reconciling an Enumeration Option Name
|
|
113
123
|
|
|
114
|
-
```
|
|
124
|
+
```python
|
|
125
|
+
from enumerific import Enum
|
|
126
|
+
|
|
127
|
+
class MyEnum(Enum):
|
|
128
|
+
Option1 = "ABC"
|
|
129
|
+
Option2 = "DEF"
|
|
130
|
+
|
|
115
131
|
# Given a string value in this case
|
|
116
132
|
value = "Option1"
|
|
117
133
|
|
|
@@ -124,7 +140,13 @@ assert value is MyEnum.Option1 # asserts successfully as enums are singletons
|
|
|
124
140
|
|
|
125
141
|
#### Example 3: Validating a Value
|
|
126
142
|
|
|
127
|
-
```
|
|
143
|
+
```python
|
|
144
|
+
from enumerific import Enum
|
|
145
|
+
|
|
146
|
+
class MyEnum(Enum):
|
|
147
|
+
Option1 = "ABC"
|
|
148
|
+
Option2 = "DEF"
|
|
149
|
+
|
|
128
150
|
# The value can be an enumeration option's name, its value, or the enumeration option
|
|
129
151
|
value = "Option1"
|
|
130
152
|
value = "ABC"
|
|
@@ -132,13 +154,21 @@ value = MyEnum.Option1
|
|
|
132
154
|
|
|
133
155
|
if MyEnum.validate(value) is True:
|
|
134
156
|
# do something if the value could be validated
|
|
157
|
+
pass
|
|
135
158
|
else:
|
|
136
159
|
# do something else if the value could not be validated
|
|
160
|
+
pass
|
|
137
161
|
```
|
|
138
162
|
|
|
139
163
|
#### Example 4: Iterating Over Enumeration Options
|
|
140
164
|
|
|
141
|
-
```
|
|
165
|
+
```python
|
|
166
|
+
from enumerific import Enum
|
|
167
|
+
|
|
168
|
+
class MyEnum(Enum):
|
|
169
|
+
Option1 = "ABC"
|
|
170
|
+
Option2 = "DEF"
|
|
171
|
+
|
|
142
172
|
for option in MyEnum.options():
|
|
143
173
|
# do something with each option
|
|
144
174
|
print(option.name, option.value)
|
|
@@ -150,14 +180,14 @@ The Enumerific library includes a suite of comprehensive unit tests which ensure
|
|
|
150
180
|
|
|
151
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:
|
|
152
182
|
|
|
153
|
-
```
|
|
183
|
+
```shell
|
|
154
184
|
$ docker compose build
|
|
155
185
|
$ docker compose run tests
|
|
156
186
|
```
|
|
157
187
|
|
|
158
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:
|
|
159
189
|
|
|
160
|
-
```
|
|
190
|
+
```shell
|
|
161
191
|
$ docker compose run tests -vv
|
|
162
192
|
```
|
|
163
193
|
|
|
@@ -165,4 +195,4 @@ See the documentation for [PyTest](https://docs.pytest.org/en/latest/) regarding
|
|
|
165
195
|
|
|
166
196
|
### Copyright & License Information
|
|
167
197
|
|
|
168
|
-
Copyright © 2024–2025 Daniel Sissman;
|
|
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,8 +0,0 @@
|
|
|
1
|
-
enumerific/__init__.py,sha256=GWEZBAT_ml4VXTQLjUKLnkQtf3AUljF0vokd8-cINk8,3227
|
|
2
|
-
enumerific/version.txt,sha256=klIfw8vZZL3J9YSpkbif3apXVO0cyW1tQkRTOGacEwU,5
|
|
3
|
-
enumerific-1.0.0.dist-info/LICENSE.md,sha256=j1XidOCGUhPx7CyXA31uC0XGKDRnvUcZpMp161qHI6g,1077
|
|
4
|
-
enumerific-1.0.0.dist-info/METADATA,sha256=MHKN_mQRKEneFtNS5VAX4bJd33Z0NhRTKVzAUL9xb6M,8066
|
|
5
|
-
enumerific-1.0.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
|
6
|
-
enumerific-1.0.0.dist-info/top_level.txt,sha256=hyemsMgPYZgSx71XHmFRF-gvc_2Y4rDAESR8e0hbYHU,11
|
|
7
|
-
enumerific-1.0.0.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
8
|
-
enumerific-1.0.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|