_repr_enum.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import functools
  12. from enum import Enum
  13. class _ReprEnum(Enum):
  14. @classmethod
  15. @functools.lru_cache
  16. def _from_repr(cls, repr_: str):
  17. return next(filter(lambda e: repr(e) == repr_, cls)) # type: ignore
  18. class _OrderedEnum(_ReprEnum):
  19. def __ge__(self, other):
  20. if self.__class__ is other.__class__:
  21. return self.value >= other.value
  22. return NotImplemented
  23. def __gt__(self, other):
  24. if self.__class__ is other.__class__:
  25. return self.value > other.value
  26. return NotImplemented
  27. def __le__(self, other):
  28. if self.__class__ is other.__class__:
  29. return self.value <= other.value
  30. return NotImplemented
  31. def __lt__(self, other):
  32. if self.__class__ is other.__class__:
  33. return self.value < other.value
  34. return NotImplemented