Implementing a plug-in mechanism

dn PythonList at DancesWithMice.info
Wed Mar 15 14:45:18 EDT 2023


On 16/03/2023 01.47, Loris Bennett wrote:
> I have written a program which, as part of the non-core functionality,
> contains a module to generate email.  This is currently very specific
> to my organisation, so the main program contains
> 
>    import myorg.mailer
> 
> This module is specific to my organisation in that it can ask an
> internal server to generate individualised salutations for a given UID
> which is known within the organisation.
> 
> I want to share the code with other institutions, so I would like to
> 
>    1. replace the organisation-specific mailer with a generic one
>    2. allow an organisation-specific mailer to be used instead of the
>       generic one, if so desired

This may call for the plug-in pattern, ie the user will choose whether 
to plug-in the specific, or the generic, module.

In Python, we would tend to use a Dependency Injection approach (one of 
Uncle Bob's SOLID principles).

There's a rather abstract description of the plugin pattern at 
https://martinfowler.com/eaaCatalog/plugin.html

OpenClassrooms has a more practical discussion at 
https://openclassrooms.com/en/courses/6397806-design-your-software-architecture-using-industry-standard-patterns/6896171-plug-in-architecture

There is a PyPi library called pluggy (not used it). I've used informal 
approaches using an ABC as a framework/reminder (see @George's response).

-- 
Regards,
=dn


More information about the Python-list mailing list