Example #1
0
def iterable_source(iterable, target):
    """Convert an iterable into a stream of events.

    Args:
        iterable: A series of items which will be sent to the target one by one.
        target: The target coroutine or sink.

    Returns:
        An iterator over any remaining items.
    """
    it = iter(iterable)
    for item in it:
        try:
            target.send(item)
        except StopIteration:
            return prepend(item, it)
    return empty_iter()
Example #2
0
def poisson_source(rate, iterable, target):
    """Send events at random times with uniform probability.

    Args:
        rate: The average number of events to send per second.
        iterable: A series of items which will be sent to the target one by one.
        target: The target coroutine or sink.

    Returns:
        An iterator over any remaining items.
    """
    if rate <= 0.0:
        raise ValueError("poisson_source rate {} is not positive".format(rate))

    it = iter(iterable)
    for item in it:
        duration = random.expovariate(rate)
        sleep(duration)
        try:
            target.send(item)
        except StopIteration:
            return prepend(item, it)
    return empty_iter()
Example #3
0
 def test_empty_iterable(self):
     result = list(prepend(42, []))
     self.assertListEqual(result, [42])
Example #4
0
 def test_non_empty_iterable(self):
     result = list(prepend(42, [1, 2, 3]))
     self.assertListEqual(result, [42, 1, 2, 3])
Example #5
0
 def test_non_empty_iterable(self):
     result = list(prepend(42, [1, 2, 3]))
     self.assertListEqual(result, [42, 1, 2, 3])
Example #6
0
 def test_empty_iterable(self):
     result = list(prepend(42, []))
     self.assertListEqual(result, [42])