Simple Smart Pipe Operator
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!
As an example, here is a single line code for reading students' data from 'data.csv', reporting those in the class 'A19' whose score is more than the average class score into 'report.csv':
from sspipe import p, px import pandas as pd pd.read_csv('data.csv') | px[px['class'] == 'A19'] | px[px.score > px.score.mean()].to_csv('report.csv')
As another example, here is a single line code for plotting sin(x) for points in range(0, 2*pi) where cos(x) is less than 0 in red color:
from sspipe import p, px import numpy as np import matplotlib.pyplot as plt np.linspace(0, 2*np.pi, 100) | px[np.cos(px) < 0] | p(plt.plot, px, np.sin(px), 'r') # The single-line code above is equivalent to the following code without SSPipe: # X = np.linspace(0, 2*np.pi, 100) # X = X[np.cos(X) < 0] # plt.plot(X, np.sin(X), 'r')
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).
|Description||Python expression using
||Equivalent python code|
with extra args
|Pipe as variable||
How it works
p(func, *args, **kwargs) returns a
Pipe object that overloads
__ror__ operators. This object keeps
x | <Pipe>, when
Pipe.__ror__ is called by python. Then it will evaluate
func(x, *args, **kwargs) and return the result.
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
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())
Release history Release notifications
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size sspipe-0.1.13-py3-none-any.whl (7.3 kB)||File type Wheel||Python version py3||Upload date||Hashes View hashes|
|Filename, size sspipe-0.1.13.tar.gz (7.2 kB)||File type Source||Python version None||Upload date||Hashes View hashes|