python - 我什么时候应该继承 EnumMeta 而不是 Enum?

标签 python enums metaclass

this article Nick Coghlan 谈到了 PEP 435 Enum type 中的一些设计决策,以及如何将 EnumMeta 子类化以提供不同的 Enum 体验。

但是,我(我是 stdlib Enum 的主要作者)关于使用元类的建议是,如果没有真正充分的理由,就不应该这样做——比如无法完成你需要一个类装饰器,或者一个隐藏任何丑陋的专用函数;在我自己的工作中,我已经能够通过在创建 时使用 __new____init__ 和/或普通类/实例方法来做我需要的任何事情枚举类:

还有一个警示故事,在深入研究 Enum 时要小心,无论是否有元类子类化:

考虑到所有这些,我什么时候需要摆弄 EnumMeta 本身?

最佳答案

到目前为止,我见过的最好的(也是唯一的)子类化 EnumMeta 案例来自以下四个问题:

我们将在这里进一步研究动态成员案例。


首先,看一下不继承 EnumMeta 时所需的代码:

stdlib 方式

from enum import Enum
import json

class BaseCountry(Enum):
    def __new__(cls, record):
        member = object.__new__(cls)
        member.country_name = record['name']
        member.code = int(record['country-code'])
        member.abbr = record['alpha-2']
        member._value_ = member.abbr, member.code, member.country_name
        if not hasattr(cls, '_choices'):
            cls._choices = {}
        cls._choices[member.code] = member.country_name
        cls._choices[member.abbr] = member.country_name
        return member                
    def __str__(self):
        return self.country_name

Country = BaseCountry(
        'Country',
        [(rec['alpha-2'], rec) for rec in json.load(open('slim-2.json'))],
        )

aenum方式 1 2

from aenum import Enum, MultiValue
import json

class Country(Enum, init='abbr code country_name', settings=MultiValue):
    _ignore_ = 'country this'  # do not add these names as members
    # create members
    this = vars()
    for country in json.load(open('slim-2.json')):
        this[country['alpha-2']] = (
                country['alpha-2'],
                int(country['country-code']),
                country['name'],
                )
    # have str() print just the country name
    def __str__(self):
        return self.country_name

以上代码适用于一次性枚举——但是如果从 JSON 文件创建枚举对您来说很常见怎么办?想象一下,如果您可以这样做:

class Country(JSONEnum):
    _init_ = 'abbr code country_name'  # remove if not using aenum
    _file = 'some_file.json'
    _name = 'alpha-2'
    _value = {
            1: ('alpha-2', None),
            2: ('country-code', lambda c: int(c)),
            3: ('name', None),
            }

如你所见:

  • _file 是要使用的json文件名
  • _name 是任何应该用于名称的路径
  • _value 是字典映射路径到值3
  • _init_ 指定不同值组件的属性名称(如果使用 aenum)

JSON数据取自https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes -- 这是一个简短的摘录:

[{"name":"Afghanistan","alpha-2":"AF","country-code":"004"},

{"name":"Åland Islands","alpha-2":"AX","country-code":"248"},

{"name":"Albania","alpha-2":"AL","country-code":"008"},

{"name":"Algeria","alpha-2":"DZ","country-code":"012"}]

这里是 JSONenumMeta 类:

class JSONEnumMeta(EnumMeta):

    @classmethod
    def __prepare__(metacls, cls, bases, **kwds):
        # return a standard dictionary for the initial processing
        return {}

    def __init__(cls, *args , **kwds):
        super(JSONEnumMeta, cls).__init__(*args)

    def __new__(metacls, cls, bases, clsdict, **kwds):
        import json
        members = []
        missing = [
               name
               for name in ('_file', '_name', '_value')
               if name not in clsdict
               ]
        if len(missing) in (1, 2):
            # all three must be present or absent
            raise TypeError('missing required settings: %r' % (missing, ))
        if not missing:
            # process
            name_spec = clsdict.pop('_name')
            if not isinstance(name_spec, (tuple, list)):
                name_spec = (name_spec, )
            value_spec = clsdict.pop('_value')
            file = clsdict.pop('_file')
            with open(file) as f:
                json_data = json.load(f)
            for data in json_data:
                values = []
                name = data[name_spec[0]]
                for piece in name_spec[1:]:
                    name = name[piece]
                for order, (value_path, func) in sorted(value_spec.items()):
                    if not isinstance(value_path, (list, tuple)):
                        value_path = (value_path, )
                    value = data[value_path[0]]
                    for piece in value_path[1:]:
                        value = value[piece]
                    if func is not None:
                        value = func(value)
                    values.append(value)
                values = tuple(values)
                members.append(
                    (name, values)
                    )
        # get the real EnumDict
        enum_dict = super(JSONEnumMeta, metacls).__prepare__(cls, bases, **kwds)
        # transfer the original dict content, _items first
        items = list(clsdict.items())
        items.sort(key=lambda p: (0 if p[0][0] == '_' else 1, p))
        for name, value in items:
            enum_dict[name] = value
        # add the members
        for name, value in members:
            enum_dict[name] = value
        return super(JSONEnumMeta, metacls).__new__(metacls, cls, bases, enum_dict, **kwds)

# for use with both Python 2/3
JSONEnum = JSONEnumMeta('JsonEnum', (Enum, ), {})

一些注意事项:

  • JSONEnumMeta.__prepare__ 返回正常的 dict

  • EnumMeta.__prepare__ 用于获取 _EnumDict 的实例——这是获取实例的正确方法

  • 带有前导下划线的键首先传递给真正的 _EnumDict,因为在处理枚举成员时可能需要它们

  • 枚举成员的顺序与它们在文件中的顺序相同


1 披露:我是 Python stdlib Enum 的作者, enum34 backport , 和 Advanced Enumeration (aenum)图书馆。

2 这需要 aenum 2.0.5+

3 如果您的 Enum 需要多个值,键是数字的,以保持多个值的顺序。

关于python - 我什么时候应该继承 EnumMeta 而不是 Enum?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43730305/

相关文章:

C# 枚举标志比较

带范围的 Python 列表

python - 我的 ipy.exe 在哪里?

c# - 枚举到 int 最佳实践

c# - Swashbuckle SchemaFilter 用于 api 操作参数

python - 如何在元类中自动生成属性?

python - 如何按照声明的顺序读取类属性?

python - 为什么元类应该从类型继承?

python - 如何确保轮换 TLS session 票证?

python - 在python中将字符串/字符转换为整数