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)

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)

echo

yield a single item

Usage

  • echo(<object>)

Examples

>>> from grapevine import *
>>> echo('quux') | list
['quux']

grep

yield items matching a pattern

Usage

  • <iterable> | grep(<test>…)
  • grep(<test>, <iterable>…)

test is:

  • a regular expression;
  • or a single argument function.

Examples

>>> from grapevine import *
>>> grep('a[rz]', ['foo', 'bar', 'baz']) | tuple
('bar', 'baz')
>>> range(-10, 10) | grep(lambda x: x % 7 == 1) | tuple
(-6, 1, 8)

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

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)

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

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,)]

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',)

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)

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

yes

yield repeatedly the same object

Usage

  • yes(<object>)

Examples

>>> from grapevine import *
>>> zip((1, 2, 3), yes(7))
[(1, 7), (2, 7), (3, 7)]
>>> sum(yes(6) | head(7))
42

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')]

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

See also

zuniq

omit duplicate items

Usage

  • <iterable> | zuniq

Examples

>>> from grapevine import *
>>> cat((1, 2, 3, 3, 2, 1)) | zuniq | tuple
(1, 2, 3)

Caveats

Items need to be hashable.

See also

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

leak

remove magic

Usage

  • <pipelined-iterable> | leak
  • leak(<pipelined-iterable>)

Examples

>>> from grapevine import *
>>> leak(dev_null)  
<...iterator object at 0x...>
>>> cat([1, 2, 3]) | leak  
<...iterator object at 0x...>

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)

STDIN

standard input for generators