如何在枚举类型上添加文档字符串?

31
Python 3.4有一个新的枚举模块和枚举数据类型。如果你还不能切换到3.4版本,枚举已经被回溯移植
由于枚举成员支持文档字符串,就像几乎所有的Python对象一样,我想设置它们。有没有一种简单的方法来做到这一点?
4个回答

16
对于现在的许多集成开发环境(IDE)来说,在2022年,以下内容将填充智能感知功能:
class MyEnum(Enum):
    """
    MyEnum purpose and general doc string
    """

    VALUE = "Value"
    """
    This is the Value selection. Use this for Values
    """
    BUILD = "Build"
    """
    This is the Build selection. Use this for Buildings
    """

在VSCode中的示例:

enums with doc strings

popping in intellisense


有没有一个PEP或者更正式的东西? - undefined
@lynkfox:除了VSCode,还有哪些IDE支持这种特定的风格?而这些三引号字符串实际上是作为枚举成员的__doc__属性创建的吗? - undefined

11

有的,而且这是到目前为止我最喜欢的食谱。作为额外福利,你也不需要指定整数值。以下是一个例子:

class AddressSegment(AutoEnum):
    misc = "not currently tracked"
    ordinal = "N S E W NE NW SE SW"
    secondary = "apt bldg floor etc"
    street = "st ave blvd etc"

你可能会问为什么我不直接将"N S E W NE NW SE SW"作为ordinal的值?因为当我获取它的repr时,看到<AddressSegment.ordinal: 'N S E W NE NW SE SW'>有点笨重,但在文档字符串中随时可以获得这些信息是一个很好的折衷方案。

以下是Enum的配方:

class AutoEnum(enum.Enum):
    """
    Automatically numbers enum members starting from 1.

    Includes support for a custom docstring per member.
    """
    #
    def __new__(cls, *args):
        """Ignores arguments (will be handled in __init__."""
        value = len(cls) + 1
        obj = object.__new__(cls)
        obj._value_ = value
        return obj
    #
    def __init__(self, *args):
        """Can handle 0 or 1 argument; more requires a custom __init__.

        0  = auto-number w/o docstring
        1  = auto-number w/ docstring
        2+ = needs custom __init__

        """
        if len(args) == 1 and isinstance(args[0], (str, unicode)):
            self.__doc__ = args[0]
        elif args:
            raise TypeError('%s not dealt with -- need custom __init__' % (args,))

并在使用中:

>>> list(AddressSegment)
[<AddressSegment.ordinal: 1>, <AddressSegment.secondary: 2>, <AddressSegment.misc: 3>, <AddressSegment.street: 4>]

>>> AddressSegment.secondary
<AddressSegment.secondary: 2>

>>> AddressSegment.secondary.__doc__
'apt bldg floor etc'

我在__init__中处理参数而不是在__new__中处理的原因是,如果我想进一步扩展它,那么处理子类AutoEnum会更加容易。


抱歉,我不明白这里的文档字符串是什么。你是在定义四个枚举类型,分别命名为“misc”、“ordinal”、“secondary”和“street”吗? - Noumenon
@Noumenon:是的,正在创建四个成员,并且每个成员的文档字符串是其后面的文本。这四个成员的值分别为1234 - Ethan Furman

0

这并不是直接回答问题,但我想添加一个更强大的版本@Ethan Furman的AutoEnum类,它使用auto枚举函数。

下面的实现与Pydantic配合使用,并对值进行模糊匹配以获得相应的枚举类型。

用法:

In [2]: class Weekday(AutoEnum):  ## Assume AutoEnum class has been defined.
   ...:     Monday = auto()
   ...:     Tuesday = auto()
   ...:     Wednesday = auto()
   ...:     Thursday = auto()
   ...:     Friday = auto()
   ...:     Saturday = auto()
   ...:     Sunday = auto()
   ...:

In [3]: Weekday('MONDAY')  ## Fuzzy matching: case-insensitive
Out[3]: Monday

In [4]: Weekday(' MO NDAY') ## Fuzzy matching: ignores extra spaces
Out[4]: Monday

In [5]: Weekday('_M_onDa y')  ## Fuzzy matching: ignores underscores
Out[5]: Monday

In [6]: %timeit Weekday('_M_onDay')  ## Fuzzy matching takes ~1 microsecond.
1.15 µs ± 10.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [7]: %timeit Weekday.from_str('_M_onDay')  ## You can further speedup matching using from_str (this is because _missing_ is not called)
736 ns ± 8.89 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [8]: list(Weekday)  ## Get all the enums
Out[8]: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

In [9]: Weekday.Monday.matches('Tuesday')  ## Check if a string matches a particular enum value
Out[9]: False

In [10]: Weekday.matches_any('__TUESDAY__')  ## Check if a string matches any enum
Out[10]: True

In [11]: Weekday.Tuesday is Weekday('  Tuesday') and Weekday.Tuesday == Weekday('_Tuesday_')  ## `is` and `==` work as expected
Out[11]: True

In [12]: Weekday.Tuesday == 'Tuesday'  ## Strings don't match enum values, because strings aren't enums!
Out[12]: False

In [13]: Weekday.convert_keys({  ## Convert matching dict keys to an enum. Similar: .convert_list, .convert_set
    'monday': 'alice', 
    'tuesday': 'bob', 
    'not_wednesday': 'charles', 
    'THURSDAY ': 'denise', 
}) 
Out[13]: 
{Monday: 'alice',
 Tuesday: 'bob',
 'not_wednesday': 'charles',
 Thursday: 'denise'}

AutoEnum 的代码可以在下面找到。

如果您想更改模糊匹配逻辑,则可以覆盖类方法 _normalize(例如,在 _normalize 中返回输入不变将执行精确匹配)。

from typing import *
from enum import Enum, auto

class AutoEnum(str, Enum):
    """
    Utility class which can be subclassed to create enums using auto().
    Also provides utility methods for common enum operations.
    """

    @classmethod
    def _missing_(cls, enum_value: Any):
        ## Ref: https://dev59.com/Mbnoa4cB1Zd3GeqPLzGM#60174274
        ## This is needed to allow Pydantic to perform case-insensitive conversion to AutoEnum.
        return cls.from_str(enum_value=enum_value, raise_error=True)

    def _generate_next_value_(name, start, count, last_values):
        return name

    @property
    def str(self) -> str:
        return self.__str__()

    def __repr__(self):
        return self.__str__()

    def __str__(self):
        return self.name

    def __hash__(self):
        return hash(self.__class__.__name__ + '.' + self.name)

    def __eq__(self, other):
        return self is other

    def __ne__(self, other):
        return self is not other

    def matches(self, enum_value: str) -> bool:
        return self is self.from_str(enum_value, raise_error=False)

    @classmethod
    def matches_any(cls, enum_value: str) -> bool:
        return cls.from_str(enum_value, raise_error=False) is not None

    @classmethod
    def does_not_match_any(cls, enum_value: str) -> bool:
        return not cls.matches_any(enum_value)

    @classmethod
    def _initialize_lookup(cls):
        if '_value2member_map_normalized_' not in cls.__dict__:  ## Caching values for fast retrieval.
            cls._value2member_map_normalized_ = {}
            for e in list(cls):
                normalized_e_name: str = cls._normalize(e.value)
                if normalized_e_name in cls._value2member_map_normalized_:
                    raise ValueError(
                        f'Cannot register enum "{e.value}"; '
                        f'another enum with the same normalized name "{normalized_e_name}" already exists.'
                    )
                cls._value2member_map_normalized_[normalized_e_name] = e

    @classmethod
    def from_str(cls, enum_value: str, raise_error: bool = True) -> Optional:
        """
        Performs a case-insensitive lookup of the enum value string among the members of the current AutoEnum subclass.
        :param enum_value: enum value string
        :param raise_error: whether to raise an error if the string is not found in the enum
        :return: an enum value which matches the string
        :raises: ValueError if raise_error is True and no enum value matches the string
        """
        if isinstance(enum_value, cls):
            return enum_value
        if enum_value is None and raise_error is False:
            return None
        if not isinstance(enum_value, str) and raise_error is True:
            raise ValueError(f'Input should be a string; found type {type(enum_value)}')
        cls._initialize_lookup()
        enum_obj: Optional[AutoEnum] = cls._value2member_map_normalized_.get(cls._normalize(enum_value))
        if enum_obj is None and raise_error is True:
            raise ValueError(f'Could not find enum with value {enum_value}; available values are: {list(cls)}.')
        return enum_obj

    @classmethod
    def _normalize(cls, x: str) -> str:
        ## Found to be faster than .translate() and re.sub() on Python 3.10.6
        return str(x).replace(' ', '').replace('-', '').replace('_', '').lower()

    @classmethod
    def convert_keys(cls, d: Dict) -> Dict:
        """
        Converts string dict keys to the matching members of the current AutoEnum subclass.
        Leaves non-string keys untouched.
        :param d: dict to transform
        :return: dict with matching string keys transformed to enum values
        """
        out_dict = {}
        for k, v in d.items():
            if isinstance(k, str) and cls.from_str(k, raise_error=False) is not None:
                out_dict[cls.from_str(k, raise_error=False)] = v
            else:
                out_dict[k] = v
        return out_dict

    @classmethod
    def convert_keys_to_str(cls, d: Dict) -> Dict:
        """
        Converts dict keys of the current AutoEnum subclass to the matching string key.
        Leaves other keys untouched.
        :param d: dict to transform
        :return: dict with matching keys of the current AutoEnum transformed to strings.
        """
        out_dict = {}
        for k, v in d.items():
            if isinstance(k, cls):
                out_dict[str(k)] = v
            else:
                out_dict[k] = v
        return out_dict

    @classmethod
    def convert_values(
            cls,
            d: Union[Dict, Set, List, Tuple],
            raise_error: bool = False
    ) -> Union[Dict, Set, List, Tuple]:
        """
        Converts string values to the matching members of the current AutoEnum subclass.
        Leaves non-string values untouched.
        :param d: dict, set, list or tuple to transform.
        :param raise_error: raise an error if unsupported type.
        :return: data structure with matching string values transformed to enum values.
        """
        if isinstance(d, dict):
            return cls.convert_dict_values(d)
        if isinstance(d, list):
            return cls.convert_list(d)
        if isinstance(d, tuple):
            return tuple(cls.convert_list(d))
        if isinstance(d, set):
            return cls.convert_set(d)
        if raise_error:
            raise ValueError(f'Unrecognized data structure of type {type(d)}')
        return d

    @classmethod
    def convert_dict_values(cls, d: Dict) -> Dict:
        """
        Converts string dict values to the matching members of the current AutoEnum subclass.
        Leaves non-string values untouched.
        :param d: dict to transform
        :return: dict with matching string values transformed to enum values
        """
        out_dict = {}
        for k, v in d.items():
            if isinstance(v, str) and cls.from_str(v, raise_error=False) is not None:
                out_dict[k] = cls.from_str(v, raise_error=False)
            else:
                out_dict[k] = v
        return out_dict

    @classmethod
    def convert_list(cls, l: List) -> List:
        """
        Converts string list itmes to the matching members of the current AutoEnum subclass.
        Leaves non-string items untouched.
        :param l: list to transform
        :return: list with matching string items transformed to enum values
        """
        out_list = []
        for item in l:
            if isinstance(item, str) and cls.matches_any(item):
                out_list.append(cls.from_str(item))
            else:
                out_list.append(item)
        return out_list

    @classmethod
    def convert_set(cls, s: Set) -> Set:
        """
        Converts string list itmes to the matching members of the current AutoEnum subclass.
        Leaves non-string items untouched.
        :param s: set to transform
        :return: set with matching string items transformed to enum values
        """
        out_set = set()
        for item in s:
            if isinstance(item, str) and cls.matches_any(item):
                out_set.add(cls.from_str(item))
            else:
                out_set.add(item)
        return out_set

    @classmethod
    def convert_values_to_str(cls, d: Dict) -> Dict:
        """
        Converts dict values of the current AutoEnum subclass to the matching string value.
        Leaves other values untouched.
        :param d: dict to transform
        :return: dict with matching values of the current AutoEnum transformed to strings.
        """
        out_dict = {}
        for k, v in d.items():
            if isinstance(v, cls):
                out_dict[k] = str(v)
            else:
                out_dict[k] = v
        return out_dict

-5

函数和类有文档字符串,但大多数对象没有,也根本不需要。对于实例属性,没有本地的文档字符串语法,因为它们可以在类的文档字符串中详细描述,这也是我建议你这样做的原因。类的实例通常也没有自己的文档字符串,枚举成员也只是如此。

当然,你几乎可以给任何东西添加文档字符串。事实上,你确实可以给几乎任何东西添加任何内容,因为这就是Python的设计方式。但这既不实用也不干净,即使像@Ethan Furman发布的那样,似乎为了给静态属性添加文档字符串而增加了太多的开销。

长话短说,尽管一开始可能不喜欢: 别这么做,使用枚举的文档字符串就足够解释其成员的含义了。


1
虽然我同意它们不是“必需的”,但文档字符串具有重要作用。一些枚举可能需要更详细的描述,这就是为什么会有这个问题存在的原因。总的来说,这个答案显示了对文档化代码目的的完全不理解。 - Error - Syntactical Remorse
@Error-SyntacticalRemorse 当然我理解记录代码的目的。但是我期望文档在一个共同的地方,对于类属性来说就是类的docstring。枚举是定义一组可能值的类。描述其目的以及任何可能值的含义的正确位置是枚举的docstring。想要构建一个允许意外和完全不常见的东西的黑客行为,显示出完全不理解标准和清晰代码的目的。;) - Bachsau
1
在枚举类型的情况下,应该同时都写(文档)。类应该被注释,但有时注释属性也非常有用。例如,HTTP响应代码。它们可以被存储为枚举,但是对于简单的枚举名称之外的代码含义进行注释会很有帮助。同样,正则表达式等内容也可以存储为枚举,并进行注释。 - Error - Syntactical Remorse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接