classBasePlugin(object):"""Base class for our plugin."""defexecute(self,x:int,y:int)->int:"""Plugin performs operation on two integers and returns integer."""...
classBasePlugin(object):"""Base class for our plugin."""defexecute(self,x:int,y:int)->int:"""Plugin performs operation on two integers and returns integer."""...
Define a base class for our plugins. We have no particular requirements,
so we can derive it from object.
classBasePlugin(object):"""Base class for our plugin."""defexecute(self,x:int,y:int)->int:"""Plugin performs operation on two integers and returns integer."""...
Though docstrings are advisory, it will help to navigate our code,
so describe plugin tasks and features.
classBasePlugin(object):"""Base class for our plugin."""defexecute(self,x:int,y:int)->int:"""Plugin performs operation on two integers and returns integer."""...
Our main function of the plugin, do not to forget to place the proper type hints to allow
static type checking.
classBasePlugin(object):"""Base class for our plugin."""defexecute(self,x:int,y:int)->int:"""Plugin performs operation on two integers and returns integer."""...
The main function of the plugin already should be documented.
classBasePlugin(object):"""Base class for our plugin."""defexecute(self,x:int,y:int)->int:"""Plugin performs operation on two integers and returns integer."""...
Ellipses operator (...) shows the implementation is abstract.
If missed, mypy
will complain the function doesn't return an integer value.
Then let's create a loader instance. You need only one loader instance per each type for your application. So loaders are usually singletons.
Loader is the generic type, so we must pass the exact plugin type. In the subclass scheme
plugins are classes, derived from the BasePlugin class. In Python's typing terms,
the subclass of BasePlugin has the Type[BasePlugin] type. We'd placed the type into
the brackets just after the Loader.
After defining the plugin's type, we need to initialize the loader itself.
Loader has several initialization parameters, see Reference
for details. Here we consider our plugins will be in plugins folder of our applications.
Loader returns the class. We create the instance to show we can use some plugin initialization
tasks. We can also define the execute method as a @classmethod to skip
the initialization step.