from enum import Enum

{%- if ordinals | length == 0 %}
_{{ class_name }}_members = []
{%- endif %}

class {{ class_name }}(Enum):
    """
    {{ docstring }}
    """
    {%- for symbol in symbols %}
    {{ symbol }} = '{{symbol}}'
    {%- endfor %}

    @classmethod
    def from_ordinal(cls, ordinal: int|str) -> '{{ class_name }}':
        """
        Get enum member by ordinal

        Args:
            ordinal (int| str): The ordinal of the enum member. This can be an integer or a string representation of an integer.

        Returns:
            The enum member corresponding to the ordinal.
        """
        {%- if ordinals | length == 0 %}
        # pylint: disable=global-statement
        global _{{ class_name }}_members
        # pylint: enable=global-statement
        {%- endif %}

        if ordinal is None:
            raise ValueError("ordinal must not be None")
        if isinstance(ordinal, str) and ordinal.isdigit():
            ordinal = int(ordinal)
        {%- if ordinals | length > 0 %}
        if isinstance(ordinal, int):
        {%- for sym, ord in ordinals.items() -%}
            {%- if loop.first %}
            if ordinal == {{ ord }}:
            {%- else %}
            elif ordinal == {{ ord }}:
            {%- endif %}
                return {{ class_name }}.{{ sym }}
        {%- endfor %}
            raise ValueError("Ordinal not found in enum")
        else:
            raise ValueError("Ordinal must be an integer or a string representation of an integer")
        {%- else %}
        if not _{{ class_name }}_members:
            _{{ class_name }}_members = list(cls)
        if 0 <= int(ordinal) < len(_{{ class_name }}_members):
            return _{{ class_name }}_members[ordinal]
        else:
            raise IndexError("Ordinal out of range for enum")
        {%- endif %}

    @classmethod
    def to_ordinal(cls, member: '{{ class_name }}') -> int:
        """
        Get enum ordinal

        Args:
            member ({{ class_name }}): The enum member to get the ordinal of.

        Returns:
            The ordinal of the enum member.
        """
        {%- if ordinals | length == 0 %}
        # pylint: disable=global-statement
        global _{{ class_name }}_members
        # pylint: enable=global-statement
        
        if not _{{ class_name }}_members:
            _{{ class_name }}_members = list(cls)
        return _{{ class_name }}_members.index(member)
        {%- else %}
        {% for sym, ord in ordinals.items() %}
        if member == {{ class_name }}.{{ sym }}:
            return {{ ord }}
        {%- endfor %}
        raise ValueError("Member not found in enum")
        {%- endif %}

{%- if ordinals | length == 0 %}
_{{ class_name }}_members = list({{ class_name }})
{%- endif %}