File: memory.py

package info (click to toggle)
python-tubes 0.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 740 kB
  • sloc: python: 3,215; makefile: 149
file content (66 lines) | stat: -rw-r--r-- 1,606 bytes parent folder | download | duplicates (2)
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
# -*- test-case-name: tubes.test.test_memory -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Founts and drains that can produce values from static data in memory.
"""

from twisted.python.failure import Failure

from .tube import _NullFount, tube, series


@tube
class _IteratorTube(object):
    """
    An L{_IteratorTube} is an L{ITube} delivering the values from an iterable.
    """

    def __init__(self, iterable):
        """
        Create an L{_IteratorTube} from the given iterable.
        """
        self.iterable = iterable


    def started(self):
        """
        Deliver all the values in the iterable as a greeting.
        """
        for value in self.iterable:
            yield value



class _NotQuiteNull(_NullFount):
    """
    A L{_NotQuiteNull} is a fount that delivers a L{StopIteration} flowStopped
    after yielding its values.
    """

    def flowTo(self, drain):
        """
        Start the flow as usual and then immediately stop it with
        L{StopIteration}.

        @param drain: The drain to deliver to.

        @return: the next fount in the series.
        """
        result = super(_NotQuiteNull, self).flowTo(drain)
        drain.flowStopped(Failure(StopIteration()))
        return result



def iteratorFount(iterable):
    """
    Create and return an L{IFount} that delivers the values from the given
    iterator.

    @param iterable: an iterable of any values.

    @return: a fount which will deliver the given iterable to its drain.
    """
    return _NotQuiteNull().flowTo(series(_IteratorTube(iterable)))