_enum.py 704 B

1234567891011121314151617181920212223
  1. from enum import IntEnum
  2. class ParamEnum(IntEnum):
  3. """Wraps IntEnum to provide validation of a requested item.
  4. Intended for enums used for function parameters.
  5. Use enum.get_value(item) for this behavior instead of builtin enum[item].
  6. """
  7. @classmethod
  8. def get_value(cls, item):
  9. """Validate item and raise a ValueError with valid options if not present."""
  10. try:
  11. return cls[item].value
  12. except KeyError:
  13. valid_options = {e.name for e in cls}
  14. raise ValueError(
  15. "'{}' is not a valid option, must be one of '{}'".format(
  16. item, "', '".join(valid_options)
  17. )
  18. )