Python - extend enum fields during creation

Is it possible extend enum during creation? Example:

class MyEnum(enum.StrEnum):
    ID = "id"
    NAME = "NAME

And I need that after creating this enum contains the next fields:

ID = "id"
NAME = "name"
ID_DESC = "-id"
NAME_DESC = "-name"

I need this to create custom ordering enum for FastAPI project

Now I have the next way to create new enum

NewEnum = enum.StrEnum(
    f"{name.title()}OrderingEnum",
    [
        (
            f"{ordering_field.upper()}_DESC"
            if ordering_field.startswith("-")
            else ordering_field.upper(),
            ordering_field,
        )
        for ordering_field in itertools.chain(
            values,
            [f"-{field}" for field in values],
        )
    ],
)

But I need do this automatically, because each module with model have similar enum. Maybe this is possible to solve my problem using MetaClass for my enum class, or override __new__ method, but I didn't find working solution yet



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation