Exemple #1
0
    def test_not(self):

        from phi import P

        assert True == P.Pipe(
            1,
            P + 1,  # 1 + 1 == 2
            P > 5,  # 2 > 5 == False
            P.Not()  # not False == True
        )

        ################################
        ################################

        from phi import P

        assert True == P.Pipe(
            1,
            (P + 1 >
             5).Not()  # not 1 + 1 > 5 == not 2 > 5 == not False == True
        )

        ############################
        #############################

        from phi import P

        f = (P + 1 > 5).Not()  #lambda x: not x + 1 > 5

        assert f(1) == True
Exemple #2
0
    def test_ref_integraton_with_dsl(self):

        y = P.Ref('y')


        assert 5 == P.Pipe(
            1,
            P + 4,
            P.On(y),
            P * 10,
            'y'
        )

        assert 5 == P.Pipe(
            1,
            P + 4,
            P.On(y),
            P * 10,
            'y'
        )

        assert 5 == P.Pipe(
            1,
            P + 4,
            P.On('y'),
            P * 10,
            'y'
        )
Exemple #3
0
    def test_builder_NPipe(self):

        from phi import P

        assert 1 == P.Pipe(
            Write(s=1),  # write s == 1, outer context
            lambda x: P.Pipe(
                x,
                Write(s=P + 1)  # write s == 2, inner context
            ),
            Read('s')  # read s == 1, outer context
        )
Exemple #4
0
    def test_register_1(self):

        #register
        assert 5 == P.Pipe(
            3,
            P.add(2)
        )

        #Register2
        assert 8 == P.Pipe(
            3,
            P.pow(2)
        )

        #RegisterMethod
        assert "identity" == P.get_function_name()
Exemple #5
0
    def test_if(self):

        from phi import P, Val

        assert "Between 2 and 10" == P.Pipe(
            5,
            P.If(P > 10, "Greater than 10").Elif(
                P < 2, "Less than 2").Else("Between 2 and 10"))
Exemple #6
0
    def test_random(self):

        assert 9 == P.Pipe(
            "Hola Cesar",
            P.Obj.split(" "),
            P.map(len)
            .sum()
        )
Exemple #7
0
    def test_example_1(self):

        text = "a bb ccc"
        avg_word_length = P.Pipe(
            text,
            Obj.split(" "),  #['a', 'bb', 'ccc']
            P.map(len),  #[1, 2, 3]
            P._(sum) / len  #6 / 3 == 2
        )

        assert 2 == avg_word_length

        text = "a bb ccc"
        avg_word_length = P.Pipe(
            text,
            Obj.split(" "),  #['a', 'bb', 'ccc']
            P.map(len),  #[1, 2, 3]
            M(sum) / len  #6 / 3 == 2
        )

        assert 2 == avg_word_length

        text = "a bb ccc"
        avg_word_length = P.Pipe(
            text,
            Obj.split(" "),  #['a', 'bb', 'ccc']
            P.map(len),  #[1, 2, 3]
            P.sum() / P.len()  #6 / 3 == 2
        )

        assert 2 == avg_word_length

        avg_word_length = P.Pipe(
            "1 22 333",
            Obj.split(' '),  # ['1', '22', '333']
            P.map(len),  # [1, 2, 3]
            [
                sum  # 1 + 2 + 3 == 6
                ,
                len  # len([1, 2, 3]) == 3
            ],
            P[0] / P[1]  # sum / len == 6 / 3 == 2
        )

        assert avg_word_length == 2
Exemple #8
0
    def test_contains(self):

        from phi import P

        assert False == P.Pipe(
            [1, 2, 3, 4],
            P.filter(P % 2 != 0)  #[1, 3], keeps odds
            .Contains(4)  #4 in [1, 3] == False
        )
Exemple #9
0
    def test_record_object(self):

        x = P.Pipe([1, 2, 3], dict(sum=sum, len=len))

        assert x.sum == 6
        assert x.len == 3

        assert x['sum'] == 6
        assert x['len'] == 3
Exemple #10
0
    def test_builder_MakeRefContext(self):

        from phi import P

        assert 2 == P.Pipe(
            Write(s=1),  #s = 1
            P.Seq(
                Write(s=P + 1),  #s = 2
            ),
            Read('s')  # s == 2
        )
Exemple #11
0
    def test_lambda_opt_lambda(self):

        assert 3 == P.Pipe(0, [P + 1, P + 2], P[0] + P[1])

        assert 3 == P.Run(dict(a=Val(1), b=Val(2)), Rec.a + Rec.b)

        assert 5 == P.Run(dict(a=Val(10), b=Val(2)), Rec.a / Rec.b)

        assert 6 == 1 >> (P + 1) * (P + 2)

        assert 6 == 10 >> (P * 3) / (P - 5)
Exemple #12
0
    def test_2(self):
        assert [2, 4] == [1, 2, 3] >> P.Make(
            P
            ._2(map, P + 1)
            ._2(filter, P % 2 == 0)
        )

        assert [2, 4] == P.Pipe(
            [1, 2, 3],
            P
            ._2(map, P + 1)
            ._2(filter, P % 2 == 0)
        )
Exemple #13
0
    def test_methods(self):
        assert 5 == P.Pipe(
            "hello world",
            Obj.split(" ")
            .filter(P.Contains("w").Not())
            .map(len),
            P[0]
        )

        assert not P.Pipe(
            [1,2,3],
            P.Contains(5)
        )

        class A(object):
            def something(self, x):
                return "y" * x


        assert "yyy" == P.Pipe(
            A(),
            P.Obj.something(3) #used something
        )
Exemple #14
0
    def test_context(self):
        y = P.Ref('y')

        length = P.Pipe(
            "phi/tests/test.txt",
            P.With( open,
                Context,
                Obj.read(), { y },
                len
            )
        )

        assert length == 11
        assert y() == "hello world"
Exemple #15
0
    def test_list(self):

        assert [['4', '6'], [4, 6]] == P.Pipe(
            3,
            [
                P + 1
            ,
                P * 2
            ],
            [
                P._2(map, str)
            ,
                ()
            ]
        )
Exemple #16
0
    def test_scope_property(self):

        assert "some random text" == P.Pipe(
            "some ",
            P.With( DummyContext("random "),
            (
                P + P.Context,
                P.With( DummyContext("text"),
                    P + P.Context
                )
            )
            )
        )

        with pytest.raises(Exception):
            P.Context() #Cannot use it outside of With
Exemple #17
0
    def test_ref_props(self):

        a = P.Ref('a')
        b = P.Ref('b')

        assert [7, 3, 5] == P.Pipe(
            1,
            add2, a.set,
            add2, b.set,
            add2,
            [
                (),
                a,
                b
            ]
        )
Exemple #18
0
    def test_ref(self):

        from phi import P, Obj, Ref

        assert {
            'a': 97,
            'b': 98,
            'c': 99
        } == P.Pipe(
            "a b c",
            Obj.split(' ')  #['a', 'b', 'c']
            .Write(keys=P)  # key = ['a', 'b', 'c']
            .map(ord),  # [ord('a'), ord('b'), ord('c')] == [97, 98, 99]
            lambda it: zip(Ref.keys, it),  # [('a', 97), ('b', 98), ('c', 99)]
            dict  # {'a': 97, 'b': 98, 'c': 99}
        )
Exemple #19
0
    def test_getting_started(self):
        from phi import P

        def add1(x):
            return x + 1

        def mul3(x):
            return x * 3

        x = P.Pipe(
            1.0,  #input 1
            add1,  #1 + 1 == 2
            mul3  #2 * 3 == 6
        )

        assert x == 6

        ################
        ################

        from phi import P

        x = P.Pipe(
            1.0,  #input 1
            P + 1,  #1 + 1 == 2
            P * 3  #2 * 3 == 6
        )

        assert x == 6

        ################
        ################

        from phi import P, List

        [x, y] = P.Pipe(
            1.0,  #input 1
            List(
                P + 1  #1 + 1 == 2
                ,
                P * 3  #1 * 3 == 3
            ))

        assert x == 2
        assert y == 3

        ################
        ################

        from phi import P, List

        [x, y] = P.Pipe(
            1.0,  #input 1
            P * 2,  #1 * 2 == 2
            List(
                P + 1  #2 + 1 == 3
                ,
                P * 3  #2 * 3 == 6
            ))

        assert x == 3
        assert y == 6

        ################
        ################

        from phi import P, Rec

        result = P.Pipe(
            1.0,  #input 1
            P * 2,  #1 * 2 == 2
            Dict(
                x=P + 1  #2 + 1 == 3
                ,
                y=P * 3  #2 * 3 == 6
            ))

        assert result.x == 3
        assert result.y == 6

        ################
        ################

        from phi import P, Rec

        result = P.Pipe(
            1.0,  #input 1
            P * 2,  #1 * 2 == 2
            Dict(
                x=P + 1  #2 + 1 == 3
                ,
                y=P * 3  #2 * 3 == 6
            ),
            Rec.x / Rec.y  #3 / 6 == 0.5
        )

        assert result == 0.5

        ################
        ################

        from phi import P, Rec, List, Write, Read

        [result, s] = P.Pipe(
            1.0,  #input 1
            Write(s=P * 2),  #s = 2 * 1 == 2
            Dict(
                x=P + 1  #2 + 1 == 3
                ,
                y=P * 3  #2 * 3 == 6
            ),
            List(
                Rec.x / Rec.y  #3 / 6 == 0.5
                ,
                Read('s')  #load 's' == 2
            ))

        assert result == 0.5
        assert s == 2

        ################
        ################

        from phi import P, Rec, Write, Read, List

        [result, s] = P.Pipe(
            1.0,  #input 1
            Write(s=P * 2),  #s = 2 * 1 == 2
            Dict(
                x=P + 1  #2 + 1 == 3
                ,
                y=P * 3  #2 * 3 == 6
            ),
            List(
                Rec.x / Rec.y  #3 / 6 == 0.5
                ,
                Read.s + 3  # 2 + 3 == 5
            ))

        assert result == 0.5
        assert s == 5

        ################
        ################

        from phi import P, Rec, Read, Write

        [result, s] = P.Pipe(
            1.0,  #input 1
            Write(s=P * 2),  #s = 2 * 1 == 2
            Dict(
                x=P + 1  #2 + 1 == 3
                ,
                y=P * 3  #2 * 3 == 6
            ),
            List(
                Rec.x / Rec.y  #3 / 6 == 0.5
                ,
                Read.s + 3  # 2 + 3 == 5
            ))

        assert result == 0.5
        assert s == 5

        ################
        ################

        from phi import P, Rec, Val

        [result, s, val] = P.Pipe(
            1.0,  #input 1
            Write(s=P * 2),  #s = 2 * 1 == 2
            Dict(
                x=P + 1  #2 + 1 == 3
                ,
                y=P * 3  #2 * 3 == 6
            ),
            List(
                Rec.x / Rec.y  #3 / 6 == 0.5
                ,
                Read.s + 3  # 2 + 3 == 5
                ,
                Val(9) + 1  #input 9 and add 1, gives 10
            ))

        assert result == 0.5
        assert s == 5
        assert val == 10

        #########################
        #########################

        from phi import P, Rec, Read, Write, Val, If

        [result, s, val] = P.Pipe(
            1.0,  #input 1
            Write(s=(P + 3) / (P + 1)),  #s = 4 / 2 == 2
            Dict(
                x=P + 1  #2 + 1 == 3
                ,
                y=P * 3  #2 * 3 == 6
            ),
            List(
                Rec.x / Rec.y  #3 / 6 == 0.5
                ,
                Read.s + 3  # 2 + 3 == 5
                ,
                If(
                    Rec.y > 7,
                    Val(9) + 1  #input 9 and add 1, gives 10
                ).Elif(Rec.y < 4, "Yes").Else("Sorry, come back latter.")))

        assert result == 0.5
        assert s == 5
        assert val == "Sorry, come back latter."

        ######################################
        #######################################

        from phi import P, Rec, Read, Write, Val, If

        f = P.Seq(
            Write(s=(P + 3) / (P + 1)),  #s = 4 / 2 == 2
            Dict(
                x=P + 1  #2 + 1 == 3
                ,
                y=P * 3  #2 * 3 == 6
            ),
            List(
                Rec.x / Rec.y  #3 / 6 == 0.5
                ,
                Read.s + 3  # 2 + 3 == 5
                ,
                If(
                    Rec.y > 7,
                    Val(9) + 1  #input 9 and add 1, gives 10
                ).Else("Sorry, come back latter.")))

        [result, s, val] = f(1.0)

        assert result == 0.5
        assert s == 5
        assert val == "Sorry, come back latter."
Exemple #20
0
    def test_fn(self):

        assert "hola" == P.Pipe("HOLA", P.Obj.lower())
Exemple #21
0
    def test_pipe(self):
        assert P.Pipe(4, add2, mul3) == 18

        assert [18, 14] == P.Pipe(
            4,
            [
            (
                add2,
                mul3
            )
            ,
            (
                mul3,
                add2
            )
            ]
        )

        assert [18, 18, 15, 16] == P.Pipe(
            4,
            [
                (
                    add2,
                    mul3
                )
            ,
                [
                    (
                        add2,
                        mul3
                    )
                ,
                    (
                        mul3,
                        add2,
                        [
                            P + 1,
                            P + 2
                        ]
                    )
                ]
            ],
            flatten=True
        )

        assert [18, [18, 14, get_list(None)]] == P.Pipe(
            4,
            [
            (
                add2,
                mul3
            )
            ,
                [
                (
                    add2,
                    mul3
                )
                ,
                (
                    mul3,
                    add2
                )
                ,
                    get_list
                ]
            ]
        )

        [a, [b, c]] = P.Pipe(
            4,
            [
            (
                add2,
                mul3
            )
            ,
                [
                (
                    add2,
                    mul3
                )
                ,
                (
                    mul3,
                    add2
                )
                ]
            ]
        )

        assert a == 18 and b == 18 and c == 14
Exemple #22
0
    def test_ops(self):

        assert 2 == P.Pipe(1, P + 1)