Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
pipe operation in python
Pipe
classfrom pipable import Pipe
list = Pipe(list)
"abc" | list # ["a", "b", "c"]
|
operatorfunctools.partial
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!!
@Pipe
decorator transforms function into Pipe object# 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 !!
>>
operator to pass-in variable length arguments@Pipe
def kebab(*args):
return "-".join(args)
["a", "b"] >> kebab # "a-b"
<<
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"
Pipe operation is a handy feature in functional programming. It allows us to:
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.
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:
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'
FAQs
pseudo pipe operation in python
We found that pipable demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.