Source

CoPipes / copipes / __init__.py

Full commit
  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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
from contextlib import contextmanager
from functools import update_wrapper
from os import linesep
from sys import version_info


__all__ = ['coroutine', 'pipeline', 'null']
__version__ = '0.1'
__author__ = 'Dmitry Vakhrushev <self@kr41.net>'
__license__ = 'BSD'


is2 = version_info[0] == 2


class _null(object):
    """
    A fake coroutine, which does nothing.

    The ``null`` is useful as pipeline end point or default value of next
    worker in coroutine definition:

    ..  code-block:: pycon

        >>> @coroutine
        ... def increment(next=null):
        ...     while True:
        ...         item = yield
        ...         next.send(item + 1)
        >>> inc = increment()
        >>> inc.send(1)         # No exception is raised

    Also it's converted to boolean as ``False``:

    ..  code-block:: pycon

        >>> bool(null)
        False
        >>> next = null or increment
        >>> next
        increment

    """

    def __call__(self, *args, **kw):
        """ Mimics to coroutine initialization """
        return self

    def __nonzero__(self):
        # Python 2.x boolean representation
        return False

    def __bool__(self):
        # Python 3.x boolean representation
        return False

    def __repr__(self):
        return 'null'

    def send(self, *args, **kw):
        """ Mimics to coroutine processing """
        pass

    def close(self):
        """ Mimics to coroutine termination """
        pass


null = _null()


class coroutine(object):
    """
    Decorator turns callable to coroutine.

    Examples::

    ..  code-block:: pycon

        >>> @coroutine
        ... def increment(next=null):
        ...     while True:
        ...         item = yield
        ...         next.send(item + 1)

        >>> @coroutine
        ... def collect(target, next=null):
        ...     while True:
        ...         item = yield
        ...         target.append(item)
        ...         next.send(item)

        >>> target = []
        >>> inc = increment(collect(target))    # Init coroutines

        >>> inc.send(1)
        >>> inc.send(2)
        >>> inc.send(3)

        >>> target
        [2, 3, 4]

    """

    def __init__(self, func):
        self.func = func
        self.args = ()
        self.kw = {}
        update_wrapper(self, func)

    def __call__(self, *args, **kw):
        """ Returns initialized coroutine """
        pargs = self.args + args
        kwargs = self.kw.copy()
        kwargs.update(kw)
        c = self.func(*pargs, **kwargs)
        method = getattr(c, 'next' if is2 else '__next__')
        method()
        return c

    def __repr__(self):
        params = [repr(a) for a in self.args]
        params.extend('{0!s}={1!r}'.format(k, v) for k, v in self.kw.items())
        params = ', '.join(params)
        return '{0}'.format(self.__name__) if not params else \
               '{0}.params({1})'.format(self.__name__, params)

    def params(self, *args, **kw):
        """
        Returns a parametrized copy of coroutine.

        Examples:

        ..  code-block:: pycon

            >>> @coroutine
            ... def increment(next=null):
            ...     while True:
            ...         item = yield
            ...         next.send(item + 1)

            >>> @coroutine
            ... def collect(target, next=null):
            ...     while True:
            ...         item = yield
            ...         target.append(item)
            ...         next.send(item)

            >>> target = []
            >>> collector = collect.params(target)
            >>> inc = increment(collector())    # Init coroutines

            >>> inc.send(1)
            >>> inc.send(2)
            >>> inc.send(3)

            >>> target
            [2, 3, 4]

        """
        p = self.__class__(self.func)
        p.args = args
        p.kw = kw
        return p


class pipeline(object):
    """
    Coroutine pipeline is utility class to connect number of coroutines into
    single pipeline.  The main goal is readability.  The following examples
    of code are equal:

    ..  code-block:: pycon

        >>> @coroutine
        ... def collect(target, next=null):
        ...     while True:
        ...         item = yield
        ...         target.append(item)
        ...         next.send(item)

        >>> @coroutine
        ... def increment(next=null):
        ...     while True:
        ...         item = yield
        ...         next.send(item + 1)

        >>> # Without pipelines
        >>> result = []
        >>> p = increment(collect(result))
        >>> for i in [1, 2, 3]:
        ...     p.send(i)
        >>> p.close()

        >>> # With pipelines
        >>> result = []
        >>> p = pipeline(
        ...     increment,
        ...     collect.params(result),
        ... )
        >>> p.feed([1, 2, 3])

    Pipeline also provides a readable representation, which is useful in debug:

    ..  code-block:: pycon
        >>> p
        increment
        collect.params([2, 3, 4])

    """

    def __init__(self, *workers):
        self.pipe = []
        self.connect(*workers)

    def __call__(self, next=null):
        """ Returns initialized coroutine pipeline """
        for worker in reversed(self.pipe):
            next = worker(next)
        return next

    def __repr__(self):
        return linesep.join(repr(worker) for worker in self.pipe) or \
               '<empty pipeline>'

    def connect(self, *workers):
        """
        Connect to pipeline passed coroutines.

        Examples:

        ..  code-block:: pycon

            >>> @coroutine
            ... def collect(target, next=null):
            ...     while True:
            ...         item = yield
            ...         target.append(item)
            ...         next.send(item)

            >>> @coroutine
            ... def increment(next=null):
            ...     while True:
            ...         item = yield
            ...         next.send(item + 1)

            >>> result = []
            >>> p = pipeline()
            >>> p.connect(increment, collect.params(result))
            >>> p.feed([1, 2, 3])
            >>> result
            [2, 3, 4]

        """
        self.pipe.extend(workers)

    def plug(self):
        """ Plug pipeline, i.e. connect ``null`` to pipeline """
        self.pipe.append(null)

    @contextmanager
    def fork(self, worker, *pipes):
        """
        Connect to pipeline forked coroutine.  The method is a context manager.
        The first argument is a coroutine.  If the second one is a number, then
        this number of pipelines will be created and passed to coroutine as
        positional arguments during initialization.  If the second argument and
        next ones are strings, then coroutine will be initialized using
        keyword arguments.

        Examples:

        ..  code-block:: pycon

            >>> @coroutine
            ... def collect(target, next=null):
            ...     while True:
            ...         item = yield
            ...         target.append(item)
            ...         next.send(item)

            >>> @coroutine
            ... def split(even=null, odd=null):
            ...     while True:
            ...         item = yield
            ...         next = odd if item % 2 else even
            ...         next.send(item)

            >>> evens = []
            >>> odds = []
            >>> p = pipeline()
            >>> with p.fork(split, 2) as (even, odd):
            ...     # ``p.fork(split, 'even', 'odd')`` is also correct
            ...     even.connect(collect.params(evens))
            ...     odd.connect(collect.params(odds))

            >>> p.feed([1, 2, 3, 4])
            >>> evens
            [2, 4]
            >>> odds
            [1, 3]

        Note, forked pipelines are joined at the end of fork.  This means that
        if you connect some coroutines after fork, it will be connected to each
        of forked pipeline.  To prevent this behavior call :meth:`plug` on the
        forked pipeline.

        ..  code-block:: pycon

            >>> @coroutine
            ... def broadcast(*next):
            ...     while True:
            ...         item = yield
            ...         for n in next:
            ...             n.send(item)

            >>> @coroutine
            ... def increment(next=null):
            ...     while True:
            ...         item = yield
            ...         next.send(item + 1)

            >>> @coroutine
            ... def decrement(next=null):
            ...     while True:
            ...         item = yield
            ...         next.send(item - 1)

            >>> incremented = []
            >>> decremented = []
            >>> original = []
            >>> result = []
            >>> p = pipeline()
            >>> with p.fork(broadcast, 3) as (inc, dec, orig):
            ...     inc.connect(increment, collect.params(incremented))
            ...     dec.connect(decrement, collect.params(decremented))
            ...     orig.connect(collect.params(original))
            ...     orig.plug()
            >>> p.connect(collect.params(result))

            >>> p.feed([1, 2, 3])
            >>> incremented
            [2, 3, 4]
            >>> decremented
            [0, 1, 2]
            >>> original
            [1, 2, 3]
            >>> result
            [2, 0, 3, 1, 4, 2]

        The representation of forked pipeline looks like:

        ..  code-block:: pycon

            >>> p
            broadcast:
                -->
                    increment
                    collect.params([2, 3, 4])
                -->
                    decrement
                    collect.params([0, 1, 2])
                -->
                    collect.params([1, 2, 3])
                    null
            collect.params([2, 0, 3, 1, 4, 2])

        """
        if isinstance(pipes[0], int):
            pipe_count = pipes[0]
            pipe_names = None
        else:
            pipe_count = len(pipes)
            pipe_names = pipes
        pipes = tuple(pipeline() for i in range(pipe_count))
        yield pipes
        if pipe_names:
            self.connect(_fork(worker, **dict(zip(pipe_names, pipes))))
        else:
            self.connect(_fork(worker, *pipes))

    def feed(self, source):
        """
        Feed pipeline using items from ``source``.

        The method initializes pipeline, feeds it, then closes it.

        """
        p = self()
        for item in source:
            p.send(item)
        p.close()


class _fork(object):
    """
    Forked coroutine pipeline is utility class.  You don't need to deal
    with it directly, use :meth:`pipeline.fork` method.

    """

    def __init__(self, worker, *pipes, **named_pipes):
        self.worker = worker
        self.pipes = pipes
        self.named_pipes = named_pipes

    def __call__(self, next):
        """ Returns initialized forked pipeline """
        pipes = (pipe(next) for pipe in self.pipes)
        named_pipes = dict((name, pipe(next)) for name, pipe
                                              in self.named_pipes.items())
        return self.worker(*pipes, **named_pipes)

    def __repr__(self):
        result = [repr(self.worker) + ':']
        for pipe in self.pipes:
            result.append('    -->')
            result.extend(' ' * 8 + wr for wr in repr(pipe).split(linesep))
        for name, pipe in self.named_pipes.items():
            result.append('    {0} -->'.format(name))
            result.extend(' ' * 8 + wr for wr in repr(pipe).split(linesep))
        return linesep.join(result)