Created
April 1, 2020 14:50
-
-
Save rrmerugu/fd24f447bb7e23d51c9be843dbe20219 to your computer and use it in GitHub Desktop.
register classes to a dictionary
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
registry = {} | |
def register_microservice(cls): | |
name = cls.__name__ | |
force_bound = False | |
if '__init__' in cls.__dict__: | |
cls.__init__.func_globals[name] = cls | |
force_bound = True | |
try: | |
registry[name] = cls() | |
finally: | |
if force_bound: | |
del cls.__init__.func_globals[name] | |
return cls | |
@register_microservice | |
class HelloWorld: | |
pass | |
print(registry) | |
# {'HelloWorld': <__main__.HelloWorld object at 0x107ddfbd0>} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment