コード例 #1
0
ファイル: stream_test.py プロジェクト: ShivKJ/Stream
    def test_conditional2(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).otherwise(lambda x: -1))

        out = (Stream.from_supplier(get).limit(size).conditional(
            conditions).collect(GroupingBy(identity, Counting())))

        out_target = defaultdict(int)
        rnd.reset()

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

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

            out_target[k] += 1

        self.assertDictEqual(out, out_target)
コード例 #2
0
    def if_else(self, if_: Filter[X],
                then: Function[X, Y],
                else_: Function[X, Y] = identity) -> 'Stream[Y]':
        """
        if "if_" returns True then elements are transformed according to "then" otherwise else_
        function is used. This method is the special case of "conditional" method.
        "else_" has default value "identity" which return element as it is in case "if_" fails;

        Example:
            Stream(range(10)).condition(lambda x: 3 <= x <= 7, lambda x: 1 , lambda x: 0).collect(ToList())
            -> [0, 0, 0, 1, 1, 1, 1, 1, 0, 0]

        :param if_:
        :param then:
        :param else_: by default it returns element as it is.
        :return: Stream itself
        """

        return self.map(ChainedCondition.if_else(if_, then, else_))
コード例 #3
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)
コード例 #4
0
    def test_map6(self):
        rnd = self.rnd
        a, b, size = 1, 100, 50

        cc = (ChainedCondition().if_then(lambda x: x < 50,
                                         lambda x: x**2).done())

        out = Stream(rnd.int_range_supplier(a, b)) \
            .limit(size) \
            .conditional(cc) \
            .reduce(1, bi_func=op.mul) \
            .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)
コード例 #5
0
ファイル: stream_test.py プロジェクト: pyAddict/Basics
    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)
コード例 #6
0
 def __init__(self, methodName='runTest'):
     super().__init__(methodName=methodName)
     self.chained_condition = ChainedCondition()
コード例 #7
0
class TestingConditional(TestCase):
    def __init__(self, methodName='runTest'):
        super().__init__(methodName=methodName)
        self.chained_condition = ChainedCondition()

    def tearDown(self):
        self.chained_condition = None

    @expectedFailure
    def test_done1(self):
        self.chained_condition.done().if_then(lambda x: x < 5, identity)

    @expectedFailure
    def test_done2(self):
        self.chained_condition.done().otherwise(identity)

    def test_done3(self):
        self.chained_condition \
            .if_then(if_=lambda e: e < 5,
                     then=lambda e: e ** 2) \
            .done()

        out = [self.chained_condition.apply(e) for e in range(10)]
        out_target = [e**2 if e < 5 else e for e in range(10)]

        self.assertListEqual(out, out_target)

    def test_done4(self):
        self.chained_condition \
            .if_then(if_=lambda e: e < 5,
                     then=lambda e: e ** 2) \
            .if_then(if_=lambda e: e < 8,
                     then=lambda e: e + 2) \
            .done()

        out = (self.chained_condition.apply(e) for e in range(10))

        for o, e in zip(out, range(10)):
            if e < 5:
                e **= 2
            elif e < 8:
                e += 2

            with self.subTest(e=e):
                self.assertEqual(o, e)

    @expectedFailure
    def test_otherwise1(self):
        self.chained_condition.otherwise(identity).done()

    @expectedFailure
    def test_otherwise2(self):
        self.chained_condition.otherwise(identity).if_then(
            lambda x: x < 10, identity)

    def test_otherwise3(self):
        self.chained_condition \
            .if_then(if_=lambda e: e < 5,
                     then=lambda e: e ** 2) \
            .otherwise(lambda x: x + 1)

        out = [self.chained_condition.apply(e) for e in range(10)]
        out_target = [e**2 if e < 5 else e + 1 for e in range(10)]

        self.assertListEqual(out, out_target)

    def test_otherwise4(self):
        self.chained_condition \
            .if_then(if_=lambda e: e < 5,
                     then=lambda e: e ** 2) \
            .if_then(if_=lambda e: e < 8,
                     then=lambda e: e + 2) \
            .otherwise(lambda x: x % 3)

        out = (self.chained_condition.apply(e) for e in range(10))

        for o, e in zip(out, range(10)):
            if e < 5:
                e **= 2
            elif e < 8:
                e += 2
            else:
                e %= 3

            with self.subTest(e=e):
                self.assertEqual(o, e)