From 95650ddd83b95c7ba7224531a41908fc6316ac67 Mon Sep 17 00:00:00 2001 From: Ra Date: Wed, 17 Aug 2022 18:54:56 -0600 Subject: [PATCH] --- extendable.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 extendable.py diff --git a/extendable.py b/extendable.py new file mode 100644 index 0000000..ae139e5 --- /dev/null +++ b/extendable.py @@ -0,0 +1,50 @@ +from enum import IntFlag, EnumMeta + + +class BaseExt(IntFlag): + __extending__ = None + + @classmethod + def append(cls, members: list[tuple[str, int]]): + _members = BaseExt(cls.__name__, members) + for member in _members: + cls._member_names_.append(member.name) + cls._value2member_map_[member.value] = member + cls._member_map_[member.name] = member + + +class Extendable(EnumMeta): + def __subclasscheck__(cls, sub): + return issubclass(sub, cls.__bases__[0]) + + def __new__(cls, name, bases, kwds, **kwargs): + ext = kwargs.get("extending", None) + extends = [(m.name, m.value) for m in ext] if ext else [] + new_cls = BaseExt( + name, [(k, v) for k, v in kwds.items() if not k.startswith("_")] + extends + ) + new_cls.__extending__ = ext if ext else None + return new_cls + + +class First(BaseExt, metaclass=Extendable): + B = 0x0 + C = 0x1 + D = 0x2 + E = 0x4 + F = 0x8 + G = 0x10 + H = 0x20 + I = 0x40 + + +class Second( + BaseExt, + extending=First, + metaclass=Extendable, +): + BLIND = 0x80 + DYING = 0x100 + + +First.append([("J", 1000)])