Passed
Pull Request — master (#17)
by Ramon
50s
created

jsons.serializers.default_enum   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 7
dl 0
loc 18
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A default_enum_serializer() 0 14 2
1
from enum import EnumMeta
2
3
4
def default_enum_serializer(obj: EnumMeta,
5
                            use_enum_name: bool = True,
6
                            **_) -> str:
7
    """
8
    Serialize the given obj. By default, the name of the enum element is
9
    returned.
10
    :param obj: an instance of an enum.
11
    :param use_enum_name: determines whether the name or the value should be
12
    used for serialization.
13
    :param _: not used.
14
    :return: ``obj`` serialized as a string.
15
    """
16
    attr = 'name' if use_enum_name else 'value'
17
    return getattr(obj, attr)
18