Skip to content

File: Broken/Core/BrokenEnum.py

Broken.Core.BrokenEnum

BrokenEnumBase

Source code in Broken/Core/BrokenEnum.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class BrokenEnumBase:

    @classmethod
    @functools.lru_cache()
    def get(cls, /, value: Union[str, enum.Enum, Any], default: Any=None) -> Optional[Self]:
        """Get enum members from their value, name or themselves"""

        # Value is already a member of the enum
        if isinstance(value, cls):
            return value

        # Attempt to get by value
        with contextlib.suppress(ValueError):
            return cls(value)

        # Attempt to get by key
        with contextlib.suppress(KeyError):
            return cls[value]

        return default

    # Values

    @classmethod
    def options(cls) -> tuple[enum.Enum]:
        """Get all options (Class.A, Class.B, ...) of the enum"""
        return tuple(cls)

    @classmethod
    def values(cls) -> tuple[Any]:
        """Get all 'values' of the enum (name=value)"""
        return tuple(member.value for member in cls)

    # Key/names properties

    @classmethod
    def keys(cls) -> tuple[str]:
        """Get all 'keys' of the enum (key=value)"""
        return tuple(member.name for member in cls)

    # Items and dict-like

    @classmethod
    def items(cls) -> tuple[tuple[str, Any]]:
        """Get the tuple of (name, value) of all members of the enum"""
        return tuple((member.name, member.value) for member in cls)

    @classmethod
    def as_dict(cls) -> dict[str, Any]:
        """Get the dictionary of all key=value of the enum"""
        return dict(cls.items())

    def __getitem__(self, index: int) -> enum.Enum:
        return self.members[index]

    # # Smart methods

    @functools.lru_cache()
    def next(self, value: Union[str, enum.Enum]=None, *, offset: int=1) -> Self:
        """Get the next enum member defined in the class"""
        cls   = type(self)
        value = cls.get(value or self)
        return cls.options()[(cls.options().index(value) + offset) % len(cls)]

    def __next__(self) -> Self:
        return self.next()

    @functools.lru_cache()
    def previous(self, value: Union[str, enum.Enum]=None, *, offset: int=1) -> Self:
        """Get the previous enum member defined in the class"""
        cls   = type(self)
        value = cls.get(value or self)
        return cls.options()[(cls.options().index(value) - offset) % len(cls)]

    # # Advanced functions

    def field(self, **kwargs: dict[str, Any]) -> attrs.Attribute:
        """Get an attrs.field() with this option as default and Enum.get as converter"""
        return attrs.field(
            default=self,
            converter=type(self).get,
            **kwargs
        )

    @classmethod
    def extend(cls, name: str, value: Any) -> Self:
        """Dynamically extend the enum with a new member (name=value)"""
        raise NotImplementedError("This method is not implemented yet")

get

get(
    value: Union[str, enum.Enum, Any], default: Any = None
) -> Optional[Self]

Get enum members from their value, name or themselves

Source code in Broken/Core/BrokenEnum.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@classmethod
@functools.lru_cache()
def get(cls, /, value: Union[str, enum.Enum, Any], default: Any=None) -> Optional[Self]:
    """Get enum members from their value, name or themselves"""

    # Value is already a member of the enum
    if isinstance(value, cls):
        return value

    # Attempt to get by value
    with contextlib.suppress(ValueError):
        return cls(value)

    # Attempt to get by key
    with contextlib.suppress(KeyError):
        return cls[value]

    return default

options

options() -> tuple[enum.Enum]

Get all options (Class.A, Class.B, ...) of the enum

Source code in Broken/Core/BrokenEnum.py
33
34
35
36
@classmethod
def options(cls) -> tuple[enum.Enum]:
    """Get all options (Class.A, Class.B, ...) of the enum"""
    return tuple(cls)

values

values() -> tuple[Any]

Get all 'values' of the enum (name=value)

Source code in Broken/Core/BrokenEnum.py
38
39
40
41
@classmethod
def values(cls) -> tuple[Any]:
    """Get all 'values' of the enum (name=value)"""
    return tuple(member.value for member in cls)

keys

keys() -> tuple[str]

Get all 'keys' of the enum (key=value)

Source code in Broken/Core/BrokenEnum.py
45
46
47
48
@classmethod
def keys(cls) -> tuple[str]:
    """Get all 'keys' of the enum (key=value)"""
    return tuple(member.name for member in cls)

items

items() -> tuple[tuple[str, Any]]

Get the tuple of (name, value) of all members of the enum

Source code in Broken/Core/BrokenEnum.py
52
53
54
55
@classmethod
def items(cls) -> tuple[tuple[str, Any]]:
    """Get the tuple of (name, value) of all members of the enum"""
    return tuple((member.name, member.value) for member in cls)

as_dict

as_dict() -> dict[str, Any]

Get the dictionary of all key=value of the enum

Source code in Broken/Core/BrokenEnum.py
57
58
59
60
@classmethod
def as_dict(cls) -> dict[str, Any]:
    """Get the dictionary of all key=value of the enum"""
    return dict(cls.items())

__getitem__

__getitem__(index: int) -> enum.Enum
Source code in Broken/Core/BrokenEnum.py
62
63
def __getitem__(self, index: int) -> enum.Enum:
    return self.members[index]

next

next(
    value: Union[str, enum.Enum] = None, *, offset: int = 1
) -> Self

Get the next enum member defined in the class

Source code in Broken/Core/BrokenEnum.py
67
68
69
70
71
72
@functools.lru_cache()
def next(self, value: Union[str, enum.Enum]=None, *, offset: int=1) -> Self:
    """Get the next enum member defined in the class"""
    cls   = type(self)
    value = cls.get(value or self)
    return cls.options()[(cls.options().index(value) + offset) % len(cls)]

__next__

__next__() -> Self
Source code in Broken/Core/BrokenEnum.py
74
75
def __next__(self) -> Self:
    return self.next()

previous

previous(
    value: Union[str, enum.Enum] = None, *, offset: int = 1
) -> Self

Get the previous enum member defined in the class

Source code in Broken/Core/BrokenEnum.py
77
78
79
80
81
82
@functools.lru_cache()
def previous(self, value: Union[str, enum.Enum]=None, *, offset: int=1) -> Self:
    """Get the previous enum member defined in the class"""
    cls   = type(self)
    value = cls.get(value or self)
    return cls.options()[(cls.options().index(value) - offset) % len(cls)]

field

field(**kwargs: dict[str, Any]) -> attrs.Attribute

Get an attrs.field() with this option as default and Enum.get as converter

Source code in Broken/Core/BrokenEnum.py
86
87
88
89
90
91
92
def field(self, **kwargs: dict[str, Any]) -> attrs.Attribute:
    """Get an attrs.field() with this option as default and Enum.get as converter"""
    return attrs.field(
        default=self,
        converter=type(self).get,
        **kwargs
    )

extend

extend(name: str, value: Any) -> Self

Dynamically extend the enum with a new member (name=value)

Source code in Broken/Core/BrokenEnum.py
94
95
96
97
@classmethod
def extend(cls, name: str, value: Any) -> Self:
    """Dynamically extend the enum with a new member (name=value)"""
    raise NotImplementedError("This method is not implemented yet")

BrokenEnum

Bases: BrokenEnumBase, enum.Enum

Source code in Broken/Core/BrokenEnum.py
 99
100
class BrokenEnum(BrokenEnumBase, enum.Enum):
    ...

MultiEnum

Bases: BrokenEnumBase, MultiValueEnum

Source code in Broken/Core/BrokenEnum.py
102
103
class MultiEnum(BrokenEnumBase, MultiValueEnum):
    ...

FlagEnum

Bases: BrokenEnumBase, Flag

Source code in Broken/Core/BrokenEnum.py
105
106
class FlagEnum(BrokenEnumBase, Flag):
    ...