Beispiel #1
0
    def test_zero_items_returns_initial_empty_collection(self):
        collection = CollectingSink()

        transduce(Transducer,
                  sending(),
                  empty_iter(),
                  init=collection())

        self.assertEqual(len(collection), 0)
Beispiel #2
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()
Beispiel #3
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()
Beispiel #4
0
 def test_zero_items_returns_initial_empty_set(self):
     result = transduce(Transducer,
                        adding(),
                        empty_iter())
     self.assertEqual(result, set())
Beispiel #5
0
 def test_zero_items_returns_initial_empty_tuple(self):
     result = transduce(Transducer,
                        conjoining(),
                        empty_iter())
     self.assertEqual(result, tuple())
Beispiel #6
0
 def test_empty_iterator_returns_none(self):
     self.assertIsNone(iterator_or_none(empty_iter()))
Beispiel #7
0
 def test_is_empty(self):
     self.assertEqual(len(list(empty_iter())), 0)
Beispiel #8
0
 def test_empty_iterator_returns_none(self):
     self.assertIsNone(iterator_or_none(empty_iter()))
Beispiel #9
0
 def test_is_empty(self):
     self.assertEqual(len(list(empty_iter())), 0)