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.

An acompose variant is provided which can compose both regular and async functions into an async function.

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

Uploaded Python 3.8

compose-1.2.7-py35-none-any.whl (4.0 kB view details)

Uploaded Python 3.5

compose-1.2.7-py2.py30-none-any.whl (3.9 kB view details)

Uploaded Python 2Python 3.0

File details

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

File metadata

  • Download URL: compose-1.2.7.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for compose-1.2.7.tar.gz
Algorithm Hash digest
SHA256 63adc21c4e6afc5787b84e322a9b0cf48b9de9eec9f72f2dbd11d5cd2cbb90c0
MD5 925e20f618c6a6b82f88a16e91b3bb49
BLAKE2b-256 363149744c43a0985f55d31f02c8285d86f04819e1503fc93d66bdbd1638d6d5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: compose-1.2.7-py38-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for compose-1.2.7-py38-none-any.whl
Algorithm Hash digest
SHA256 508ab6ad85f9926b16fd8e3f4591b5fb303436592a66ff77e9f67cbb256d32ba
MD5 a492fb60235f0600b4a64f75411447d6
BLAKE2b-256 fbc882c83a47740fbb7893f990c555b659c7239714f03d795bfa6b97b3c5bf10

See more details on using hashes here.

File details

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

File metadata

  • Download URL: compose-1.2.7-py35-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3.5
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for compose-1.2.7-py35-none-any.whl
Algorithm Hash digest
SHA256 61a2212a26f8152778ba86709f851a364cdca0b0104683a92ef85ac9c2e638b9
MD5 a91f25ce4834e63d9e0b59e9858bc86c
BLAKE2b-256 83425e02726b0ab4fee4900f0b29beaba5885ff41c1a6d5ba44d2deb7d2467ff

See more details on using hashes here.

File details

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

File metadata

  • Download URL: compose-1.2.7-py2.py30-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 2, Python 3.0
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

File hashes

Hashes for compose-1.2.7-py2.py30-none-any.whl
Algorithm Hash digest
SHA256 59e3f748baf92b688536a9c65020e57583bcc0128fb097eed3753606d3a650d9
MD5 ce8668fc7cdbb31a59ce267c3bf38cf7
BLAKE2b-256 da432441ae00fe1b75f6dbb0b3591f9edc702fa0939705341ed1e3974fc2df05

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