Skip to main content

A python package exposing the class composition design pattern

Project description

XComposite Overview

This module exposes the composite design pattern in an easy to use way which attempts to minimalise the repetitive overhead.

The composite design pattern is an alternative to top-down inheritance when there is no clear hierarchical chain. Examples might include assigning roles to entities - where an entity can have any variation of roles.

Methods between composite parts can return all manor of variable types, therefore xcomposite gives you a library of decorators which you can utilise to define how the collective set of results should be wrangled and returned.

Installation

You can install this using pip:

pip install xcomposite

Alternatively you can get the source from: https://github.com/mikemalinowski/xcomposite

Examples

You can utilise this pattern like this:

.. code-block:: python

    >>> import xcomposite
    >>>
    >>>
    >>> # -- The composition class defines the decoration rules for
    >>> # -- each method which requires compositing. This must inherit
    >>> # -- from the xcomposite.Composition class.
    >>> class A(xcomposite.Composition):
    ...
    ...     @xcomposite.extend_results
    ...     def items(self):
    ...         return ['a', 'b']
    >>>
    >>>
    >>> # -- The class(es) being bound to the composition do not need
    >>> # -- to inherit from the composition. Equally their functions
    >>> # -- do not need to be decorated either
    >>> class B(object):
    ...
    ...     def items(self):
    ...         return ['x', 'y']
    >>>
    >>>
    >>> # -- We instance the composition, then bind any amount of classes
    >>> # -- to that composition. All classes being bound to a composition
    >>> # -- *must* be class instances.
    >>> a = A()
    >>> a.bind(B())
    >>>
    >>> # -- Call the items method, noting that the result is the expected
    >>> # -- list of items from the 'items' call of both A and B
    >>> # -- The composition cycles through all the bound classes, and 
    >>> # -- where it finds a class with the same method name it will be
    >>> # -- called.
    >>> print(a.items())

Another, similiar example might be:

    >>> import xcomposite
    >>>
    >>>
    >>> # -- Inheriting off the composition class means that your class can
    >>> # -- immediately bind any other class which is of a Composition type.
    >>> # -- You should declare (through composite decorators) what the
    >>> # -- expactation is of any bound methods. This allows you to tailor
    >>> # -- exactly how the results should be combined/returned.
    >>> class Definition(xcomposite.Composition):
    ...
    ...     @xcomposite.extend_results
    ...     def items(self):
    ...         return ['a', 'b']
    >>>
    >>>
    >>> class MyObject(object):
    ...
    ...     def items(self):
    ...         return ['x', 'y']
    >>>
    >>> 
    >>> class MyOtherObject(object):
    ...
    ...     def items(self):
    ...         return [1, 2]
    >>>
    >>>
    >>> # -- Instance any one of the classes, and bind it to the instance
    >>> # -- of the other
    >>> definition = Definition()
    >>> definition.bind(MyObject())
    >>> definition.bind(MyOtherObject())
    >>>
    >>> # -- Call the items method, noting that the result is the expected
    >>> # -- list of items from the 'items' call of both A and B
    >>> print(definition.items())
    ['a', 'b', 'x', 'y', 1, 2]

Decorators

All composition rules are defined as decorators which you can apply to your methods on your classes. The following decorators:

take_min
take_max
take_sum
take_range
take_average
take_first
take_last
any_true
any_false
absolute_true
absolute_false
append_unique
append_results
extend_results
extend_unique
update_dictionary

Restrictions

  • Version 2.0.0 onward is significantly different to version 1.x, and is therefore not compatible without changes.

  • All methods decorated with xcomposite decorators are expected to be instance methods and not class methods.

  • Functions which are decorated with xcomposite decorators may be decorated with other decorators, but any additional decorators should sit atop of the xcomposite decorator.

Testing and Stability

There are currently unittests which cover most of composite's core, but it is not yet exhaustive.

Compatability

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

Contribute

If you would like to contribute thoughts, ideas, fixes or features please get in touch! mike@twisted.space

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

xcomposite-2.0.8.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xcomposite-2.0.8-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file xcomposite-2.0.8.tar.gz.

File metadata

  • Download URL: xcomposite-2.0.8.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for xcomposite-2.0.8.tar.gz
Algorithm Hash digest
SHA256 ddabe99ae340da6cdeb7d223981e0abfceab2794a0e37acbed9ace755c1cd37c
MD5 9dbbabebd2446e5cd677c8ab6f1548dd
BLAKE2b-256 d473c8529b04c5edeb7f79718cd251ecdd9a10aab89621dc861eae4a939e6ac8

See more details on using hashes here.

File details

Details for the file xcomposite-2.0.8-py3-none-any.whl.

File metadata

  • Download URL: xcomposite-2.0.8-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for xcomposite-2.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 670f494ebac8674e492d7600630a145a8aa6becd861c22a3ae38034c07935510
MD5 30a87ed638920002b96bb60083283c8a
BLAKE2b-256 2b0f26a905d88f59b3ab2a9ee420b36727c55ff929d3a21aefdb750e13faefcc

See more details on using hashes here.

Supported by

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