enumerific 0.0.0__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
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from enum import *
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class EnumValueError(ValueError):
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
logger = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Enum(Enum):
|
|
16
|
+
"""An extended Enum class that provides support for validating an Enum value and
|
|
17
|
+
accepting either enumeration class properties as enumeration values or their string
|
|
18
|
+
names or values, and providing straightforward access to the enumeration values an
|
|
19
|
+
Enum class holds."""
|
|
20
|
+
|
|
21
|
+
@classmethod
|
|
22
|
+
def validate(cls, value: Enum | str | int | object) -> bool:
|
|
23
|
+
"""Determine if an enum value name or enum value is valid or not"""
|
|
24
|
+
|
|
25
|
+
try:
|
|
26
|
+
return cls.reconcile(value=value, default=None) is not None
|
|
27
|
+
except EnumValueError as exception:
|
|
28
|
+
return False
|
|
29
|
+
|
|
30
|
+
@classmethod
|
|
31
|
+
def reconcile(
|
|
32
|
+
cls,
|
|
33
|
+
value: Enum | str | int | object,
|
|
34
|
+
default: Enum = None,
|
|
35
|
+
raises: bool = False,
|
|
36
|
+
) -> Enum | None:
|
|
37
|
+
"""Reconcile enum values and enum names to their corresponding enum option, as
|
|
38
|
+
well as allowing valid enum options to be returned unmodified; if the provided
|
|
39
|
+
enum option, enum value or enum name cannot be reconciled and if a default value
|
|
40
|
+
has been provided, the default value will be returned instead and a warning
|
|
41
|
+
message will be logged, otherwise an EnumValueError exception will be raised."""
|
|
42
|
+
|
|
43
|
+
if isinstance(value, str):
|
|
44
|
+
for prop, enumeration in cls.__members__.items():
|
|
45
|
+
if enumeration.name.casefold() == value.casefold():
|
|
46
|
+
return enumeration
|
|
47
|
+
elif (
|
|
48
|
+
isinstance(enumeration.value, str)
|
|
49
|
+
and enumeration.value.casefold() == value.casefold()
|
|
50
|
+
):
|
|
51
|
+
return enumeration
|
|
52
|
+
elif isinstance(value, int) and not isinstance(value, bool):
|
|
53
|
+
for prop, enumeration in cls.__members__.items():
|
|
54
|
+
if isinstance(enumeration.value, int) and enumeration.value == value:
|
|
55
|
+
return enumeration
|
|
56
|
+
elif isinstance(value, bool):
|
|
57
|
+
for prop, enumeration in cls.__members__.items():
|
|
58
|
+
if enumeration.value is value:
|
|
59
|
+
return enumeration
|
|
60
|
+
elif isinstance(value, cls):
|
|
61
|
+
if value in cls:
|
|
62
|
+
return value
|
|
63
|
+
elif not value is None:
|
|
64
|
+
for prop, enumeration in cls.__members__.items():
|
|
65
|
+
if enumeration.value == value:
|
|
66
|
+
return enumeration
|
|
67
|
+
|
|
68
|
+
if value is not None:
|
|
69
|
+
if raises is True:
|
|
70
|
+
raise EnumValueError(
|
|
71
|
+
"The provided value, %r, is invalid and does not correspond with this enumeration's options!"
|
|
72
|
+
% (value)
|
|
73
|
+
)
|
|
74
|
+
else:
|
|
75
|
+
logger.warning(
|
|
76
|
+
"The provided value, %r, is invalid, but a default, %r, has been provided, and will be returned instead!",
|
|
77
|
+
value,
|
|
78
|
+
default,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
return default
|
|
82
|
+
|
|
83
|
+
@classmethod
|
|
84
|
+
def options(cls) -> list[Enum]:
|
|
85
|
+
"""Provide straightforward access to the list of enumeration options"""
|
|
86
|
+
|
|
87
|
+
return cls.__members__.values()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright © 2024–2025 Daniel Sissman.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,7 @@
|
|
|
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,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
enumerific
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|