Created
June 26, 2020 05:27
-
-
Save nicain/5b74e2d63026c134937e304d880b2da2 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
def get_registry_metaclass(registry_name, class_name="MetaRegistry"): | |
registration_method_name = 'register_class' | |
def new(meta, name, bases, class_dict): | |
cls = type.__new__(meta, name, bases, class_dict) | |
if name not in getattr(meta, registry_name): | |
getattr(meta, registration_method_name)(getattr(meta, registry_name), cls) | |
return cls | |
def register_class(registry, target_class): | |
registry[target_class.__name__] = target_class | |
return type(class_name, (type,), {registry_name: {}, | |
"__new__": new, | |
registration_method_name: staticmethod(register_class)}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment