Skip to main content

A python package exposing the factory/plugin design pattern

Project description

Installation

You can either clone or download this github repo, or alternatively you can install this via pip:

pip install factories

Overview

factories is a module which exposes a take on the Factory/Plugin design pattern. The idea behind this pattern is to be able to define a structure which your functionality sits within - allowing you to call that functionality without ever really knowing what it is doing.

This approach is particularly useful when building systems which are likely to expand in unknown ways over time. Example use cases might include:

  • Toolboxes, where each tool is represented as a plugin - and an interface which is arbitrarily populated with those tools

  • Node graphs, where we have no up-front knowledge of what nodes may be available to use

  • Data parsers which include data that changes format over time due to deprecation, meaning each data type can be represented by a plugin allowing the framework to never care about the storage details of the data

The commonality between all these structures is that the core of each system needs to do something but it does not have to care about the detail of how that task is achieved. Instead the detail is held within plugins libraries which can be expanded and contracted over time.

This pattern is incredibly useful but tends to come with an overhead of writing dynamic loading mechanisms and functionality to easily interact and query the plugins. The Factories module aims to diminish that overhead - allowing you to focus on your end goal and the development of plugins.

Quick Example

To utilise a factory we first need to declare what the factory will contain. This is class where you define exactly what functionality should be adhered to - consider this to be the base class of all your plugins.

class Tool(object):
    name = 'unknonw'
    
    def activate(self):
        return None
       
    def about(self):
        return ''

In this example we're defining a tool as a class which has a name attribute and two methods. Now we can instance a factory giving our base class.

import factories

factory = factories.Factory(
    abstract=Tool,
    plugin_identifier='name',
)

Note here that we're giving the Factory the Tool class type - it uses this when searching to know what to look for. We also give it an (optional) identifier, this can be used to request specific plugins from the factory.

We can now start adding paths to our factory. The factory will immediately search these locations looking for plugins (any classes which inherit form the Tool clas).

# -- Register with a hard coded path
factory.add_path(
    'c:/some/plugin/location'
)

# -- Register relatively
factory.add_path(
    os.path.join(
        os.path.dirname(__file__),
        'our_plugins',
    ),
)

We can now begin to interact with our factory

# -- Perhaps we're dealing with something  obscure, like a 
# -- UI where we do not know exactly what is needed up front.
# -- So we add a button for each tool
for tool_name in factory.identifiers():
    ui.addButton(tool_name)
   
# -- In a scenario where we connect the click event to a function
# -- which passes the tool name
def button_click(button):
    tool = factory.request(button.text())
    tool().activate()

In this example our tool box has no pre-conception of what tools it contains, instead it is populated dynamically using the factory and we only instance the tool at the time the user is actually requesting it to be activated.

Further Examples

Factories comes with two additional examples. One which shows how plugins can be used to parse data - and giving authority to the plugins to determine which should parse what rather than the core functionality. The other example is a demonstration of using plugins to represent 'animals in a zoo'.

These examples live under factories.examples.reader and factories.examples.zoo respectively.

Testing and Stability

This module comes with a suite of unit tests which give a 96% coverage. It is therefere highly recommended that you run these tests prior to making any alterations, and again before putting forward fixes or contributions.

Whilst every effort goes into stability, given that this is a relatively new module it is always appreciated if you can communicate any bugs or issues to mike.malinowski@outlook.com

Compatability

This has been tested under Python 2.7.13 and Python 3.6.6 under both Windows and Ubuntu.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

factories-1.4.1.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

factories-1.4.1-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

Details for the file factories-1.4.1.tar.gz.

File metadata

  • Download URL: factories-1.4.1.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for factories-1.4.1.tar.gz
Algorithm Hash digest
SHA256 bcfa96c5e77ed4903532ad0cd181ce088e433b424af6892c3f76ac511def929c
MD5 81d10dff683a01a6784681effbedd7df
BLAKE2b-256 9ff576c0228c7282ff89410d011518f98aaff8dc0268e665c151d8ab64a4ee69

See more details on using hashes here.

File details

Details for the file factories-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: factories-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 27.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for factories-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd674268f20bf5ed25c08092860fd1c2ceeb98f61dc7ec6a46fcbbd61fe4fbac
MD5 1a1248fa84bf8ce8c231cb74b63bee75
BLAKE2b-256 ce21722276fb41e84ebac8ede72a8f091e784be2e15ea220cd7425402fa1726d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page