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)
  • To compose arguments in reverse order:

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

    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.1.tar.gz (3.5 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.1-py38-none-any.whl (4.1 kB view details)

Uploaded Python 3.8

compose-1.2.1-py35-none-any.whl (4.1 kB view details)

Uploaded Python 3.5

compose-1.2.1-py2.py30-none-any.whl (4.0 kB view details)

Uploaded Python 2Python 3.0

File details

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

File metadata

  • Download URL: compose-1.2.1.tar.gz
  • Upload date:
  • Size: 3.5 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.1.tar.gz
Algorithm Hash digest
SHA256 b6f8436a720d98f1075fe8374918e80c25fde1391a92ab16db93ac9188fac09b
MD5 eaf1b8645bc4a59da59255ebe6313b26
BLAKE2b-256 b5d8538e29ae7ed863aa9f031982c90e8a09887ae6eeb8168696dbb5fc5afdcb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: compose-1.2.1-py38-none-any.whl
  • Upload date:
  • Size: 4.1 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.1-py38-none-any.whl
Algorithm Hash digest
SHA256 701eaf7ef383cc3e64a056a91bb04459a82f5c1094c4473a5692fba502e3763c
MD5 e0fbc7275b210c9d47e5119e4b86d7bb
BLAKE2b-256 d653d68f481ae6d4bb95a7beb1650d0ff6f1cf9532b90f2dadec2e724e6f6691

See more details on using hashes here.

File details

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

File metadata

  • Download URL: compose-1.2.1-py35-none-any.whl
  • Upload date:
  • Size: 4.1 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.1-py35-none-any.whl
Algorithm Hash digest
SHA256 9f3fbe6442ce98eefa661bea46dd3751c2048fe2c3fa128c907a6c9de1ed2af9
MD5 e1e4152dabedd6f2a765a73a2ba9dd87
BLAKE2b-256 67e29f182e285bef19b97ce803840b8ddbacaa3b1d66d3886d2b399b36421194

See more details on using hashes here.

File details

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

File metadata

  • Download URL: compose-1.2.1-py2.py30-none-any.whl
  • Upload date:
  • Size: 4.0 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.1-py2.py30-none-any.whl
Algorithm Hash digest
SHA256 44eee4a0b12cf4ff63814b7e3194882c37aba467ccc9c15d8c936764d493f906
MD5 64a730471395539151123c95dc9aa9fc
BLAKE2b-256 b180c50ac9c95a5261dc2849adee967f540fd3d8313baeb3d8203f32f894c9e9

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