kuroko/test/testDecorators.krk

139 lines
3.6 KiB
Python
Raw Normal View History

2021-01-02 15:58:24 +03:00
def decorator(func):
def wrapper(butts):
print("I am a great decorated function that now needs an argument!", butts)
2021-01-02 15:58:24 +03:00
func()
print("And this is after the function call.")
print("Decorating!")
2021-01-02 15:58:24 +03:00
return wrapper
print("Decorator defined.")
2021-01-02 15:58:24 +03:00
@decorator
def foo():
print("Bar")
2021-01-02 15:58:24 +03:00
print("Function with decorator defined.")
2021-01-02 15:58:24 +03:00
foo("I am the argument")
print("Function with decorator called.")
2021-01-02 15:58:24 +03:00
def __main__():
def decorator(func):
def wrapper(butts):
print("I am a great decorated function (within a function!) that now needs an argument!", butts)
2021-01-02 15:58:24 +03:00
func()
print("And this is after the function call (in a function call!)")
print("Decorating!")
2021-01-02 15:58:24 +03:00
return wrapper
print("Decorator defined.")
2021-01-02 15:58:24 +03:00
let i = 42
let l = [1,2,3]
@decorator
def foo():
print("Bar (the inner one)")
2021-01-02 15:58:24 +03:00
print("Function with decorator defined.")
2021-01-02 15:58:24 +03:00
foo("I am in the inner wrapper.")
print("Function with decorator called.")
2021-01-02 15:58:24 +03:00
print("I like the numbers", l)
2021-01-02 15:58:24 +03:00
print("Calling main.")
2021-01-02 15:58:24 +03:00
__main__()
def methodDecorator(func):
def wrappedMethod(instance, butts):
print("I need an extra arg:", butts)
func(instance)
print("Done.")
return wrappedMethod
class Foo():
def __init__(self):
self.foo = "bar"
print("I am an initializer.")
def undecorated(self):
print("I am here.")
print("self.foo =", self.foo)
@methodDecorator
def decorated(self):
print("(decorated) self.foo = ", self.foo)
let f = Foo()
f.undecorated()
print("Returned from f.undecorated()")
f.decorated("butts")
print("Returned from f.decorated()")
def decoratorWithArgument(decoratorArg):
def theActualDecorator(func):
print("I am decorating",func.__qualname__,"with this argument:",decoratorArg)
def wrapped():
print("This wrapper captured this decorator arg:", decoratorArg)
func()
return wrapped
return theActualDecorator
@decoratorWithArgument("foo")
def decoratedWithArgument():
print("I don't take any args of mine own, and nor does my wrapper.")
decoratedWithArgument()
def methodDecoratorWithArguments(decoratorArgA, decoratorArgB):
def theActualDecorator(method):
print("I am decorating", method.__qualname__, "with these args:", decoratorArgA, decoratorArgB)
def wrapped(instance, wrapperArg):
print("I captured these from the decorator:", decoratorArgA, decoratorArgB)
print("I also take my own argument:", wrapperArg)
method(instance,"and modify this one")
return wrapped
return theActualDecorator
class Bar():
def __init__(self):
self.foo = "weee"
@methodDecoratorWithArguments("foo","bar")
def superDecoratedMethod(self, arg):
print("I am method, so I can access myself", self.foo, "and also my own argument which is now", arg)
let b = Bar()
b.superDecoratedMethod("this arg goes to the wrapper")
2021-01-03 11:59:50 +03:00
def genericDecorator(func):
def wrapper(*args,**kwargs):
print("I am a generic wrapper, I will pass all of my args along.")
2021-01-03 11:59:50 +03:00
func(*args,**kwargs)
print("And I am done.")
2021-01-03 11:59:50 +03:00
return wrapper
@genericDecorator
def aMethod(with_,args=None):
print("I got",with_,"and",args)
2021-01-03 11:59:50 +03:00
try:
aMethod()
except as exception:
print(exception.arg)
2021-01-03 11:59:50 +03:00
aMethod("just the first")
aMethod("the first","and the second")
aMethod(args="hello",with_="world")
2021-01-03 12:26:55 +03:00
try:
aMethod(foo="hello",with_="bar")
except as exception:
print(exception.arg) # unrecognized keyword arg, from inner method
2021-01-03 12:26:55 +03:00
aMethod(*[1,2])
try:
aMethod(*[1,2,3])
except as exception:
print(exception.arg) # too many arguments