Ejemplo n.º 1
0
def test2 ():

    class C:
        def __init__ (self, name, value):
            self.name = name
            self.value = value

    test = fun.Function (object, name="test")

    @test.match (fun.bind (X = fun.if_else (fun.matchattr (name = "abc"),
                                            lambda arg: arg.value)))
    def do (arg):
        return "1:" + X

    @test.match (fun.bind (X = fun.if_else (fun.matchattr (name = "def"),
                                            lambda arg: arg.value)))
    def do (arg):
        return "2:" + X

    @test.match (fun.any)
    def do (arg):
        return "default"

    assert test (C ("test", "")) == "default"
    assert test (C ("abc", "yay!")) == "1:yay!"
    assert test (C ("def", "meh!")) == "2:meh!"
Ejemplo n.º 2
0
def test2():
    class C:
        def __init__(self, name, value):
            self.name = name
            self.value = value

    test = fun.Function(object, name="test")

    @test.match(
        fun.bind(X=fun.if_else(fun.matchattr(
            name="abc"), lambda arg: arg.value)))
    def do(arg):
        return "1:" + X

    @test.match(
        fun.bind(X=fun.if_else(fun.matchattr(
            name="def"), lambda arg: arg.value)))
    def do(arg):
        return "2:" + X

    @test.match(fun.any)
    def do(arg):
        return "default"

    assert test(C("test", "")) == "default"
    assert test(C("abc", "yay!")) == "1:yay!"
    assert test(C("def", "meh!")) == "2:meh!"
Ejemplo n.º 3
0
def test1():
    class B:
        def __init__(self, name):
            self.name = name

        def __str__(self):
            return self.name

    classify = fun.Function(object, name="classify")

    @classify.match(fun.catch(lambda a: a % 2 == 0))
    def classify_even(a):
        return "even"

    @classify.match(fun.catch(lambda a: a % 3 == 0))
    def classify_div3(a, **kwargs):
        return "divisible by 3"

    @classify.match(fun.val == 0, priority=1)
    def classify_zero(a):
        return "zero"

    @classify.match(lambda a: a > 5, priority=1)
    def classify_big(a):
        return "big"

    @classify.match(fun.any)
    def classify_unclassified(a):
        return "unclassified"

    @classify.match(fun.matchclass(B), priority=2)
    def classify_string(a):
        return "B"

    @classify.match(fun.matchattr(name="xxx"), priority=3)
    def classify_xxx(a):
        return "B(xxx)"

    assert classify(0) == "zero"
    assert classify(1) == "unclassified"
    assert classify(2) == "even"
    assert classify(3) == "divisible by 3"
    assert classify(4) == "even"
    assert classify(5) == "unclassified"
    assert classify(6) == "big"
    assert classify(7) == "big"
    assert classify(8) == "big"
    assert classify(9) == "big"
    assert classify(B("xx")) == "B"
    assert classify(B("xxx")) == "B(xxx)"
Ejemplo n.º 4
0
def test1 ():
    class B:
        def __init__ (self, name):
            self.name = name
        def __str__ (self):
            return self.name

    classify = fun.Function (object, name="classify")

    @classify.match (fun.catch (lambda a: a % 2 == 0))
    def classify_even (a):
        return "even"
    @classify.match (fun.catch (lambda a: a % 3 == 0))
    def classify_div3 (a, **kwargs):
        return "divisible by 3"


    @classify.match (fun.val == 0, priority=1)
    def classify_zero (a):
        return "zero"

    @classify.match (lambda a: a > 5, priority=1)
    def classify_big (a):
        return "big"

    @classify.match (fun.any)
    def classify_unclassified (a):
        return "unclassified"

    @classify.match (fun.matchclass (B), priority=2)
    def classify_string (a):
        return "B"

    @classify.match (fun.matchattr (name = "xxx"), priority=3)
    def classify_xxx (a):
        return "B(xxx)"

    assert classify (0) == "zero"
    assert classify (1) == "unclassified"
    assert classify (2) == "even"
    assert classify (3) == "divisible by 3"
    assert classify (4) == "even"
    assert classify (5) == "unclassified"
    assert classify (6) == "big"
    assert classify (7) == "big"
    assert classify (8) == "big"
    assert classify (9) == "big"
    assert classify (B ("xx")) == "B"
    assert classify (B ("xxx")) == "B(xxx)"