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 compose even provides an acompose variant which does the right thing with async code.

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(double, 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 compose async code by using acompose:

>>> import asyncio
>>> from compose import acompose
>>>
>>> async def get_data():
...     await asyncio.sleep(0)
...     return 42
...
>>> get_and_double_data = acompose(double, get_data)
>>> asyncio.run(get_and_double_data())
84

Of course we can compose any number of async and regular functions, in any order:

>>> async def async_double(x):
...     await asyncio.sleep(0)
...     return x * 2
...
>>> async_times_16 = acompose(async_double, double, async_double, double)
>>> asyncio.run(async_times_16(1))
16

compose and acompose instances are distinct types:

>>> isinstance(async_times_16, acompose)
True
>>> isinstance(async_times_16, compose)
False
>>> isinstance(g_of_f, acompose)
False

Recipes

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

    def identity(x):
        return x
    
    icompose = partial(compose, identity)
  • To compose arguments in reverse order:

    def rcompose(*functions):
        return compose(*reversed(functions))
  • When you need composition to return a regular Python function:

    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.8.tar.gz (5.0 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.8-py38-none-any.whl (4.2 kB view details)

Uploaded Python 3.8

compose-1.2.8-py35-none-any.whl (4.2 kB view details)

Uploaded Python 3.5

compose-1.2.8-py2.py30-none-any.whl (4.1 kB view details)

Uploaded Python 2Python 3.0

File details

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

File metadata

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

File hashes

Hashes for compose-1.2.8.tar.gz
Algorithm Hash digest
SHA256 f36e4fb7d1bfc359b4a8a50c4a032b48dbbd3e682f5878fe1a7f0591b1569a22
MD5 0a5b1d1afb529fa843634fb46c96a1b5
BLAKE2b-256 7c233360062b4f76fa4141b28966655ded1c92c4a30dd1f1471096393812b863

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for compose-1.2.8-py38-none-any.whl
Algorithm Hash digest
SHA256 fb0d8c876528d2beb63a25c58b581d525dabb772f5d3b3da3cef4c87cb0c65be
MD5 7a519ebbabf725b906e67dfe8a95927c
BLAKE2b-256 05c251368d1deae7d25ed6e8bb721dde02be8541efd1c745e08e32a51dd42531

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for compose-1.2.8-py35-none-any.whl
Algorithm Hash digest
SHA256 3ae6b888e45238b31633313e78f4d64c79a13f3859efd04acfd1241c2afe7b74
MD5 06501fdf6f367a5ce3f8bee5f6f84790
BLAKE2b-256 2e737237c3ee577ddaffe4aa6ab12a00396b6d62c9ac673a455dca10597d467b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for compose-1.2.8-py2.py30-none-any.whl
Algorithm Hash digest
SHA256 cf17e985b82f06247143dccd1a32e5deb16f5b8ef80d732386acb2394092c5c5
MD5 4b0b9186ebcc44c000c98aecf2681e7d
BLAKE2b-256 e099a071fe93578fbe08b8f0a15e83bc1b1883ae80e89acfd89bd6f9f37ac6e1

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