grapevine Release 1.0
July 06, 2015
Contents
1 Unix commands replicas 1 1.1 cat ...... 1 1.2 cut ...... 1 1.3 echo ...... 2 1.4 grep ...... 2 1.5 head ...... 3 1.6 nl (a.k.a. nl1)...... 3 1.7 seq ...... 4 1.8 sort ...... 4 1.9 split ...... 5 1.10 tail ...... 5 1.11 uniq ...... 6 1.12 wc ...... 6 1.13 yes ...... 6
2 Other useful tools 9 2.1 nl0 ...... 9 2.2 select ...... 9 2.3 zuniq ...... 10
3 Special tools 11 3.1 dev_null ...... 11 3.2 slurp ...... 11 3.3 leak ...... 12
4 Customization 13 4.1 Generators with input...... 13 4.2 STDIN ...... 14
i ii CHAPTER 1
Unix commands replicas
1.1 cat construct a pipeline
1.1.1 Usage
•
1.1.2 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: • GNU coreutils: cat
1.2 cut yield a slice of every item
1.2.1 Usage
•
1 grapevine, Release 1.0
1.2.2 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: • GNU coreutils: cut
1.3 echo yield a single item
1.3.1 Usage
• echo(
1.3.2 Examples
>>> from grapevine import * >>> echo('quux')| list ['quux']
See also: • GNU coreutils: echo
1.4 grep yield items matching a pattern
1.4.1 Usage
•
1.4.2 Examples
2 Chapter 1. Unix commands replicas grapevine, Release 1.0
>>> from grapevine import * >>> grep('a[rz]',['foo','bar','baz'])| tuple ('bar', 'baz') >>> xrange(-10, 10)| grep( lambda x: x%7 ==1)| tuple (-6, 1, 8)
See also: • GNU grep
1.5 head yield the first items
1.5.1 Usage
•
1.5.2 Examples
>>> from grapevine import * >>> ('foo','bar','quux')| head(2)| tuple ('foo', 'bar') >>> ('foo','bar','quux')| head(-2)| tuple ('foo',)
See also: • tail • select • GNU coreutils: head
1.6 nl (a.k.a. nl1) number items of an iterable (start with 1)
1.6.1 Usage
•
1.6.2 Examples
>>> from grapevine import * >>> ('foo','bar','quux')| nl1| list [(1, 'foo'), (2, 'bar'), (3, 'quux')] >>> nl is nl1 True
1.5. head 3 grapevine, Release 1.0
See also: • nl0 • GNU coreutils: nl
1.7 seq
yield a sequence of numbers
1.7.1 Usage
• seq(
1.7.2 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: • GNU coreutils: seq
1.8 sort
sort items
1.8.1 Usage
•
1.8.2 Examples
4 Chapter 1. Unix commands replicas grapevine, Release 1.0
>>> 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: • GNU coreutils: sort
1.9 split split an iterable into fixed-sized pieces
1.9.1 Usage
•
1.9.2 Examples
>>> from grapevine import * >>> xrange(7)| split(3)| list [(0, 1, 2), (3, 4, 5), (6,)]
See also: • GNU coreutils: split
1.10 tail yield the last items
1.10.1 Usage
•
1.10.2 Examples
>>> from grapevine import * >>> ('foo','bar','quux')| tail(-1)| tuple ('quux',) >>> ('foo','bar','quux')| tail(+3)| tuple ('quux',)
See also: • head
1.9. split 5 grapevine, Release 1.0
• select • GNU coreutils: tail
1.11 uniq discard all but one of successive equal items
1.11.1 Usage
•
1.11.2 Examples
>>> from grapevine import * >>> cat((1,2,3,3,2,1))| uniq| tuple (1, 2, 3, 2, 1)
See also: • zuniq • GNU coreutils: uniq
1.12 wc return the number of items
1.12.1 Usage
•
1.12.2 Examples
>>> from grapevine import * >>> wc(['foo','bar','quux']) 3 >>> cat(None for x in xrange(0,7) for y in xrange(0, x) for z in xrange(y, x))| wc 56
See also: • GNU coreutils: wc
1.13 yes yield repeatedly the same object
6 Chapter 1. Unix commands replicas grapevine, Release 1.0
1.13.1 Usage
• yes(
1.13.2 Examples
>>> from grapevine import * >>> zip((1,2,3), yes(7)) [(1, 7), (2, 7), (3, 7)] >>> sum(yes(6)| head(7)) 42
See also: • GNU coreutils: yes
1.13. yes 7 grapevine, Release 1.0
8 Chapter 1. Unix commands replicas CHAPTER 2
Other useful tools
2.1 nl0 return number items of an iterable (start with 0)
2.1.1 Usage
•
2.1.2 Examples
>>> from grapevine import * >>> ('foo','bar','quux')| nl0| list [(0, 'foo'), (1, 'bar'), (2, 'quux')]
See also: • nl (a.k.a. nl1) • GNU coreutils: nl
2.2 select slice an iterable
2.2.1 Usage
•
2.2.2 Examples
9 grapevine, Release 1.0
>>> from grapevine import * >>> ('foo','bar','quux')| select[-2:]| tuple ('bar', 'quux') >>> sum(xrange(100)| select[i]| tuple !=(range(100)[i],) for i in xrange(-3,4)) 0 >>> sum(xrange(100)| select[i:j:k]| list != range(100)[i:j:k] for i in xrange(-3,4) for j in xrange(-3,4) for k in xrange(-3,4) if k !=0) 0
See also: • head • tail
2.3 zuniq omit duplicate items
2.3.1 Usage
•
2.3.2 Examples
>>> from grapevine import * >>> cat((1,2,3,3,2,1))| zuniq| tuple (1, 2, 3)
2.3.3 Caveats
Items need to be hashable. See also: • uniq
10 Chapter 2. Other useful tools CHAPTER 3
Special tools
3.1 dev_null empty sequence
3.1.1 Usage
• dev_null
3.1.2 Examples
>>> from grapevine import * >>> dev_null| tuple () >>> for x in dev_null: ... raise Exception ...
See also: • The Jargon File: /dev/null • Python Library Reference: os.devnull
3.2 slurp discard every item
3.2.1 Usage
•
11 grapevine, Release 1.0
3.2.2 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 xrange(5))| slurp >>> tmp [0, 1, 2, 3, 4]
>>> def tmp(): yield 0; raise RuntimeError() ... >>> slurp(tmp()) Traceback (most recent call last): ... RuntimeError
3.3 leak remove magic
3.3.1 Usage
•
3.3.2 Examples
>>> from grapevine import *
>>> leak(dev_null)
>>> cat([1,2,3])| leak
12 Chapter 3. Special tools CHAPTER 4
Customization
4.1 Generators with input
4.1.1 Usage
• cat(
4.1.2 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 ... >>> xrange(10)| cat([4], tmp(), [2])| tuple (4, 15, 13, 17, 2)
See also: • Python Tutorial: Generators • Python Tutorial: Generator Expressions
13 grapevine, Release 1.0
4.2 STDIN standard input for generators See also: • Generators with input
14 Chapter 4. Customization