default_enum_serializer()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 2
nop 4
1
from enum import EnumMeta
2
3
4
def default_enum_serializer(obj: EnumMeta,
5
                            *,
6
                            use_enum_name: bool = True,
7
                            **_) -> str:
8
    """
9
    Serialize the given obj. By default, the name of the enum element is
10
    returned.
11
    :param obj: an instance of an enum.
12
    :param use_enum_name: determines whether the name or the value should be
13
    used for serialization.
14
    :param _: not used.
15
    :return: ``obj`` serialized as a string.
16
    """
17
    attr = 'name' if use_enum_name else 'value'
18
    return getattr(obj, attr)
19