02_pipes

Mathematical operations

[2,3,4] | psum(start=1) == sum([2,3,4], start=1)
True
[(5,2), (3,1), (4,2), (6,3)] | pmin(key=lambda x: x[0]) 
(3, 1)
2 | padd(3) | psub(2) | pmul(4) | pdiv(2) | pmod(10)
6.0
(
    2 
        | padd(3)
        | psub(2)
        | pmul(4)
        | pdiv(2)
        | pmod(10)
)
6.0

Type conversions

[1,6,2,6,2,4,6,7] | pset
{1, 2, 4, 6, 7}

String operations

"hello world!" | pupper | prstrip("!") | psplit | pjoin("    ")
'HELLO    WORLD'

Function operations

def square(x):
    return x**2

5 | pbind(square) | pcall
25
(lambda: 10) | pcall
10
(lambda x, y: x + y) | ppartial(2) | ppartial(3) | pcall
5

Collection operations

_pflatten_helper

_pflatten_helper(lst, curr_level, levels)

Helper function to flatten a nested list.


[1, [2, [3, [4, 5]]]] | pflatten | plist
[1, 2, 3, 4, 5]
[1, 2, 3, [4, 5, 6, [7, 8, 9]]] | pflatten(levels=1) | plist
[1, 2, 3, 4, 5, 6, [7, 8, 9]]
["Joe", 32, "Mary", 28, "John", 45] | pbatched(2) | pdict
{'Joe': 32, 'Mary': 28, 'John': 45}

Misc operations

{"key1": "value1", "key2": "value2"} | pget("key1")
'value1'
class Foo:
    def __init__(self):
        self.x = 123
        
Foo() | pgetattr("x")
123

File/IO operations

pwrite

pwrite(content, file_path, mode)

pread

pread(file_path, mode)

pcopy

pcopy(content)

Copy content to clipboard.


"hello world" | pwrite | pread
'hello world'
"hello world" | pshow
hello world
'hello world'

Data wrangling operations

psave_pkl

psave_pkl(obj, file_path)

Write an object to a pickle file.


pload_pkl

pload_pkl(file_path)

Read a pickle file and return the object.


{'key': 'value'} | psave_pkl | pload_pkl
{'key': 'value'}

pto_df

pto_df(data, **kwargs)

Create a pandas DataFrame.


pcopy_df

pcopy_df(df)

Copy a DataFrame to clipboard in a format compatible with Google Sheets or Excel.

Usage:

df >> pcopy_df

papply_mask

papply_mask(mask, df)

Apply a mask to a DataFrame.

Usage:

mask >> papply_mask(df)

[
    {"name": "Alice", "age": 30},
    {"name": "Bob", "age": 25}
] | pto_df
name age
0 Alice 30
1 Bob 25
df = {
    'name' : ['Alice', 'Bob'],
    'age' : [30, 25]
} | pto_df
df
name age
0 Alice 30
1 Bob 25
(df['name'] == 'Alice') >> papply_mask(df)
name age
0 Alice 30

pto_json

pto_json(data, **kwargs)

Convert data to JSON string.


pfrom_json

pfrom_json(json_str, **kwargs)

{
    'name' : ['Alice', 'Bob'],
    'age' : [30, 25]
} | pto_json | pfrom_json
{'name': ['Alice', 'Bob'], 'age': [30, 25]}