diff --git a/Donnerstag/__pycache__/decorators.cpython-39.pyc b/Donnerstag/__pycache__/decorators.cpython-39.pyc new file mode 100644 index 0000000..62d5ba3 Binary files /dev/null and b/Donnerstag/__pycache__/decorators.cpython-39.pyc differ diff --git a/Donnerstag/__pycache__/vectors.cpython-39.pyc b/Donnerstag/__pycache__/vectors.cpython-39.pyc index f9f0992..e734b34 100644 Binary files a/Donnerstag/__pycache__/vectors.cpython-39.pyc and b/Donnerstag/__pycache__/vectors.cpython-39.pyc differ diff --git a/Donnerstag/check_zip.py b/Donnerstag/check_zip.py new file mode 100644 index 0000000..233a440 --- /dev/null +++ b/Donnerstag/check_zip.py @@ -0,0 +1,7 @@ +it1 = range(5) +it2 = range(100,105) + +for a, b in zip(it1, it2): + print(a) + print('-' * 20) + print(b) \ No newline at end of file diff --git a/Donnerstag/decorators.py b/Donnerstag/decorators.py new file mode 100644 index 0000000..743c248 --- /dev/null +++ b/Donnerstag/decorators.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +def counter(func): + def counting(*args, **kwargs): + counting.callcount += 1 + return func(*args, **kwargs) + counting.callcount = 0 + return counting + +def get_counter(func): + try: + return func.callcount + except: + return None + +def reset_counter(func): + try: + func.callcount + func.callcount = 0 + except: + return None diff --git a/Donnerstag/fakultät.py b/Donnerstag/fakultät.py new file mode 100644 index 0000000..66149c0 --- /dev/null +++ b/Donnerstag/fakultät.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 +from decorators import counter, get_counter + + +@counter +def fak(n): + if type(n) is not int or n < 0: + raise TypeError("Illegal type") + if n == 0: + return 1 + return n * fak(n-1) + +print("6! =", fak(6)) +print("Anzahl Aufrufe:", get_counter(fak)) \ No newline at end of file diff --git a/Donnerstag/v_calc.py b/Donnerstag/v_calc.py index 355b932..6fda476 100644 --- a/Donnerstag/v_calc.py +++ b/Donnerstag/v_calc.py @@ -1,10 +1,14 @@ #!/usr/bin/env python3 -from vectors import Vector +from vectors import Vector, Vector2 -v1 = Vector(1, 2, 3, ) +v1 = Vector(1, 2, 3) v2 = Vector(4,0,1) -print(v1.get_values()) -del v2 -print("Jetzt wird v2 freigebe ") - -#print(v2) \ No newline at end of file +v3 = v1 * 3 +#v4 = v1 + v2 #.addition(v2) +#print(v3.get_values()) +print(str(v1)) +print(v3) +#print(v4.get_values()) +v5 = Vector2(1, 3) +print(v5 + 3) +print() \ No newline at end of file diff --git a/Donnerstag/vectors.py b/Donnerstag/vectors.py index a6712d4..0005b92 100644 --- a/Donnerstag/vectors.py +++ b/Donnerstag/vectors.py @@ -1,16 +1,31 @@ #!/usr/bin/env python3 """ Mathematische Vektorenals Klasse + +__magic_methods__ """ +class VectorError(Exception): + pass +class VectorTypeError(VectorError): + pass + + # Oberklassen werden automatisch vererbt class Vector(): - values = () + def __init__(self, *values): "Initialisierung 1 Funktion nach Erzeugen des Objekts" self.my_values = values - def __del__(self): + def __len__(self): #entspricht len länge in der class + return len(self.my_values) + + def __str__(self) -> str: #entspricht str string in der class + return type(self).__name__ + str(self.my_values) + + def __del__(self): "Destruktor: Letzte Funktion vor Freigabe des Speichers" print("Ein Objekt wird freigegeben ({})".format(self.my_values)) + def get_values(self): return self.my_values @@ -19,6 +34,24 @@ class Vector(): print("Aufruf") print(args) + def __mul__(self, factor): #entspricht multiplikation * in der class + multipi = [v * factor for v in self.my_values] + return type(self)(*multipi) ##Nicht vergessen * und classename + + def __add__(self, other): #entspricht addition + in der class + if not isinstance(self, Vector) or not isinstance(other, Vector): + raise VectorTypeError("Addition nur von Vektoren") + if len(self) !=len(other): + raise VectorTypeError("Vektoren ungleicher Laenge") + summenwert = [] + #zip iteriert ueber alle angegebenen Iterables parallel + for a, b in zip(self.get_values(), other.get_values()): + summenwert.append(a + b) + return type(self)(*summenwert) + + def info(self): + return type(self).__name__ + def poly(self, *args): if len(args) == 1: if type(args[0]) is str: @@ -29,5 +62,15 @@ class Vector(): self.poly_2param(*args) elif len(args) == 0: Vector.poly_0param(self, *args) - - \ No newline at end of file + + +class Vector2(Vector): + def __init__(self, val1, val2): + super().__init__(val1, val2) + + def __add__(self, other): + sum = [d + other for d in self.my_values] + # return super(self).__add__(Vector2.sum) + + def __mul__(self, factor): + return super().__mul__(factor) \ No newline at end of file