5 Commits

Author SHA1 Message Date
Olli Graf
ea14cde427 Zwischen Commit 2024-11-09 10:21:36 +01:00
Olli Graf
fec0969d7f Verzeichnis für teil28 2024-10-25 14:03:27 +02:00
Olli Graf
af36c44e66 Code für Decorators 2024-10-25 14:01:47 +02:00
b913b2cb01 Dateien hochladen nach „decorator“
Decorator Sourcen
2024-10-18 05:01:01 +00:00
Olli Graf
37cbae2683 timer Decorator 2024-10-18 06:58:31 +02:00
15 changed files with 219 additions and 0 deletions

View File

@@ -32,4 +32,5 @@ CC-BY-SA Olli Graf
|25 | reguläre Ausdrücke|
|26 | lambda Funktionen|
|27 | logging.config|
|28 | Decorators|

View File

@@ -0,0 +1,9 @@
---
version: "2.1"
services:
fibserver:
image: hans:5000/fibserver:1
container_name: fibserver
ports:
- 8085:8085
restart: unless-stopped

1
gettattr/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
__pycache__

19
gettattr/Kreis_getattr.py Normal file
View File

@@ -0,0 +1,19 @@
from math import pi as pi
class Kreis_getattr:
def __init__(self, radius):
self.radius = radius
self.operators ={
'durchmesser': lambda x: self.radius * 2,
'umfang': lambda x: self.durchmesser * pi,
'flaeche': lambda x: self.radius**2 *pi
}
def __getattr__(self, name):
if name not in self.operators:
raise TypeError(f'unbekannte Operation {name}')
return self.operators[name](0)

1
teil28/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
__pycache__

25
teil28/classdecor.py Normal file
View File

@@ -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
teil28/counter.py Normal file
View File

@@ -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

16
teil28/fib.py Normal file
View File

@@ -0,0 +1,16 @@
#Datei: fib.py
import functools
import sys
from counter import counter
#@functools.cache
@counter
def fib(n):
if n in [0,1]:
return n
else:
return fib(n-1) + fib(n-2)

16
teil28/func_param.py Normal file
View File

@@ -0,0 +1,16 @@
# Datei: func_param.py
def add(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) # 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

12
teil28/nested_function.py Normal file
View File

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

20
teil28/pass_func.py Normal file
View File

@@ -0,0 +1,20 @@
def make_pretty(func):
# define the inner function
def inner():
# add some additional behavior to decorated function
print("Dies ist die innere Funktion.")
# call original function
func()
# return the inner function
return inner
# define ordinary function
def ordinary():
print("Dies ist die ordinary() Funktion.")
# decorate the ordinary function
decorated_func = make_pretty(ordinary)
# call the decorated function
decorated_func()

17
teil28/reverse.py Normal file
View File

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

21
teil28/simpledecorator.py Normal file
View File

@@ -0,0 +1,21 @@
def make_pretty(func):
# define the inner function
def inner():
# add some additional behavior to decorated function
print("I got decorated")
# call original function
func()
# return the inner function
return inner
# define ordinary function
@make_pretty
def ordinary():
print("I am ordinary")
# decorate the ordinary function
decorated_func = make_pretty(ordinary)
# call the decorated function
ordinary()

20
teil28/static.py Normal file
View File

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

28
teil28/timer.py Normal file
View File

@@ -0,0 +1,28 @@
#Datei: timer.py
from fib import fib
from counter import counter
import time
import sys
def timer(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args,**kwargs)
end_time = time.time()
print(f'Methode {func.__name__} - Laufzeit {end_time - start_time:.4f}s')
return result
return wrapper
@timer
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])))