/usr/lib/python2.7/dist-packages/calabash/pipeline.py is in python-calabash 0.0.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | # -*- coding: utf-8 -*-
from functools import wraps
import itertools
class PipeLine(object):
"""
A coroutine wrapper which enables pipelining syntax.
:class:`PipeLine` allows you to flatten once-nested code just by wrapping
your generators. The class provides combinators in the form of operators,
allowing you to plug two generators together without having to nest lots of
function calls. For example::
>>> def summer(stdin):
... sum = 0
... for item in stdin:
... sum += item
... yield sum
>>> pipeline = PipeLine(lambda: iter([1, 2, 3, 4])) | PipeLine(summer)
>>> pipeline
<PipeLine: <lambda> | summer>
>>> for item in pipeline:
... print item
1
3
6
10
The yielded output of each generator in the chain becomes the input for the
next. The rules for writing a pipeline function are simple:
:class:`PipeLine` requires a callable which accepts a single argument (the
input), and returns an iterator. The only exception is the first part of
the pipeline, which should accept no arguments (as there will be no input).
To create pipeline functions, use the :func:`pipe` decorator::
>>> @pipe
... def my_generator():
... yield 1
... yield 2
... yield 3
>>> pl = my_generator()
>>> pl
<PipeLine: my_generator>
>>> for item in pl:
... print item
1
2
3
If your pipeline accepts input, an iterator will be provided as the first
argument to the function::
>>> @pipe
... def add_one(input):
... for item in input:
... yield item + 1
>>> pl = my_generator() | add_one()
>>> pl
<PipeLine: my_generator | add_one>
>>> for item in pl:
... print item
2
3
4
Even with input, your functions can still accept other parameters::
>>> @pipe
... def adder(input, amount):
... for item in input:
... yield item + amount
>>> pl = my_generator() | adder(3)
>>> pl
<PipeLine: my_generator | adder>
>>> for item in pl:
... print item
4
5
6
"""
__slots__ = ('coro_func',)
def __init__(self, coro_func):
self.coro_func = coro_func
@property
def __name__(self):
return self.coro_func.__name__
def __repr__(self):
return '<PipeLine: %s>' % getattr(self.coro_func, '__name__', repr(self.coro_func))
def __or__(self, target):
return target.__ror__(self)
def __ror__(self, source):
r"""
Connect two pipes so that one's output becomes the other's input.
A simple example::
>>> from itertools import imap
>>> p = (PipeLine(lambda: iter([1, 2, 3, 4])) |
... PipeLine(lambda stdin: imap(lambda x: x + 3, stdin)))
>>> p
<PipeLine: <lambda> | <lambda>>
>>> list(p)
[4, 5, 6, 7]
"""
def pipe():
return self.coro_func(iter(source))
pipe.__name__ = '%s | %s' % (
getattr(source, '__name__', repr(source)),
getattr(self.coro_func, '__name__', repr(self.coro_func)))
return PipeLine(pipe)
def __mul__(self, other):
"""
Yield the cross product between two alternative pipes.
A simple example::
>>> @pipe
... def echo(values):
... for x in values:
... yield x
>>> list(echo([0, 1]) * echo([9, 10]))
[(0, 9), (0, 10), (1, 9), (1, 10)]
"""
def product(stdin=None):
if stdin is None:
return itertools.product(self, other)
stdin1, stdin2 = itertools.tee(stdin, 2)
return itertools.product((stdin1 | self), (stdin2 | other))
product.__name__ = '%s * %s' % (
getattr(self.coro_func, '__name__', repr(self.coro_func)),
getattr(other, '__name__', repr(other)))
return pipe(product)()
def __add__(self, other):
"""
Yield the chained output of two alternative pipes.
Example::
>>> @pipe
... def echo(values):
... for x in values:
... yield x
>>> list(echo([1, 2, 3]) + echo([4, 5, 6]))
[1, 2, 3, 4, 5, 6]
"""
def concat(stdin=None):
if stdin is None:
return itertools.chain(self, other)
stdin1, stdin2 = itertools.tee(stdin, 2)
return itertools.chain((stdin1 | self), (stdin2 | other))
concat.__name__ = '%s + %s' % (
getattr(self.coro_func, '__name__', repr(self.coro_func)),
getattr(other, '__name__', repr(other)))
return pipe(concat)()
def __iter__(self):
return self.coro_func()
def pipe(func):
"""
Wrap a function as a pipeline.
>>> @pipe
... def printer(stdin, outfile=None):
... for item in stdin:
... print >>outfile, item
... yield item
>>> @pipe
... def echo(*values):
... for value in values:
... yield value
>>> p = printer()
>>> p
<PipeLine: printer>
>>> p = echo(1, 2, 3) | p
>>> p
<PipeLine: echo | printer>
>>> output = list(p)
1
2
3
>>> output
[1, 2, 3]
"""
@wraps(func)
def wrapper(*args, **kwargs):
@wraps(func)
def coro_func(stdin=None):
if stdin is None:
return func(*args, **kwargs)
return func(stdin, *args, **kwargs)
return PipeLine(coro_func)
return wrapper
|