grapevine documentation¶
Unix commands replicas¶
cat
¶
construct a pipeline
Usage¶
<iterable> | cat
cat(<iterable>…)
Examples¶
>>> from grapevine import *
>>> [1, 2, 3] | cat | tuple
(1, 2, 3)
>>> cat([1, 2, 3]) | tuple
(1, 2, 3)
>>> cat([1, 2, 3], (4, 5, 6)) | tuple
(1, 2, 3, 4, 5, 6)
See also
cut
¶
yield a slice of every item
Usage¶
<iterable> | cut[<int>]
<iterable> | cut[<slice>]
Examples¶
>>> from grapevine import *
>>> ('foo', 'bar', 'quux') | cut[-2:] | tuple
('oo', 'ar', 'ux')
>>> zip(range(0, 3), range(3, 6)) | cut[1] | tuple
(3, 4, 5)
See also
grep
¶
yield items matching a pattern
Usage¶
<iterable> | grep(<test>…)
grep(<test>, <iterable>…)
test is:
- a regular expression;
- or a single argument function.
head
¶
yield the first items
Usage¶
<iterable> | head[<int>]
Examples¶
>>> from grapevine import *
>>> ('foo', 'bar', 'quux') | head(2) | tuple
('foo', 'bar')
>>> ('foo', 'bar', 'quux') | head(-2) | tuple
('foo',)
See also
nl
(a.k.a. nl1
)¶
number items of an iterable (start with 1)
Usage¶
<iterable> | nl1
<iterable> | nl
Examples¶
>>> from grapevine import *
>>> ('foo', 'bar', 'quux') | nl1 | list
[(1, 'foo'), (2, 'bar'), (3, 'quux')]
>>> nl is nl1
True
See also
seq
¶
yield a sequence of numbers
Usage¶
seq(<last>)
seq(<first>, <last>)
seq(<first>, <increment>, <last>)
Examples¶
>>> from grapevine import *
>>> seq(5) | tuple
(1, 2, 3, 4, 5)
>>> seq(3.5, 6) | tuple
(3.5, 4.5, 5.5)
>>> seq(-10, 40, 100) | tuple
(-10, 30, 70)
See also
sort
¶
sort items
Usage¶
<pipelined-iterator> | sort
<iterable> | sort(<keyword-argument>…)
sort(<iterable>…)
sort(<iterable>…, <keyword-argument>…)
sort
takes additional keyword arguments: cmp
, key
and reverse
.
See Python documentation for details.
Examples¶
>>> from grapevine import *
>>> sort((4, 2, 1, 3)) | tuple
(1, 2, 3, 4)
>>> cat(('foo', 'bar', 'qaax')) | sort(key = lambda x: x[1:3]) | tuple
('qaax', 'bar', 'foo')
>>> print '-'.join(('foo', 'bar', 'quux') | cat | sort)
bar-foo-quux
See also
split
¶
split an iterable into fixed-sized pieces
Usage¶
<iterable> | split(<int>)
Examples¶
>>> from grapevine import *
>>> range(7) | split(3) | list
[(0, 1, 2), (3, 4, 5), (6,)]
See also
tail
¶
yield the last items
Usage¶
<iterable> | tail(<int>)
Examples¶
>>> from grapevine import *
>>> ('foo', 'bar', 'quux') | tail(-1) | tuple
('quux',)
>>> ('foo', 'bar', 'quux') | tail(+3) | tuple
('quux',)
See also
uniq
¶
discard all but one of successive equal items
Usage¶
<iterable> | uniq
Examples¶
>>> from grapevine import *
>>> cat((1, 2, 3, 3, 2, 1)) | uniq | tuple
(1, 2, 3, 2, 1)
See also
wc
¶
return the number of items
Usage¶
<pipelined-iterable> | wc
wc(<iterable>)
Examples¶
>>> from grapevine import *
>>> wc(['foo', 'bar', 'quux'])
3
>>> cat(None for x in range(0, 7) for y in range(0, x) for z in range(y, x)) | wc
56
See also
Other useful tools¶
nl0
¶
return number items of an iterable (start with 0)
Usage¶
<iterable> | nl0
Examples¶
>>> from grapevine import *
>>> ('foo', 'bar', 'quux') | nl0 | list
[(0, 'foo'), (1, 'bar'), (2, 'quux')]
See also
select
¶
slice an iterable
Usage¶
<iterable> | select[<int>]
<iterable> | select[<slice>]
Examples¶
>>> from grapevine import *
>>> ('foo', 'bar', 'quux') | select[-2:] | tuple
('bar', 'quux')
>>> sum(range(100) | select[i] | tuple != (range(100)[i],) for i in range(-3, 4))
0
>>> sum(range(100) | select[i:j:k] | list != range(100)[i:j:k] for i in range(-3, 4) for j in range(-3, 4) for k in range(-3, 4) if k != 0)
0
Special tools¶
dev_null
¶
empty sequence
Usage¶
dev_null
Examples¶
>>> from grapevine import *
>>> dev_null | tuple
()
>>> for x in dev_null:
... raise Exception
...
slurp
¶
discard every item
Usage¶
<pipelined-iterable> | slurp
slurp(<iterable>)
Examples¶
>>> from grapevine import *
>>> def tmp(s): print s
...
>>> slurp(tmp(s) for s in ('foo', 'bar', 'quux'))
foo
bar
quux
>>> tmp = []
>>> cat(tmp.__iadd__([x]) for x in range(5)) | slurp
>>> tmp
[0, 1, 2, 3, 4]
>>> def tmp(): yield 0; raise RuntimeError()
...
>>> slurp(tmp())
Traceback (most recent call last):
...
RuntimeError
Customization¶
Generators with input¶
Usage¶
cat(<generator-with-input>…)
<pipelined-iterable> | <generator-with-input>
Use the special STDIN
to access your input.
Examples¶
>>> from grapevine import *
>>> cat((1, 2, 3)) | (-x for x in STDIN) | tuple
(-1, -2, -3)
>>> cat(x + 1 for x in (1, 2, 3)) | (x * x for x in STDIN) | tuple
(4, 9, 16)
>>> def tmp():
... n = 0
... for i in STDIN:
... n += i
... if n > 10:
... yield n
... n = 0
...
>>> range(10) | cat([4], tmp(), [2]) | tuple
(4, 15, 13, 17, 2)