Skip to main content

A utility for functional piping in Python that allows you to access any function in any scope as a partial.

Project description

WithPartial

Introduction

WithPartial is a simple utility for functional piping in Python. The package exposes a context manager (used with with) called PipeContext, that allows you to access any function in any scope as a partial, meaning that it's naturally pipeable. Here's a contrived example from the test suite:

import numpy as np
from with_partial import PartialContext
from pipetools import pipe

with PartialContext() as _:
    ret = (
            10 > pipe |
            _.np.ones() |
            _.np.reshape(newshape=(5, 2)) |
            _.np.mean() |
            _.int()
    )
    assert ret == 1

As you can see, we were able to call both numpy and built-in functions on the _ object, and it executed the pipeline similarly to say R's magrittr package.

Installation

pip install with_partial

Usage

Actually WithPartial doesn't provide an actual piping mechanism, but it does add a useful syntax for use with pipes. For the actual piping mechanism, I suggest that you try pipetools, which this package is actually tested against.

WithPartial provides a single class: PipeContext. The way you use PipeContext is by first using it as a context manager:

with PipeContext() as _:

Then, using the return value of the context manager, which we have named _ (but you could call it anything), you access attributes and items (using .attr or ["key"] or [0]) to locate the function you want and then you finally call it (), which will create the partial. You can use positional and keyword arguments at this point if you need

For more usage information, refer to the test suite.

Tests

Note: you will need poetry installed.

git clone https://github.com/multimeric/WithPartial.git
cd WithPartial
poetry install --extras pipetools
poetry run pytest test/

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

with_partial-1.0.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

with_partial-1.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file with_partial-1.0.0.tar.gz.

File metadata

  • Download URL: with_partial-1.0.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.7 Linux/5.13.0-19-generic

File hashes

Hashes for with_partial-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8308a18c48b4a5d6ca1464a98a32a9a9c9c402975a9f4c72cfe6122d1184b320
MD5 626925b22c5aa3f4e2e84f99d7474df3
BLAKE2b-256 e80995d3078fbad081d06af4f1754a8c95a24640b4125b0352fba5e144e9bc49

See more details on using hashes here.

File details

Details for the file with_partial-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: with_partial-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.7 Linux/5.13.0-19-generic

File hashes

Hashes for with_partial-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5180cec2900069dca6155195636203035bd0ab151670c129921efb550970b611
MD5 5ce1f43fa8df120c284bcee4a32e5b5c
BLAKE2b-256 d58fd299dfe37ea55ae33111fae972ea369bc512cab39bdec0878218166fa092

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