Simple Smart Pipe Operator
Project description
Simple Smart Pipe
SSPipe is a python productivity-tool for rapid data manipulation in python.
It helps you break up any complicated expression into a sequence of simple transformations, increasing human-readability and decreasing the need for matching parentheses!
If you're familiar with
| operator
of Unix, or
%>% operator
of R's magrittr, or
DataFrame.pipe
method of pandas library, sspipe provides the same functionality
for any object in python.
Installation and Usage
Install sspipe using pip:
pip install --upgrade sspipe
Then import it in your scripts.
from sspipe import p, px
The whole functionality
of this library is exposed by two objects p (as a wrapper for functions to
be called on the piped object) and px (as a placeholder for piped object).
Examples
| Description | Python expression using p and px |
Equivalent python code |
|---|---|---|
| Simple function call |
"hello world!" | p(print) |
X = "hello world!"print(X) |
| Function call with extra args |
"hello" | p(print, "world", end='!') |
X = "hello"print(X, "world", end='!') |
| Explicitly positioning piped argument with px placeholder |
"world" | p(print, "hello", px, "!") |
X = "world"print("hello", X, "!") |
| Chaining pipes | 5 | px + 2 | px ** 5 + px | p(print) |
X = 5X = X + 2X = X ** 5 + Xprint(X) |
| Tailored behavior for builtin mapand filter |
( range(5) | p(filter, px % 2 == 0) | p(map, px + 10) | p(list) | p(print)) |
X = range(5)X = filter((lambda x:x%2==0),X)X = map((lambda x: x + 10), X)X = list(X)print(X) |
| NumPy expressions | range(10) | np.sin(px)+1 | p(plt.plot) |
X = range(10)X = np.sin(X) + 1plt.plot(X) |
| Pandas support | people_df | px.loc[px.age > 10, 'name'] |
X = people_dfX.loc[X.age > 10, 'name'] |
| Assignment | people_df['name'] |= px.str.upper() |
X = people_df['name']X = X.str.upper()people_df['name'] = X |
| Pipe as variable | to_upper = px.strip().upper()to_underscore = px.replace(' ', '_')normalize = to_upper | to_underscore" ab cde " | normalize | p(print) |
_f1 = lambda x: x.strip().upper()_f2 = lambda x: x.replace(' ','_')_f3 = lambda x: _f2(_f1(x))X = " ab cde "X = _f3(X)print(X) |
| Builtin Data Structures |
2 | p({px-1: p([px, p((px+1, 4))])}) |
X = 2X = {X-1: [X, (X+1, 4)]} |
How it works
The expression p(func, *args, **kwargs) returns a Pipe object that overloads
__or__ and __ror__ operators. This object keeps func and args and kwargs until
evaluation of x | <Pipe>, when Pipe.__ror__ is called by python. Then it will evaluate
func(x, *args, **kwargs) and return the result.
The px object is simply p(lambda x: x).
Compatibility with JulienPalard/Pipe
This library is inspired by, and depends on, the intelligent and concise work of
JulienPalard/Pipe. If you want
a single pipe.py script or a lightweight library that implements core
functionality and logic of SSPipe, Pipe is perfect.
SSPipe is focused on facilitating usage of pipes, by integration with
popular libraries and introducing px concept and overriding python
operators to make pipe a first-class citizen.
Every existing pipe implemented by JulienPalard/Pipe
library is accessible through p.<original_name> and is compatible with SSPipe.
SSPipe does not implement any specific pipe function and delegates
implementation and naming of pipe functions to JulienPalard/Pipe.
For example, JulienPalard/Pipe's example for solving "Find the sum of all the even-valued terms in Fibonacci which do not exceed four million." can be re-written using sspipe:
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
from sspipe import p, px
euler2 = (fib() | p.where(lambda x: x % 2 == 0)
| p.take_while(lambda x: x < 4000000)
| p.add())
You can also pass px shorthands to JulienPalard/Pipe API:
euler2 = (fib() | p.where(px % 2 == 0)
| p.take_while(px < 4000000)
| p.add())
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sspipe-0.1.12.tar.gz.
File metadata
- Download URL: sspipe-0.1.12.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d09509681a8ac4e5173b3cb1051994636c19fd6f35a2b9fa7a7126928c2fc8e6
|
|
| MD5 |
d1a7327d23398f6c492102740b27f5da
|
|
| BLAKE2b-256 |
ecc3861d0f9a5c0d17b6c12fe02dc163723c60c143f7f1f2761ef7ec680aecbd
|
File details
Details for the file sspipe-0.1.12-py3-none-any.whl.
File metadata
- Download URL: sspipe-0.1.12-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ad1a6a71fc7b932d5ca975b5165c093f4497ac8e96d2dd93741f19e3b2d1c89e
|
|
| MD5 |
d005cacd488b7f84af6289537e86faa9
|
|
| BLAKE2b-256 |
934de5483a8e26755b0915418550fd3cd72d112ea99ce67e281f96efb62a4c5a
|