Module astrapy.utils.str_enum

Classes

class StrEnum (*args, **kwds)
Expand source code
class StrEnum(Enum, metaclass=StrEnumMeta):
    @classmethod
    def coerce(cls: type[T], value: str | T) -> T:
        """
        Accepts either a string or an instance of the Enum itself.
        If a string is passed, it converts it to the corresponding
        Enum value (case-insensitive).
        If an Enum instance is passed, it returns it as-is.
        Raises ValueError if the string does not match any enum member.
        """

        if isinstance(value, cls):
            return value
        elif isinstance(value, str):
            norm_value = cls._name_lookup(value)
            if norm_value is not None:
                return cls[norm_value]
            # no matches
            raise ValueError(
                f"Invalid value '{value}' for {cls.__name__}. "
                f"Allowed values are: {[e.value for e in cls]}"
            )
        raise ValueError(
            f"Invalid value '{value}' for {cls.__name__}. "
            f"Allowed values are: {[e.value for e in cls]}"
        )

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access:

Color.RED

  • value lookup:

Color(1)

  • name lookup:

Color['RED']

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.

Ancestors

  • enum.Enum

Subclasses

Static methods

def coerce(value: str | T) ‑> ~T

Accepts either a string or an instance of the Enum itself. If a string is passed, it converts it to the corresponding Enum value (case-insensitive). If an Enum instance is passed, it returns it as-is. Raises ValueError if the string does not match any enum member.

class StrEnumMeta (*args, **kwargs)
Expand source code
class StrEnumMeta(EnumMeta):
    def _name_lookup(cls, value: str) -> str | None:
        """Return a proper key in the enum if some matching logic works, or None."""
        mmap = {k: v.value for k, v in cls._member_map_.items()}
        # try exact key match
        if value in mmap:
            return value
        # try case-insensitive key match
        u_value = value.upper()
        u_mmap = {k.upper(): k for k in mmap.keys()}
        if u_value in u_mmap:
            return u_mmap[u_value]
        # try case-insensitive *value* match
        v_mmap = {v.upper(): k for k, v in mmap.items()}
        if u_value in v_mmap:
            return v_mmap[u_value]
        return None

    def __contains__(cls, value: object) -> bool:
        """Return True if the provided string belongs to the enum."""
        if isinstance(value, str):
            return cls._name_lookup(value) is not None
        return False

Metaclass for Enum

Ancestors

  • enum.EnumType
  • builtins.type