enum_util.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from __future__ import annotations
  2. from contourpy._contourpy import FillType, LineType, ZInterp
  3. def as_fill_type(fill_type: FillType | str) -> FillType:
  4. """Coerce a FillType or string value to a FillType.
  5. Args:
  6. fill_type (FillType or str): Value to convert.
  7. Return:
  8. FillType: Converted value.
  9. """
  10. if isinstance(fill_type, str):
  11. try:
  12. return FillType.__members__[fill_type]
  13. except KeyError as e:
  14. raise ValueError(f"'{fill_type}' is not a valid FillType") from e
  15. else:
  16. return fill_type
  17. def as_line_type(line_type: LineType | str) -> LineType:
  18. """Coerce a LineType or string value to a LineType.
  19. Args:
  20. line_type (LineType or str): Value to convert.
  21. Return:
  22. LineType: Converted value.
  23. """
  24. if isinstance(line_type, str):
  25. try:
  26. return LineType.__members__[line_type]
  27. except KeyError as e:
  28. raise ValueError(f"'{line_type}' is not a valid LineType") from e
  29. else:
  30. return line_type
  31. def as_z_interp(z_interp: ZInterp | str) -> ZInterp:
  32. """Coerce a ZInterp or string value to a ZInterp.
  33. Args:
  34. z_interp (ZInterp or str): Value to convert.
  35. Return:
  36. ZInterp: Converted value.
  37. """
  38. if isinstance(z_interp, str):
  39. try:
  40. return ZInterp.__members__[z_interp]
  41. except KeyError as e:
  42. raise ValueError(f"'{z_interp}' is not a valid ZInterp") from e
  43. else:
  44. return z_interp