diff options
| author | S. Solomon Darnell | 2025-03-28 21:52:21 -0500 |
|---|---|---|
| committer | S. Solomon Darnell | 2025-03-28 21:52:21 -0500 |
| commit | 4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch) | |
| tree | ee3dc5af3b6313e921cd920906356f5d4febc4ed /.venv/lib/python3.12/site-packages/strenum/mixins.py | |
| parent | cc961e04ba734dd72309fb548a2f97d67d578813 (diff) | |
| download | gn-ai-master.tar.gz | |
Diffstat (limited to '.venv/lib/python3.12/site-packages/strenum/mixins.py')
| -rw-r--r-- | .venv/lib/python3.12/site-packages/strenum/mixins.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/strenum/mixins.py b/.venv/lib/python3.12/site-packages/strenum/mixins.py new file mode 100644 index 00000000..3e5c7e49 --- /dev/null +++ b/.venv/lib/python3.12/site-packages/strenum/mixins.py @@ -0,0 +1,64 @@ +class Comparable: + """Customise how your Enum acts when compared to other objects. + + Your Enum must implement a ``_cmp_values`` method which takes the Enum + member's value and the other value and manipulates them into the actual + values that can be compared. + + A case-insensitive StrEnum might look like this:: + + class HttpHeader(Comparable, KebabCaseStrEnum): + ContentType = auto() + Host = auto() + Accept = auto() + XForwardedFor = auto() + + def _cmp_values(self, other): + return self.value.lower(), str(other).lower() + + You could then use these headers in case-insensitive comparisons:: + + assert "Content-Type" == HttpHeader.ContentType + assert "content-type" == HttpHeader.ContentType + assert "coNtEnt-tyPe" == HttpHeader.ContentType + + .. note:: + Your ``_cmp_values`` method *must not* return ``self`` as one of the + values to be compared -- that would result in infinite recursion. + Instead, perform operations on ``self.value`` and return that. + + .. warning:: + A bug in Python prior to 3.7.1 prevents mix-ins working with Enum + subclasses. + + .. versionadded:: 0.4.6 + """ + + def __eq__(self, other): + value, other = self._cmp_values(other) + return value == other + + def __ne__(self, other): + value, other = self._cmp_values(other) + return value != other + + def __lt__(self, other): + value, other = self._cmp_values(other) + return value < other + + def __le__(self, other): + value, other = self._cmp_values(other) + return value <= other + + def __gt__(self, other): + value, other = self._cmp_values(other) + return value > other + + def __ge__(self, other): + value, other = self._cmp_values(other) + return value >= other + + def _cmp_values(self, other): + raise NotImplementedError( + "Enum's using Comparable must implement their own _cmp_values function." + ) |
