diff --git a/decorator/classdecor.py b/decorator/classdecor.py new file mode 100644 index 0000000..8b9be04 --- /dev/null +++ b/decorator/classdecor.py @@ -0,0 +1,25 @@ +# Datei: classdecor.py + + +def addrepr(cls): +# Universelle __repr__ Methode + def __repr__(self): + return f"{cls.__name__}({self.__dict__})" + cls.__repr__ = __repr__ + + return cls + +@addrepr +class Fahrzeug(): + def __init__(self,farbe,typ): + self.typ = typ + self.farbe = farbe + + +f1 = Fahrzeug('grau','VW') +f2 = Fahrzeug('rot','Ferrari') + +print(f'{f1}') +print(f'{f2}') + + diff --git a/decorator/counter.py b/decorator/counter.py new file mode 100644 index 0000000..28bb820 --- /dev/null +++ b/decorator/counter.py @@ -0,0 +1,13 @@ +#Datei: counter.py + +def counter(func): + func.count = 0 + def wrapper(*args, **kwargs): + func.count = func.count +1 + print(f'{func.__name__} wurde {func.count}-mal aufgerufen.') + result = func(*args,**kwargs) + + return result + wrapper.count = 0 + return wrapper + diff --git a/decorator/fib.py b/decorator/fib.py index 6626a5e..0e69fa2 100644 --- a/decorator/fib.py +++ b/decorator/fib.py @@ -1,6 +1,11 @@ +#Datei: fib.py +import functools import sys +from counter import counter +#@functools.cache +@counter def fib(n): if n in [0,1]: return n @@ -8,5 +13,4 @@ def fib(n): return fib(n-1) + fib(n-2) -print(fib(int(sys.argv[1]))) diff --git a/decorator/func_param.py b/decorator/func_param.py index 76064b9..c6f4286 100644 --- a/decorator/func_param.py +++ b/decorator/func_param.py @@ -1,8 +1,16 @@ +# Datei: func_param.py + def add(x, y): - return x + y + return x + y + +def mul(x,y): + return x * y def calculate(func, x, y): return func(x, y) -result = calculate(add, 4, 6) -print(result) # prints 10 +result = calculate(add, 4, 6) # Aufruf von calculate mit add Funktion als Parameter +print(result) # Ausgabe ist 10 + +result = calculate(mul, 4, 6) # Aufruf von calculate mit add Funktion als Parameter +print(result) # Ausgabe ist 24 diff --git a/decorator/nested_function.py b/decorator/nested_function.py index afac62f..9a8507c 100644 --- a/decorator/nested_function.py +++ b/decorator/nested_function.py @@ -1,12 +1,12 @@ - +#Datei: nested_function.py def print_message(message): - "Umgebende Function" - def message_sender(): - "Eingebettete Function" + print('Umgebende Funktion') + def inner_function(): + print('Eingebettete Funktion') print(message) - message_sender() + inner_function() -print_message("Some random message") +print_message("Irgendein Text") diff --git a/decorator/pass_func.py b/decorator/pass_func.py index eca999c..bf9112c 100644 --- a/decorator/pass_func.py +++ b/decorator/pass_func.py @@ -2,7 +2,7 @@ def make_pretty(func): # define the inner function def inner(): # add some additional behavior to decorated function - print("I got decorated") + print("Dies ist die innere Funktion.") # call original function func() @@ -11,7 +11,7 @@ def make_pretty(func): # define ordinary function def ordinary(): - print("I am ordinary") + print("Dies ist die ordinary() Funktion.") # decorate the ordinary function decorated_func = make_pretty(ordinary) diff --git a/decorator/reverse.py b/decorator/reverse.py new file mode 100644 index 0000000..3bea71d --- /dev/null +++ b/decorator/reverse.py @@ -0,0 +1,17 @@ + + +def reverse_decorator(func): + + def wrapper(text): + make_reverse = "".join(reversed(text)) + return func(make_reverse) + + return wrapper + +@reverse_decorator +def format_message(text): + return f'Text: {text}' + +print(format_message('Hallo')) + + diff --git a/decorator/static.py b/decorator/static.py new file mode 100644 index 0000000..245fa60 --- /dev/null +++ b/decorator/static.py @@ -0,0 +1,20 @@ +# Datei static.py + +class Math(): + + @staticmethod + def add(x,y): + return x+y + + @staticmethod + def sub(x,y): + return x-y + + @staticmethod + def mul(x,y): + return x*y + + +print(f'Add: {Math.add(3,2)}') +print(f'Sub: {Math.sub(3,2)}') +print(f'Mul: {Math.mul(3,2)}') diff --git a/decorator/timer.py b/decorator/timer.py index a465e51..8ca1d4a 100644 --- a/decorator/timer.py +++ b/decorator/timer.py @@ -1,5 +1,9 @@ +#Datei: timer.py +from fib import fib +from counter import counter import time +import sys def timer(func): def wrapper(*args, **kwargs): @@ -16,6 +20,9 @@ def timer(func): def summe(n): return f"Summe: {sum(range(n))}" +@timer +def calc_fib(n): + return fib(n) print(summe(1000000)) - +print(calc_fib(int(sys.argv[1])))