Example #1
0
    def test_group_by(self):
        out = Stream(range(10)).group_by(key_hasher=lambda x: x % 3)
        self.assertDictEqual(out, {
            0: [0, 3, 6, 9],
            1: [1, 4, 7],
            2: [2, 5, 8]
        })

        out = Stream(range(10)).group_by(key_hasher=lambda x: x % 3,
                                         value_mapper=lambda x: x**2)
        self.assertDictEqual(out, {
            0: [0, 9, 36, 81],
            1: [1, 16, 49],
            2: [4, 25, 64]
        })

        out = Stream([1, 2, 3, 4, 2,
                      4]).group_by(lambda x: x % 2,
                                   value_container_clazz=ListType)
        self.assertDictEqual(out, {1: [1, 3], 0: [2, 4, 2, 4]})

        out = Stream([1, 2, 3, 4, 2,
                      4]).group_by(lambda x: x % 2,
                                   value_container_clazz=SetType)
        self.assertDictEqual(out, {1: {1, 3}, 0: {2, 4}})
Example #2
0
    def test_zip(self):
        out = Stream([4, 1, 2, 3]).zip(range(9), range(2, 9)).as_seq()
        self.assertListEqual(out, [(4, 0, 2), (1, 1, 3), (2, 2, 4), (3, 3, 5)])

        out = Stream([4, 1, 2, 3]).zip(range(9), range(2, 9),
                                       after=False).as_seq()
        self.assertListEqual(out, [(0, 2, 4), (1, 3, 1), (2, 4, 2), (3, 5, 3)])
Example #3
0
    def test_cycle(self):
        out = Stream([4, 1, 2, 3, 9, 0, 5]).cycle(range(3)).as_seq()
        self.assertListEqual(out, [(4, 0), (1, 1), (2, 2), (3, 0), (9, 1),
                                   (0, 2), (5, 0)])

        out = Stream([4, 1, 2, 3, 9, 0, 5]).cycle(range(3),
                                                  after=False).as_seq()
        self.assertListEqual(out, [(0, 4), (1, 1), (2, 2), (0, 3), (1, 9),
                                   (2, 0), (0, 5)])
Example #4
0
    def test_mapping(self):
        l = [5, 2, 5, 3, 4]
        out = Stream(l).mapping(lambda x: x,
                                lambda x: x**2,
                                resolve=lambda x, y: x + y)
        self.assertDictEqual(out, {5: 50, 2: 4, 3: 9, 4: 16})

        out = Stream([1, 2, 3, 4, 5, 6]).mapping(lambda x: x % 2, lambda x: x,
                                                 lambda o, n: o + n)
        self.assertDictEqual(out, {1: 9, 0: 12})
Example #5
0
    def test_map2(self):
        rnd = self.rnd
        a, b, size = 1, 100, 1000

        add_5 = lambda x: x + 5
        pow_3 = lambda x: x**3

        mod_2_not_zero = lambda x: x % 2 != 0
        mod_3_not_2 = lambda x: x % 3 != 2
        mod_5_not_4 = lambda x: x % 5 != 4

        out = (Stream(rnd.int_range_supplier(
            a, b)).limit(size).filter(mod_3_not_2).map(add_5).filter(
                mod_2_not_zero).map(pow_3).filter(mod_5_not_4).as_seq())

        rnd.reset()

        out_target = []

        for e in rnd.int_range(a, b, size=size):
            if mod_3_not_2(e):
                e = add_5(e)

                if mod_2_not_zero(e):
                    e = pow_3(e)

                    if mod_5_not_4(e):
                        out_target.append(e)

        self.assertListEqual(out, out_target)
Example #6
0
    def _parallel_processor(self: 'ParallelStream[T]',
                            func,
                            timeout=None,
                            batch_size=None) -> Stream[T]:
        """
        processes data points concurrently. Elements are processed in batches of size "batch_size".

        :param self:
        :param func:
        :param timeout: time to wait for task to be done, if None then there is no
                        limit on execution time.
        :param batch_size: If it is None then number of worker is used.
        :return:
        """

        batch_size = batch_size or self._worker
        assert batch_size > 0, 'Batch size must be positive.'

        stream = (Stream(iter(self._pointer)).map(
            partial(self._submit_job,
                    func)).peek(self._registered_jobs.append).batch(
                        batch_size).map(as_completed).flat_map())

        if timeout is not None:
            result_extractor = partial(Future.result, timeout=timeout)
        else:
            result_extractor = Future.result

        return stream.map(result_extractor)
Example #7
0
    def test_accumulate3(self):
        data = [False, frozenset(), 0, 1, 5, 0.1, '',
                ['a'], {}, {'bc': 'd'}, (), 0.0]

        out = Stream(data).accumulate(lambda a, b: a or b).any()

        self.assertIs(out, any(data))
Example #8
0
    def test_1(self):
        class Number:
            def __init__(self, num):
                self._num = num

            @property
            def num(self):
                return self._num

            def __add__(self, other: 'Number') -> 'Number':
                self._num += other._num  # mutating first class object.

                return self

            def __str__(self):
                return str(self._num)

            def __eq__(self, other: 'Number') -> bool:
                return self._num == other._num

        data = tuple(Number(x) for x in range(10))

        out = Stream(data).filter(lambda x: x.num % 2 == 0).limit(3).sum(
            Number(0))

        out_target = sum(islice((e for e in data if e.num % 2 == 0), 3),
                         Number(0))

        self.assertEqual(out, out_target)
Example #9
0
    def test_accumulate4(self):
        size = 10

        data = range(size)

        out = Stream(data).accumulate(op.add).reduce(op.add).get()

        self.assertEqual(out, sum(sum(range(i)) for i in range(1, size + 1)))
Example #10
0
    def test_accumulate5(self):
        size = 10

        data = range(size)

        out = Stream(data).map(lambda x: x ** 2).accumulate(op.add).reduce(op.add).get()

        self.assertEqual(out, sum(sum(j ** 2 for j in range(i)) for i in range(1, size + 1)))
Example #11
0
    def test_map3(self):
        rnd = self.rnd
        a, b, size = 1, 100, 1000

        pow_2 = lambda x: x**2

        out = sum(Stream(rnd.int_range_supplier(a, b)).limit(size).map(pow_2))

        rnd.reset()

        out_target = sum(pow_2(e) for e in rnd.int_range(a, b, size=size))

        self.assertEqual(out, out_target)
Example #12
0
    def test_map5(self):
        rnd = self.rnd
        a, b, size = 1, 100, 50

        out = Stream(rnd.int_range_supplier(a, b)) \
            .limit(size) \
            .reduce(op.mul, 1) \
            .get()

        rnd.reset()

        out_target = reduce(op.mul, rnd.int_range(a, b, size=size))

        self.assertEqual(out, out_target)
Example #13
0
    def test_zip_longest(self):
        out = Stream([4, 1, 2, 3]).zip_longest(range(9),
                                               range(2, 9),
                                               fillvalue=-1).as_seq()
        self.assertListEqual(out, [(4, 0, 2), (1, 1, 3), (2, 2, 4), (3, 3, 5),
                                   (-1, 4, 6), (-1, 5, 7), (-1, 6, 8),
                                   (-1, 7, -1), (-1, 8, -1)])

        out = Stream([4, 1, 2, 3]).zip_longest(range(9),
                                               range(2, 9),
                                               fillvalue=-1).as_seq()

        self.assertListEqual(out, [(4, 0, 2), (1, 1, 3), (2, 2, 4), (3, 3, 5),
                                   (-1, 4, 6), (-1, 5, 7), (-1, 6, 8),
                                   (-1, 7, -1), (-1, 8, -1)])

        out = Stream([4, 1, 2, 3]).zip_longest(range(9),
                                               range(2, 9),
                                               after=False,
                                               fillvalue=None).as_seq()
        self.assertListEqual(out, [(0, 2, 4), (1, 3, 1), (2, 4, 2), (3, 5, 3),
                                   (4, 6, None), (5, 7, None), (6, 8, None),
                                   (7, None, None), (8, None, None)])
Example #14
0
    def test_map1(self):
        rnd = self.rnd
        a, b, size = 1, 100, 1000

        add_5 = lambda x: x + 5
        pow_3 = lambda x: x**3

        out = (Stream(rnd.int_range_supplier(
            a, b)).limit(size).map(add_5).map(pow_3).as_seq())

        rnd.reset()

        out_target = [pow_3(add_5(e)) for e in rnd.int_range(a, b, size=size)]

        self.assertListEqual(out, out_target)
Example #15
0
    def test_map8(self):
        rnd = self.rnd
        a, b, size = 1, 100, 50

        rng = range(3, 70)

        out = (Stream(rnd.int_range_supplier(
            a, b)).limit(size).zip(rng).map(sum).reduce(op.mul, 1).get())

        rnd.reset()

        data = (e + r for e, r in zip(rnd.int_range(a, b, size=size), rng))

        out_target = reduce(op.mul, data)

        self.assertEqual(out, out_target)
Example #16
0
    def test_map7(self):
        rnd = self.rnd
        a, b, size = 1, 100, 50

        cc = (ChainedCondition.if_else(lambda x: x < 50, lambda x: x**2,
                                       lambda x: x % 50))

        out = Stream(rnd.int_range_supplier(a, b)) \
            .limit(size) \
            .conditional(cc) \
            .reduce(op.mul, 1) \
            .get()

        rnd.reset()

        data = (cc(e) for e in rnd.int_range(a, b, size=size))

        out_target = reduce(op.mul, data)

        self.assertEqual(out, out_target)
Example #17
0
    def test_reduce(self):
        def _sum(a, b):
            return a + b

        self.assertEqual(
            Stream(range(3, 9)).reduce(_sum).get(), sum(range(3, 9)))
        self.assertTrue(Stream(range(0)).reduce(_sum) is EMPTY)

        self.assertEqual(Stream([]).reduce(_sum), EMPTY)
        self.assertEqual(Stream([1]).reduce(_sum), Optional(1))
        self.assertEqual(Stream([1, 2]).reduce(_sum), Optional(3))

        initial_point = 10

        self.assertEqual(
            Stream([]).reduce(_sum, initial_point=initial_point), Optional(10))
        self.assertEqual(
            Stream([1]).reduce(_sum, initial_point=initial_point),
            Optional(11))
        self.assertEqual(
            Stream([1, 2]).reduce(_sum, initial_point=initial_point),
            Optional(13))
Example #18
0
    def test_if_else(self):
        rnd = random()

        def get():
            return rnd.randint(1, 100)

        size = 1000

        out = (Stream.from_supplier(get).limit(size).if_else(
            lambda x: x < 50, lambda x: 0,
            lambda x: 1).mapping(identity,
                                 lambda x: 1,
                                 resolve=lambda x, y: x + y))

        out_target = defaultdict(int)

        rnd.reset()

        for _ in range(size):
            out_target[0 if get() < 50 else 1] += 1

        self.assertDictEqual(out, out_target)
Example #19
0
    def test_conditional1(self):
        rnd = random()

        def get():
            return rnd.randint(1, 100)

        size = 1000

        conditions = (ChainedCondition().if_then(
            lambda x: x <= 10,
            lambda x: 10).if_then(lambda x: x <= 20, lambda x: 20).if_then(
                lambda x: x <= 30, lambda x: 30).done())

        out = (Stream.from_supplier(get).limit(size).conditional(
            conditions).mapping(identity,
                                lambda x: 1,
                                resolve=lambda x, y: x + y))

        out_target = defaultdict(int)
        rnd.reset()

        for _ in range(size):
            e = get()
            k = None

            if e <= 10:
                k = 10
            elif e <= 20:
                k = 20
            elif e <= 30:
                k = 30
            else:
                k = e

            out_target[k] += 1

        self.assertDictEqual(out, out_target)
Example #20
0
 def test_1(self):
     out = Stream([1, 6, 2, 7, 3]).window_function(lambda l: sum(l) / 3,
                                                   3).as_seq()
     self.assertListEqual(out, [3, 5, 4])
Example #21
0
 def test_2(self):
     # Stream must be greater than 3
     Stream([1, 6]).window_function(lambda l: sum(l) / 3, 3).as_seq()
Example #22
0
    def test_accumulate1(self):
        data = range(10)
        out = Stream(data).accumulate(op.add).as_seq()

        out_target = list(accumulate(data, op.add))
        self.assertListEqual(out, out_target)
Example #23
0
 def test_batch(self):
     out = list(Stream(range(10)).batch(3))
     self.assertListEqual(out, [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, )])
Example #24
0
    def test_accumulate2(self):
        data = [True, 1, 5, 0.1, 'a', ['b'], {'c'}, {'de': 'f'}]

        out = Stream(data).accumulate(lambda x, y: x and y).all()

        self.assertIs(out, all(data))
Example #25
0
 def test_enumerate(self):
     out = Stream(range(4, 10)).enumerate().as_seq()
     self.assertListEqual(out, [(0, 4), (1, 5), (2, 6), (3, 7), (4, 8),
                                (5, 9)])