Skip to main content

Function pipelines for python

Project description

Tobacco: Your shiny new pipe >>!

Do you miss the %>% from R/dplyr? What about the |> from Julia? Now you can use a tobacco.pipe to chain together function calls! The @pipe decorator will take a function and repurpose the builtin right-bitshift operator >> to be a function pipe. I've hardly ever seen anyone using >>, so it doesn't seem likely many people will miss the default behavior.

A pipeline takes the value of a function call on the left-hand side, and passes it in as the first argument to the function on the right-hand side:

0 >> f >> g  # evaluates to g(f(0))

You can even specify keyword closures within the pipeline

# Evaluates to g(f(0, some_keyword=5), another="foo")
0 >> f(some_keyword=5) >> g(another="foo")

Installation

pip install tobacco

Usage

from tobacco import pipe


@pipe  # `pipe` overrides the builtin right-bitshift operator `>>`
def plus_one(a: int) -> int:
    return a + 1


@pipe
def minus_amount(b: int, amount: int = 2) -> int:
    return b - amount


@pipe
def plus_five(c: int) -> int:
    return c + 5


# 0 + 1 - 4 + 5 = 2
res = 0 >> plus_one >> minus_amount(amount=4) >> plus_five
print(res)  # 2

For very long data pipelines, this can clean up the readability a lot:

result = (
    load_data
    >> clean_data
    >> add_columns
    >> do_analysis
    >> upload_to_database
)

In this short example, result is the output of upload_to_database after all the prior steps completed.

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

tobacco-1.0.3.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

tobacco-1.0.3-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file tobacco-1.0.3.tar.gz.

File metadata

  • Download URL: tobacco-1.0.3.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for tobacco-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ce1065c02448d88e624ed929a1c672d956c185418f147864cf983e4bad9d52c4
MD5 4ffa265af33ba1dfdc757380968d0e49
BLAKE2b-256 02c9e4e7773599545d08705cdf8af0de41c1003108efb1c50023481cc722c001

See more details on using hashes here.

File details

Details for the file tobacco-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: tobacco-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for tobacco-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 459b933e6422b654b54f7f2f1e908e65bcc070c9bf47ce4c0959aba108fa2724
MD5 428e36c1807f81103604362268e4c652
BLAKE2b-256 ec01f4e3a975682c27d74aee1ffa961403ecb1697bc64e656813af413e2b90b3

See more details on using hashes here.

Supported by

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