Skip to main content

Abstract Factory design pattern classes for scalable data in dynamic environments.

Project description

Abstract Factories

A collection of classes to support the Abstract Factory design pattern, providing a clear abstraction layer for scalable data in dynamic environments.

  • Tested on Python 3.8 - 3.12
  • Functional on Python 2.7

    Wait - what? Python 2.7? What year is this?
    I often work professionally on legacy systems that are too fragile or large to update. Providing there's no functional or notable impact supporting 2.7, I have no reason to ignore its existence yet.


Installation

Clone the project locally.


Usage

Initialize AbstractTypeFactory or AbstractInstanceFactory with the abstract type.
Optionally, provide the attribute/method name to identify items by name (and optionally version).

from abstract_factories import AbstractTypeFactory, AbstractInstanceFactory

class AbstractVehicle(object):
    def __init__(self, make=None):
        self.make = make

    def start(self):
        raise NotImplementedError()

class Car(AbstractVehicle):
    def start(self):
        print('Vrooom...')

# Type Factory
type_factory = AbstractTypeFactory(AbstractVehicle)
type_factory.register_item(Car)
assert type_factory.get('Car') is Car

# Instance Factory
honda = Car('Honda')
instance_factory = AbstractInstanceFactory(AbstractVehicle, name_key='make')
instance_factory.register_item(honda)
assert instance_factory.get('Honda') is honda

Registration:

Register viable items directly.

  • type_factory.register_item(AbstractSubclass)
  • instance_factory.register_item(AbstractSubclass())

Find and register any viable items found in the module's locals.

  • type_factory/instance_factory.register_module(module)

Find and register any viable items found in any nested python file from a dynamic import. Some limitation using relative imports.

  • type_factory/instance_factory.register_path(r'c:/tools/tool_plugins')
  • type_factory/instance_factory.register_path(r'c:/tools/tool_plugins/plugin.py')

Practical Applications

Content Creation

Useful for managing production needs in Film, TV, and Games, allowing easy modifications and versioning of components.

Rigging

Easily support and modify rig component behaviours during production.

import sys
from abstract_factories import AbstractTypeFactory

class AbstractRigComponent:
    Name = 'AbstractRigComponent'
    Version = 0

    def build(self, **kwargs):
        raise NotImplementedError()

class IKChainComponent(AbstractRigComponent):
    Name = 'IKChainComponent'
    Version = 1

    def build(self, **kwargs):
        pass

# --------------------------------------------------------------------------
class RigComponentBuilder:
    def __init__(self):
        self.factory = AbstractTypeFactory(
            abstract=AbstractRigComponent,
            modules=[sys.modules[__name__]],
            name_key='Name',
            version_key='Version'
        )

    def build(self, component_data):
        results = []
        for data in component_data:
            component = self.factory.get(
                data.pop('type'), 
                version=data.pop('version', None),
            )
            instance = component()
            instance.build(**data)
            results.append(instance)
        return results

builder = RigComponentBuilder()
builder.build([
    {'type': 'IKChainComponent', 'name': 'arm'},
    {'type': 'IKChainComponent', 'name': 'leg', 'version': 2},
])

Testing

.tests/ directory contains examples for;

  • Adding, removing & comparing items directly.
  • Adding, removing & comparing items found in modules and/or paths.

Further Information

Abstract factories is influenced by https://github.com/mikemalinowski/factories.


License

This project is licensed under the MIT License.

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

abstract_factories-0.2.4.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

abstract_factories-0.2.4-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file abstract_factories-0.2.4.tar.gz.

File metadata

  • Download URL: abstract_factories-0.2.4.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for abstract_factories-0.2.4.tar.gz
Algorithm Hash digest
SHA256 03d63defc7a7d3bbdf5f59bde01ae70a3e808e3b001c7c24b65e1bebe048b70e
MD5 01c3179c850e96036835664794455b96
BLAKE2b-256 9ff385282c2d62dc82f5ba391f85c7974ca3458fd64a4752c7fd368f37148027

See more details on using hashes here.

File details

Details for the file abstract_factories-0.2.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for abstract_factories-0.2.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1669418f61aa84a3443a0a218191a99bc9413c6d00e52c200d9299df0823ee62
MD5 accaf167fa0e5470d98c250066790953
BLAKE2b-256 0f2bda628275698f53b4cb1a2407a9b61439c4270993f8de631912df7653ce6e

See more details on using hashes here.

Supported by

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