Created
January 12, 2012 10:17
-
-
Save BradWhittington/1599710 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__
asname
argument? Then we just callExamplePlugin.register()