[Python] A more pythonic way to define an enum with dynamic members

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    I needed to create an enum to represent the ISO country codes. The country code data comes from a json file which can be obtained from: https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes

    So what I've done is:

    data = json.load(open('slim-2.json'))
    codes_list = [(data['alpha-2'], int(data['country-code']))
    for i in range(len(data))]

    CountryCode = enum.Enum('CountryCode', codes_list,)

    names_dict = {int(data['country-code']):data['name']
    for i in range(len(data))}
    setattr(CountryCode, '_names', names_dict)

    CountryCode.choices = classmethod(lambda cls:((member.value, name)
    for name, member in cls.__members__.items()))
    setattr(CountryCode, '__str__' ,lambda self: self.__class__._names[self.value])


    This code snippet is frankly ugly. I looked at alternative ways to define the enum class but couldn't piece together a solution. Is there a way to define the enum in the following form:

    class CountryCode(enum.Enum):

    data = json.load(open('slim-2.json'))
    # Some code to define the enum members

    @classmethod
    def choices(cls):
    # etc...


    Any suggestions on how to do this?

    Continue reading...

Compartilhe esta Página