Exemple #1
0
    def test_thens(arg):
        def repeat_word(word, times, upper=False):
            if upper:
                word = word.upper()

            return [word] * times

        f = P[::-1] >> Then(repeat_word, 3)
        g = P[::-1] >> Then(repeat_word, 3, upper=True)

        assert f("ward") == ["draw", "draw", "draw"]
        assert g("ward") == ["DRAW", "DRAW", "DRAW"]

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

        # since map and filter receive the iterable on their second argument, you have to use `Then2`
        f = Then2(filter, P % 2 == 0) >> Then2(
            map, P**2
        ) >> list  #lambda x: map(lambda z: z**2, filter(lambda z: z % 2 == 0, x))

        assert f([1, 2, 3, 4, 5]) == [4, 16]  #[2**2, 4**2] == [4, 16]

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

        f = P.filter(P % 2 == 0) >> P.map(
            P**2
        ) >> list  #lambda x: map(lambda z: z**2, filter(lambda z: z % 2 == 0, x))

        assert f([1, 2, 3, 4, 5]) == [4, 16]  #[2**2, 4**2] == [4, 16]
Exemple #2
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 #3
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 #4
0
    def test_random(self):

        assert 9 == P.Pipe(
            "Hola Cesar",
            P.Obj.split(" "),
            P.map(len)
            .sum()
        )
Exemple #5
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 #6
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 #7
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 #8
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 #9
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 #10
0
    def test_0(self):
        from datetime import datetime
        import time

        t0 = datetime.now()

        time.sleep(0.01)

        t1 = 2 >> P.Make(
            P + 1,
            P._0(datetime.now)
        )

        assert t1 > t0
Exemple #11
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 #12
0
    def test_list(self):

        assert [['4', '6'], [4, 6]] == P.Pipe(
            3,
            [
                P + 1
            ,
                P * 2
            ],
            [
                P._2(map, str)
            ,
                ()
            ]
        )
Exemple #13
0
    def test_others(self):
        f = Obj.split(' ') >> P.map(len) >> sum >> If(
            (P < 15).Not(), "Great! Got {0} letters!".format).Else(
                "Too short, need at-least 15 letters")

        assert f("short frase") == "Too short, need at-least 15 letters"
        assert f("some longer frase") == "Great! Got 15 letters!"
Exemple #14
0
    def test_write_tree(self):

        code = (P + 1, P * 2, [P * 100, P.On('c'), P - 3, 'c'])

        f, refs = dsl.Compile(code, {})

        assert [600, 6, 3, 6] == f(2)
Exemple #15
0
    def test_lshift(self):

        f = P * 2 << P + 1

        assert 6 == f(2)

        f = lambda x: x * 3
        g = lambda x: x + 2

        h = g << P.Make(f)

        assert 11 == h(3)

        h = P.Make(g) << f

        assert 11 == h(3)
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_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 #18
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 #19
0
    def test_compose(self):
        f = P.Make(
            P + 1,
            P * 2,
            P + 4
        )

        assert 10 == f(2)
Exemple #20
0
    def test_rrshift(self):
        builder = P.Make(
            P + 1,
            P * 2,
            P + 4
        )

        assert 10 == 2 >> builder
Exemple #21
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 #22
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 #23
0
    def test_rshift(self):

        f = P + 1 >> P * 2

        assert 6 == f(2)

        f = lambda x: x * 3
        g = lambda x: x + 2

        h = f >> P.Make(g)

        assert 11 == h(3)

        h = P.Make(f) >> g
        assert 11 == h(3)

        y = 1 >> P + 1 >> P * 2
        assert 4 == y

        y = P * 2 << P + 1 << 1
        assert 4 == y
Exemple #24
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 #25
0
    def test_compose_list_reduce(self):
        f = P.Make(
            P + 1,
            P * 2,
            P + 4,
            [
                P + 2
            ,
                P / 2
            ],
            sum
        )

        assert 17 == f(2)
Exemple #26
0
    def test_compose_list(self):
        f = P.Make(
            P + 1,
            P * 2, {'x'},
            P + 4,
            [
                P + 2
            ,
                P / 2
            ,
                'x'
            ]
        )

        assert [12, 5, 6] == f(2)
Exemple #27
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 #28
0
    def test_fluent(self):

        f = Dict(x=2 * P, y=P + 1).Tuple(Rec.x + Rec.y, Rec.y / Rec.x)

        assert f(1) == (4, 1)  # ( x + y, y / x) == ( 2 + 2, 2 / 2) == ( 4, 1 )

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

        f = Obj.split(' ') >> P.map(len) >> sum >> If(
            (P < 15).Not(), "Great! Got {0} letters!".format).Else(
                "Too short, need at-least 15 letters")

        assert f("short frase") == "Too short, need at-least 15 letters"
        assert f("some longer frase") == "Great! Got 15 letters!"

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

        f = (Obj.split(' ').map(len).sum().If(
            (P < 15).Not(), "Great! Got {0} letters!".format).Else(
                "Too short, need at-least 15 letters"))

        assert f("short frase") == "Too short, need at-least 15 letters"
        assert f("some longer frase") == "Great! Got 15 letters!"
Exemple #29
0
    def test_C_1(self):
        assert P._(add2)(4) == 6
        assert P._(add2)._(mul3)(4) == 18

        assert P.Make(add2)(4) == 6
        assert P.Make(add2, mul3)(4) == 18
Exemple #30
0
    def test_reference(self):
        add_ref = P.Ref('add_ref')

        assert 8 == 3 >> P.Make(P.add(2).On(add_ref).add(3))
        assert 5 == add_ref()