Last active
May 24, 2021 17:52
-
-
Save titovanton/0c3e6cbe99bbd5edccf077850f877e00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
log = logging.getLogger(__name__) | |
def enum_reduce(enum_name, _enum, exclude): | |
""" | |
Returns Enum type, based on specified _enum, | |
but reduced by iterable object of keys. | |
Parameters | |
---------- | |
enum_name : str | |
Needs to be passed in enum_type(enum_name, bases, attrs) | |
_enum : Enum | |
A specific subclass of Enum | |
exclude : Iterable[str] | |
Attributes to exclude | |
Returns | |
------- | |
Enum | |
The same type as passed, but without specified items. | |
""" | |
keys = [ | |
item | |
for item in _enum._member_names_ | |
if item not in exclude | |
] | |
master_class = _enum.__bases__[0] | |
master_meta = type(_enum) | |
class GenericMeta(master_meta): | |
def __contains__(cls, member): | |
# only value does matter | |
return any(x.value == member for x in cls) | |
class GenericEnum(master_class, metaclass=GenericMeta): | |
pass | |
return GenericEnum(enum_name, { | |
key: (_enum[key].value, _enum[key].label) | |
for key in keys | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment