Skip to main content

The classic ``compose``, with all the Pythonic features.

Project description

The classic compose, with all the Pythonic features.

This compose follows the lead of functools.partial and returns callable compose objects which:

  • have a regular and unambiguous repr,

  • retain correct signature introspection,

  • allow introspection of the composed callables,

  • can be type-checked,

  • can be weakly referenced,

  • can have attributes,

  • will merge when nested, and

  • can be pickled (if all composed callables can be pickled).

This compose also fails fast with a TypeError if any argument is not callable, or when called with no arguments.

This module also provides an acompose which can compose both regular and async functions.

Versioning

This library’s version numbers follow the SemVer 2.0.0 specification.

Installation

pip install compose

Usage

Import compose:

from compose import compose

All the usual function composition you know and love:

>>> def double(x):
...     return x * 2
...
>>> def increment(x):
...     return x + 1
...
>>> double_then_increment = compose(increment, double)
>>> double_then_increment(1)
3

Of course any number of functions can be composed:

>>> def double(x):
...     return x * 2
...
>>> times_eight = compose(douple, double, double)
>>> times_16 = compose(douple, double, double, double)

We still get the correct signature introspection:

>>> def f(a, b, c=0, **kwargs):
...     pass
...
>>> def g(x):
...     pass
...
>>> g_of_f = compose(g, f)
>>> import inspect
>>> inspect.signature(g_of_f)
<Signature (a, b, c=0, **kwargs)>

And we can inspect all the composed callables:

>>> g_of_f.functions  # in order of execution:
(<function f at 0x4048e6f0>, <function g at 0x405228e8>)

When programmatically inspecting arbitrary callables, we can check if we are looking at a compose instance:

>>> isinstance(g_of_f, compose)
True

We can do all of the above with async functions mixed in by using acompose instead of compose:

from compose import acompose

Recipes

  • If you want composing zero functions to be the identity function:

    def identity(x):
        return x
    
    icompose = partial(compose, identity)
  • If you want the functions attribute to be cached:

    import functools
    
    class ccompose(compose):
        @functools.cache_property
        def functions(self):
            return super().functions
  • Compose arguments in opposite order (useful with things like functools.partial, or just more intuitive in some cases):

    def rcompose(*functions):
        return compose(*reversed(functions))
  • When you need a regular function instead of a callable class instance (for example, when you want to define a method using compose):

    def fcompose(*functions):
        composed = compose(*functions)
        return lambda *args, **kwargs: composed(*args, **kwargs)

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

compose-1.2.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distributions

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

compose-1.2.0-py38-none-any.whl (4.5 kB view details)

Uploaded Python 3.8

compose-1.2.0-py35-none-any.whl (4.6 kB view details)

Uploaded Python 3.5

compose-1.2.0-py2.py30-none-any.whl (4.4 kB view details)

Uploaded Python 2Python 3.0

File details

Details for the file compose-1.2.0.tar.gz.

File metadata

  • Download URL: compose-1.2.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for compose-1.2.0.tar.gz
Algorithm Hash digest
SHA256 56a71755ccff404b3bee9c19473c9665c1244288e377b9114ec8740b06b2d33b
MD5 924147d501a148fcabdccbb9d63991c9
BLAKE2b-256 43ccb5ebb5094271a47e7f7f6682a4fc3443c66339ff2871039ffd4924de2e5a

See more details on using hashes here.

File details

Details for the file compose-1.2.0-py38-none-any.whl.

File metadata

  • Download URL: compose-1.2.0-py38-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for compose-1.2.0-py38-none-any.whl
Algorithm Hash digest
SHA256 5a15efbc59fde70da34edc10b57d1afc9737c62a08ea266dcd3d9b5d5a4511f5
MD5 9581887695b5bf0f01a339cef98d5371
BLAKE2b-256 9beb78add41f40373157cb72d67ca3e04d06934ca8999dc12a36e7ee6efe2524

See more details on using hashes here.

File details

Details for the file compose-1.2.0-py35-none-any.whl.

File metadata

  • Download URL: compose-1.2.0-py35-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3.5
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for compose-1.2.0-py35-none-any.whl
Algorithm Hash digest
SHA256 b6559277cbe0f21cc5469a7351b2764f529a6dd498312c50e7dbe3874174223d
MD5 2d10391fc5030ffeb5e083e9c7935131
BLAKE2b-256 134a0d46dbfdcbbd5ae92c0001a3148340941ab62371ed008a0c2d440aed315f

See more details on using hashes here.

File details

Details for the file compose-1.2.0-py2.py30-none-any.whl.

File metadata

  • Download URL: compose-1.2.0-py2.py30-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 2, Python 3.0
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for compose-1.2.0-py2.py30-none-any.whl
Algorithm Hash digest
SHA256 ac8f8726fe6bad1cc3e6d830a04b4268bba6219a84cc6c21f83f7da5ba23da4c
MD5 b65ccfb46adadecf727dac70475d5c52
BLAKE2b-256 7d76cdf1de1ef8ce1e322f71bd3658cbaf776cee8bb850e8b3f554d5e393c080

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