enumerific 1.0.0__py3-none-any.whl → 1.0.2__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 CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.2