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!"
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!"
def test3 (): class A: def __init__ (self, a): self.m_a = a def a (self): return self.m_a class B: pass x = fun.Function (object, object) @x.match (fun.any, fun.any) def do (i, j): return "%s, %s" % (i, j) @x.match (fun.matchclass (B), fun.any) def do (i, j): return "some B, %s" % j @x.match (fun.any, fun.val == 5) def do (i, j): return "%s, value 5" % i @x.match (fun.bind (X = fun.catch (lambda arg: arg.a ())), fun.any) def do (i, j): return "a=%s, %s" % (X, j) assert (x (1, 2) == "1, 2") assert (x (A (1), 2) == "a=1, 2") assert (x (B (), 2) == "some B, 2") assert (x (1, 5) == "1, value 5")
def test3(): class A: def __init__(self, a): self.m_a = a def a(self): return self.m_a class B: pass x = fun.Function(object, object) @x.match(fun.any, fun.any) def do(i, j): return "%s, %s" % (i, j) @x.match(fun.matchclass(B), fun.any) def do(i, j): return "some B, %s" % j @x.match(fun.any, fun.val == 5) def do(i, j): return "%s, value 5" % i @x.match(fun.bind(X=fun.catch(lambda arg: arg.a())), fun.any) def do(i, j): return "a=%s, %s" % (X, j) assert (x(1, 2) == "1, 2") assert (x(A(1), 2) == "a=1, 2") assert (x(B(), 2) == "some B, 2") assert (x(1, 5) == "1, value 5")