pseudo pipe operation in python
Project description
Pipable
pipe operation in python
Quick Start
Create the Pipe Object
- instantiate with the
Pipe
class
from pipable import Pipe
list = Pipe(list)
"abc" | list # ["a", "b", "c"]
Pipe Object is Partial with Infix Operator
- at the core Pipe create partial function while overriding it's
|
operator - instantiate Pipe object like the built-in
functools.partial
- preceding output will be assigned to the last positional argument of the Pipe object
square = Pipe(pow, exp=2)
3 | square # 9
Since that Pipe appends preceding output to the last positional argument,
assigning 1st argument with keyword will raise exception.
This behave the same as functools.partial
base2 = Pipe(pow, 2) # positional arg ok
3 | base2 # 8
base2 = Pipe(pow, base=2) # keyword arg don't
3 | base2 # raise!!
Using Decorator
@Pipe
decorator transforms function into Pipe object- preceding output will be assigned to the last positional argument
- instantiate Pipe decorated function similar to creating partial
# only one argument
@Pipe
def hi(name: str) -> str:
return f"hi {name}"
"May" | hi # "hi May"
# multiple arguments
@Pipe
def power(base: int, exp: int) -> int:
return base ** exp
# instantiate Pipe obj by partially calling the function
2 | power(3) # 9, note we need to use positional argument here
2 | power(exp=3) # 8, subsequent arguments can use keyword
# assign the 1st argument with keyword will raise exception
2 | power(base=3) # raise !!
Passing Variable Length Arguments
- use
>>
operator to pass-in variable length arguments
@Pipe
def kebab(*args):
return "-".join(args)
["a", "b"] >> kebab # "a-b"
- use
<<
operator to pass variable length keyword arguments
@Pipe
def concat(**kwargs):
return ", ".join([f"{k}-{v}" for k, v in kwargs.items()])
dict(b="boy", c="cat") << concat # "b-boy, c-cat"
- refer the docs for details
Motivation
Pipe operation is a handy feature in functional programming. It allows us to:
- write more succinct and readable code
- create less variables
- easily create new function by chaining other functions
However it's still a missing feature in Python as of 2023. This package try to mimic pipe operation by overriding the bitwise-or operator, and turn any function into pipable partial.
There are packages, such as pipe take the similar approach. It works great with iterables, and create pipe as iterator, ie. open pipe). However, I simply want to take preceding expression as an input argument of the current function then execute it, ie. close pipe. It leads to the creation of this package.
FAQ
How can I assign value to the first argument?
use a wrapper function
square = Pipe(lambda x: pow(x, 2))
3 | square # 9
Can I create open pipe?
Pipe
only create closed pipe, ie. execute the function after piping with the |
operator. You may consider other solutions such as:
- pipe, which create open pipe for iterators
- Coconut, a python variant that embrace functional programming
Can I append the preceding output at the beginning of the argument list?
Put the preceding output as the 1st argument of a wrapper function
# prepend is the default behaviour
def kebab(*args):
return "-".join(*args)
'a' | Pipe(kebab, 'b', 'c') # 'b c a'
@Pipe
def wrapper(first, others):
return kebab(first, *others)
'a' | wrapper(others=['b', 'c']) # 'a b c'
Need Help?
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
Built Distribution
File details
Details for the file pipable-0.3.4.tar.gz
.
File metadata
- Download URL: pipable-0.3.4.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78329ad961cad82bf418e2cc1d4768aa45a76c60e96987983f1212bba5e81c54 |
|
MD5 | d97490ea44bed9004bf319142d239ffb |
|
BLAKE2b-256 | 9aa8b5cb97ac20c014dc20aad5813abe38e5a0605c18da659404e44fbe01d16e |
File details
Details for the file pipable-0.3.4-py3-none-any.whl
.
File metadata
- Download URL: pipable-0.3.4-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30b504f44081da56f4830bfffc3d2c80e611365a13c37fa7ea75e884b7bcf9b3 |
|
MD5 | fe41ff82b698a780ea55c53f0d317e56 |
|
BLAKE2b-256 | cc6cd41ee88a756aa6fe4039ef06877472e9c3e9edbcd9e3bd6f48877981d1dc |