-
-
Save h0rn3t/0dcd72ea5c71a516dd9dda06748808b7 to your computer and use it in GitHub Desktop.
Plugin pattern for python classes
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
plugins = {} | |
def get_input_plugins(): | |
return plugins['input'].items() | |
class Plugin(object): | |
plugin_class = None | |
@classmethod | |
def register(cls, name): | |
plugins[cls.plugin_class][name] = cls | |
class InputPlugin(Plugin): | |
plugin_class = 'input' | |
def process_input(self, something): | |
raise NotImplementedError | |
class ExamplePlugin(InputPlugin): | |
def process_input(self, something): | |
return str(something) | |
ExamplePlugin.register('example') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ExamplePlugin.register('example')
Why not just use cls.name as name argument? Then we just call ExamplePlugin.register()