Browse Source

Code für Decorators

master
Olli Graf 4 weeks ago
parent
commit
af36c44e66
  1. 25
      decorator/classdecor.py
  2. 13
      decorator/counter.py
  3. 6
      decorator/fib.py
  4. 12
      decorator/func_param.py
  5. 12
      decorator/nested_function.py
  6. 4
      decorator/pass_func.py
  7. 17
      decorator/reverse.py
  8. 20
      decorator/static.py
  9. 9
      decorator/timer.py

25
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}')

13
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

6
decorator/fib.py

@ -1,6 +1,11 @@
#Datei: fib.py
import functools
import sys import sys
from counter import counter
#@functools.cache
@counter
def fib(n): def fib(n):
if n in [0,1]: if n in [0,1]:
return n return n
@ -8,5 +13,4 @@ def fib(n):
return fib(n-1) + fib(n-2) return fib(n-1) + fib(n-2)
print(fib(int(sys.argv[1])))

12
decorator/func_param.py

@ -1,8 +1,16 @@
# Datei: func_param.py
def add(x, y): def add(x, y):
return x + y return x + y
def mul(x,y):
return x * y
def calculate(func, x, y): def calculate(func, x, y):
return func(x, y) return func(x, y)
result = calculate(add, 4, 6) result = calculate(add, 4, 6) # Aufruf von calculate mit add Funktion als Parameter
print(result) # prints 10 print(result) # Ausgabe ist 10
result = calculate(mul, 4, 6) # Aufruf von calculate mit add Funktion als Parameter
print(result) # Ausgabe ist 24

12
decorator/nested_function.py

@ -1,12 +1,12 @@
#Datei: nested_function.py
def print_message(message): def print_message(message):
"Umgebende Function" print('Umgebende Funktion')
def message_sender(): def inner_function():
"Eingebettete Function" print('Eingebettete Funktion')
print(message) print(message)
message_sender() inner_function()
print_message("Some random message") print_message("Irgendein Text")

4
decorator/pass_func.py

@ -2,7 +2,7 @@ def make_pretty(func):
# define the inner function # define the inner function
def inner(): def inner():
# add some additional behavior to decorated function # add some additional behavior to decorated function
print("I got decorated") print("Dies ist die innere Funktion.")
# call original function # call original function
func() func()
@ -11,7 +11,7 @@ def make_pretty(func):
# define ordinary function # define ordinary function
def ordinary(): def ordinary():
print("I am ordinary") print("Dies ist die ordinary() Funktion.")
# decorate the ordinary function # decorate the ordinary function
decorated_func = make_pretty(ordinary) decorated_func = make_pretty(ordinary)

17
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'))

20
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)}')

9
decorator/timer.py

@ -1,5 +1,9 @@
#Datei: timer.py
from fib import fib
from counter import counter
import time import time
import sys
def timer(func): def timer(func):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
@ -16,6 +20,9 @@ def timer(func):
def summe(n): def summe(n):
return f"Summe: {sum(range(n))}" return f"Summe: {sum(range(n))}"
@timer
def calc_fib(n):
return fib(n)
print(summe(1000000)) print(summe(1000000))
print(calc_fib(int(sys.argv[1])))

Loading…
Cancel
Save