Skip to main content

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/

Metadata

Release files for with-partial 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for with-partial 1.0.1
File Size Uploaded
with_partial-1.0.1.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for with-partial 1.0.1
File Interpreter ABI Platform
with_partial-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.8 kB

Release files / with_partial-1.0.1.tar.gz

Download URL with_partial-1.0.1.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4201c0ad3790eeee4d653b60fde743b70d553043bf0bae900f7d54b278682766
BLAKE2b-256 checksum
How to use checksums
674783b5aff76fd9fc9d9c5aa75880c38e8af1eeffbd71a4b98f589e9c047892
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.7 Linux/5.13.0-19-generic

Release files / with_partial-1.0.1-py3-none-any.whl

Download URL with_partial-1.0.1-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
544898c98881a69ffce563f35df32933f29270156fafb837e800edace0816ca2
BLAKE2b-256 checksum
How to use checksums
a52f1d16144b4648efaa0388cca89596b3d20ce463a111178de5b29c8b143b58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.9.7 Linux/5.13.0-19-generic

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page