Python enum 模块,auto() 实例源码

我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用enum.auto()

项目:enumap    作者:TadLeonard    | 项目源码 | 文件源码
def test_repr_sparse_typed():
    """Make sure that SparseEnumapMeta's __repr___ method works
    with typed fields
    """
    class Tools(SparseEnumap):
        head = default("your head")
        horse: float = default(3.14)
        donkey: int = auto()
        spatula = 100  # this isn't a default

    # None defaults are not explicitly shown for readability
    assert repr(Tools) == """Tools(
    head = 'your head',
    horse: float = 3.14,
    donkey: int,
    spatula
)"""
项目:pyrealtime    作者:ewhitmire    | 项目源码 | 文件源码
def auto() -> int:
        global __my_enum_auto_id
        i = __my_enum_auto_id
        __my_enum_auto_id += 1
        return i
项目:pg_query    作者:lelit    | 项目源码 | 文件源码
def int_enum_value_factory(index, enumerator):
    if enumerator.value is None:
        return '0' if index == 0 else 'auto()'

    if isinstance(enumerator.value, c_ast.BinaryOp):
        return emit_binary_op(enumerator.value)
    elif isinstance(enumerator.value, c_ast.Constant):
        return emit_constant(enumerator.value)
    elif isinstance(enumerator.value, c_ast.UnaryOp):
        return emit_unary_op(enumerator.value)
    elif enumerator.value.name == 'PG_INT32_MAX':
        return '0x7FFFFFFF'

    assert enumerator.value.type == 'int'
    return enumerator.value.value
项目:enumap    作者:TadLeonard    | 项目源码 | 文件源码
def __init__(self, default_value=None):
        self._value = (enum.auto.value, default_value)
项目:enumap    作者:TadLeonard    | 项目源码 | 文件源码
def _iter_member_defaults(members):
    """Iterates through Enum members and teases out the default value
    the user selected with `default(<user's special value>)` from the
    `default` object.
    """
    for k, v in members.items():
        if isinstance(v.value, default):
            yield k, v.value.default

        # By not yielding k, v for non-default() objects, we avoid using
        # things like auto() as defaults in our .tuple()/.map() collections.
        # This makes it explicit when a user is using an enum value
        # as a default while ALSO allowing SparseEnumaps to adhere to the
        # rules of Enums. Each value of an Enum must be unique, and those that
        # aren't are basically just aliases
项目:enumap    作者:TadLeonard    | 项目源码 | 文件源码
def test_type_cast_exception():
    """Make sure our type casting exceptions are informative"""
    class A(Enumap):
        a: int = auto()
        this_here_is_a_bad_key: int = auto()

    assert A.tuple_casted("1", "2") == (1, 2)
    with pytest.raises(TypeCastError) as e:
        A.tuple_casted("1", None)
    assert "'this_here_is_a_bad_key' got invalid value 'None'" in str(e)
    assert "of type NoneType" in str(e)
    assert e.value.key == "this_here_is_a_bad_key"
项目:enumap    作者:TadLeonard    | 项目源码 | 文件源码
def test_type_cast_exception_non_nonetype():
    """Make sure our type casting exceptions are informative"""
    class A(Enumap):
        a: int = auto()
        this_here_is_a_fine_key = auto()

    with pytest.raises(TypeCastError) as e:
        A.tuple_casted("1.0", None)

    assert "'a' got invalid value '1.0'" in str(e)
    assert "of type str" in str(e)
    assert e.value.key == "a"
项目:enumap    作者:TadLeonard    | 项目源码 | 文件源码
def test_type_cast_exception_sparse():
    class A(SparseEnumap):
        a: int = default(1)
        b: float = default(2.0)
        a_fine_key = auto()
        c = default("a pastry")

    assert A.tuple_casted() == (1, 2.0, None, "a pastry")

    with pytest.raises(TypeCastError) as e:
        A.tuple_casted("1.0")

    assert "'a' got invalid value '1.0'" in str(e)
    assert "of type str" in str(e)
    assert e.value.key == "a"
项目:enumap    作者:TadLeonard    | 项目源码 | 文件源码
def test_str_typed():
    """Make sure that EnumapMeta's __str___ method works with typed fields"""
    class Tools(Enumap):
        head = auto()
        horse: float = auto()
        donkey: int = auto()
        spatula = auto()

    assert str(Tools) == "Tools(head, horse: float, donkey: int, spatula)"
项目:enumap    作者:TadLeonard    | 项目源码 | 文件源码
def test_str_sparse_typed():
    """Make sure that EnumapMeta's __str___ method works with typed fields"""
    class Tools(SparseEnumap):
        head = default("your head")
        horse: float = default(3.14)
        donkey: int = auto()
        spatula = default(1)

    assert str(Tools) == "Tools(head = 'your head', horse: float = 3.14, donkey: int, spatula = 1)"  # noqa