reference
Pipe
Put a value into a function with 1 argument.
Examples:
Map
Filter
Reduce
>>> import operator
>>> range(5) | Reduce(operator.add)
10
>>> range(5) | Reduce(operator.add, 5) # with initial value
15
MapKeys
MapValues
FilterKeys
>>> [(0, 2), (3, 0)] | FilterKeys() | Pipe(list)
[(3, 0)]
>>> [(0, 2), (3, 0)] | FilterKeys(lambda x: x % 2 == 0) | Pipe(list)
[(0, 2)]
FilterValues
>>> [(0, 2), (3, 0)] | FilterValues() | Pipe(list)
[(0, 2)]
>>> [(0, 2), (3, 0)] | FilterValues(lambda x: x % 2 == 0) | Pipe(list)
[(0, 2), (3, 0)]
FlatMap
>>> [0, 2, 3, 0, 4] | FlatMap(range) | Pipe(list)
[0, 1, 0, 1, 2, 0, 1, 2, 3]
>>> [2, 3, 4] | FlatMap(lambda x: [(x, x), (x, x)]) | Pipe(list)
[(2, 2), (2, 2), (3, 3), (3, 3), (4, 4), (4, 4)]
>>> def yield_even(it):
... for x in it:
... if x % 2 == 0:
... yield x
>>> [range(0, 5), range(100, 105)] | FlatMap(yield_even) | Pipe(list)
[0, 2, 4, 100, 102, 104]
FlatMapValues
>>> [("a", ["x", "y", "z"]), ("b", ["p", "r"])] | FlatMapValues(lambda x: x) | Pipe(list)
[('a', 'x'), ('a', 'y'), ('a', 'z'), ('b', 'p'), ('b', 'r')]
KeyBy
ValueBy
Append
>>> [(0,), (1,)] | Append(lambda x: str(x[0])) | Pipe(list)
[(0, '0'), (1, '1')]
>>> [(0, '0'), (1, '1')] | Append(lambda x: str(x[0] * 10)) | Pipe(list)
[(0, '0', '0'), (1, '1', '10')]
Keys
Values
Grep
>>> ['hello 42 bro', 'world', 'awesome 42'] | Grep('42') | Pipe(list)
['hello 42 bro', 'awesome 42']
# regex is supported (passed to re.search)
>>> ['foo1', 'foo2', '3foo', 'bar1'] | Grep('^foo.*') | Pipe(list)
['foo1', 'foo2']
GrepV
>>> ['hello 42 bro', 'world', 'awesome 42'] | GrepV('42') | Pipe(list)
['world']
>>> ['foo1', 'foo2', '3foo', 'bar1'] | GrepV('^foo.*') | Pipe(list)
['3foo', 'bar1']
Count
useful for objects that don't have __len__
method:
Slice
>>> range(5) | Slice(2) | Pipe(list)
[0, 1]
>>> range(5) | Slice(2, 4) | Pipe(list)
[2, 3]
>>> range(5) | Slice(2, None) | Pipe(list)
[2, 3, 4]
>>> range(5) | Slice(0, None, 2) | Pipe(list)
[0, 2, 4]
Take
Chunked
>>> range(5) | Chunked(2) | Pipe(list)
[(0, 1), (2, 3), (4,)]
>>> range(5) | Chunked(3) | Pipe(list)
[(0, 1, 2), (3, 4)]
GroupBy
>>> import operator
>>> [(0, 'a'), (0, 'b'), (1, 'c'), (2, 'd')] | GroupBy(operator.itemgetter(0)) | MapValues(list) | Pipe(list)
[(0, [(0, 'a'), (0, 'b')]), (1, [(1, 'c')]), (2, [(2, 'd')])]
>>> ['ab', 'cd', 'e', 'f', 'gh', 'ij'] | GroupBy(len) | MapValues(list) | Pipe(list)
[(2, ['ab', 'cd']), (1, ['e', 'f']), (2, ['gh', 'ij'])]
IterLines
>>> import tempfile
>>> f = tempfile.NamedTemporaryFile('w+')
>>> f.write('hello\nworld\n')
12
>>> f.seek(0)
0
>>> f.name | IterLines() | Pipe(list)
['hello\n', 'world\n']
PipeArgs
>>> (1, 2) | PipeArgs(operator.add)
3
>>> ('FF', 16) | PipeArgs(int)
255
>>> ([1, 2], 'A') | PipeArgs(dict.fromkeys)
{1: 'A', 2: 'A'}
>>> ({1, 2}, {3, 4, 5}) | PipeArgs(set.union)
{1, 2, 3, 4, 5}
StarMap
>>> [(2, 5), (3, 2), (10, 3)] | StarMap(pow) | Pipe(list)
[32, 9, 1000]
>>> [('00', 16), ('A5', 16), ('FF', 16)] | StarMap(int) | Pipe(list)
[0, 165, 255]
IsUnique
>>> [0, 1, 2, 3] | IsUnique()
True
>>> [0, 1, 1, 3] | IsUnique()
False
>>> '0123' | IsUnique(int)
True
>>> '0113' | IsUnique(int)
False
Sorted
>>> '3510' | Sorted()
['0', '1', '3', '5']
>>> '3510' | Sorted(reverse=True)
['5', '3', '1', '0']
>>> '!*&)#' | Sorted(key=ord)
['!', '#', '&', ')', '*']
>>> '!*&)#' | Sorted(key=ord, reverse=True)
['*', ')', '&', '#', '!']
Unique
>>> ['a', 'cd', 'cd', 'e', 'fgh'] | Unique() | Pipe(list)
['a', 'cd', 'e', 'fgh']
>>> ['a', 'cd', 'cd', 'e', 'fgh'] | Unique(len) | Pipe(list)
['a', 'cd', 'fgh']
Apply
>>> import random
>>> random.seed(42)
>>> range(5) | Pipe(list) | Apply(random.shuffle)
[3, 1, 2, 4, 0]
MapApply
>>> import random
>>> random.seed(42)
>>> range(3, 5) | Map(range) | Map(list) | MapApply(random.shuffle) | Pipe(list)
[[1, 0, 2], [3, 1, 2, 0]]
>>> def setitem(key, value):
... def inner(x):
... x[key] = value
... return inner
>>> [{'hello': 'world'}] | MapApply(setitem('foo', 'bar')) | Pipe(list)
[{'hello': 'world', 'foo': 'bar'}]