diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..01c523a3e539c27e69355790b6cc9275dbb402d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ + +.idea/ +*.pyc diff --git a/Lektion2/Buses.py b/Lektion2/Buses.py new file mode 100644 index 0000000000000000000000000000000000000000..ba3f2446fee881f7496149466408abe3351e494a --- /dev/null +++ b/Lektion2/Buses.py @@ -0,0 +1,64 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +# Da die Klassen recht klein sind, ist es übersichtlicher sie in einer Datei zusammenzufassen. +# Die einfache Version der Bus Klasse. +class Bus: + typ = "undefined" + driver = "undefined" + number_of_seats = 0 + + def __init__(self, typ, number_of_seats, driver): + self.typ = typ + self.number_of_seats = number_of_seats + self.driver = driver + + # Ausgabe der Attribute + def printData(self): + print("\t\tName: {}\t Bustyp: {}\t Anzahl der Sitze: {}".format(self.driver, self.typ, self.number_of_seats)) + + +# Klasse mit public Attributen +class BusTypA: + typ = "17b" + number_of_seats = 41 + driver = "undefined" + + def __init__(self, driver): + self.driver = driver + + def printData(self): + print("\t\tName: {}\t Bustyp: {}\t Anzahl der Sitze: {}".format(self.driver, self.typ, self.number_of_seats)) + + +# Klasse mit einem Konstruktor der einen optionalen Parameter enthält +class BusTypB: + typ = "17b" + number_of_seats = 41 + driver = "undefined" + + def __init__(self, typ, driver="Fahrer1"): + self.driver = driver + self.typ = typ + + def printData(self): + print("\t\tName: {}\t Bustyp: {}\t Anzahl der Sitze: {}".format(self.driver, self.typ, self.number_of_seats)) + diff --git a/Lektion2/Dog.py b/Lektion2/Dog.py new file mode 100644 index 0000000000000000000000000000000000000000..ea875b8707dd7f5a4b3dcfb9ea5d3b5c286f6a40 --- /dev/null +++ b/Lektion2/Dog.py @@ -0,0 +1,32 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +# Klasse mit einem privaten Attribut +class Dog: + name = "undefined" + + # Underscores werden verwendet um ein Attribut als privat zu markieren + def __init__(self, name): + self.name = name + self.__private_variable = "Bello ist versteckt." + + def printData(self): + print("\t\tWurde {} gefunden? {}".format(self.name, self.__private_variable)) + diff --git a/Lektion2/RunLecture2.py b/Lektion2/RunLecture2.py new file mode 100644 index 0000000000000000000000000000000000000000..3c7bb700a8d121ff94befa3111a9f33006a57efe --- /dev/null +++ b/Lektion2/RunLecture2.py @@ -0,0 +1,91 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +# Import Statements um die Inhalte der anderen Dateien hier zugänglich zu machen. +import sys +import os +sys.path.append(os.path.abspath(os.path.dirname(__file__))) +from Buses import Bus, BusTypA, BusTypB +from Dog import Dog + + +# Diese Methode wird in main.py ausgeführt und soll die Ergebnisse in einer leserlichen Form ausgeben. +def run_lecture_two(): + print("\n*** Lecture 2 Start ***") + print(" --Ausgabe der Bus Beispiele-- \n") + + text_wrapper("Erstes Beispiel", first_bus_example) + text_wrapper("Zweites Beispiel", second_bus_example) + text_wrapper("Drittes Beispiel", third_bus_example) + + print(" --Ausgabe des Dog Beispiels-- \n") + text_wrapper("Dog Beispiel", dog_example) + print("*** Lecture 2 End ***") + + +# Um eine gleichmässige Ausgabe zu gewährleisten und Code zu sparen haben wir hier eine kleine Hilfsfunktion +def text_wrapper(title, example): + print(f"\t - {title} -") + example() + print("\n") + + +# Das erste Bus-Beispiel das mit der Klasse Bus arbeitet +def first_bus_example(): + bus1 = Bus("3a", 38, "John A.") + bus2 = Bus("17b", 42, "Klaus M.") + + bus1.printData() + bus2.printData() + + +# Das zweite Bus-Beispiel, bei dem wir sehen, dass wir Attribute verändern können +def second_bus_example(): + bus1 = BusTypA("John A.") + bus2 = BusTypA("Klaus M.") + + bus1.typ = "3a" + + bus1.printData() + bus2.printData() + + +# Das dritte Bus-Beispiel das die Verwendung von optionalen Parametern zeigt +def third_bus_example(): + bus1 = BusTypB("3a") + bus2 = BusTypB("2a", "Klaus M.") + + bus1.printData() + bus2.printData() + + +# Dieses Beispiel zeigt das von außen auch auf private Attribute zugegriffen werden kann +def dog_example(): + bello = Dog("Bello") + + bello.printData() + bello.__private_variable = "\tAuf der Suche nach Bello" + + bello.printData() + + bello._Dog__private_variable = "Bello wurde gefunden" + bello.printData() + diff --git a/Lektion2/__init__.py b/Lektion2/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..d44fdb1fb30c457e842c551bcb89ace6f4fb66f6 --- /dev/null +++ b/Lektion2/__init__.py @@ -0,0 +1 @@ +# Dieses File wird verwendet, um ein Paket in Python zu erstellen. diff --git a/Lektion3/BasicDog.py b/Lektion3/BasicDog.py new file mode 100644 index 0000000000000000000000000000000000000000..0cceef92c81526f8425754b4bf897df8611d718e --- /dev/null +++ b/Lektion3/BasicDog.py @@ -0,0 +1,40 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +# The basic dog version with simple getters and setters +class BasicDog: + __name = "" + __age = 0 + + def __init__(self, name, age): + self.__name = name + self.__age = age + + def get_name(self): + return self.__name + + def get_age(self): + return self.__age + + def set_age(self, newAge): + self.__age = newAge + + diff --git a/Lektion3/Calculator.py b/Lektion3/Calculator.py new file mode 100644 index 0000000000000000000000000000000000000000..58d86f783c595d80248aae686f8e3a46cea51726 --- /dev/null +++ b/Lektion3/Calculator.py @@ -0,0 +1,31 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +# Die Calculator Klasse soll die Verwendung von statischen Methoden zeigen +class Calculator: + + # @ signalisiert die Verwendung eines Decorators. Statische Methoden verwenden keine objektbezogenen Informationen + @staticmethod + def addNumbers(integerlist): + value = 0 + for number in integerlist: + value += number + return value diff --git a/Lektion3/DecoratedDog.py b/Lektion3/DecoratedDog.py new file mode 100644 index 0000000000000000000000000000000000000000..6771417936666c79690e0ab5199378fbb15f1983 --- /dev/null +++ b/Lektion3/DecoratedDog.py @@ -0,0 +1,47 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +# Diese Klasse enthält alle Methodenversionen. +class DecoratedDog: + name = "undefined" + species = "Canis" + + def __init__(self, name): + self.name = name + + # Beispiel einer Instanz Methode + def bark_in_language(self, language): + match language: + case "french": + print("\t\t{}: wau, wau".format(self.name)) + case "englisch": + print("\t\t{}: woof, woof".format(self.name)) + case _: + print("\t\t{}: barf, barf".format(self.name)) + + # @ signalisiert die Verwendung eines Decorators. + @classmethod + def print_species(cls): + print(("\t\t{}'s spezies ist: " + cls.species).format(cls.name)) + + @staticmethod + def bark(): + print("\t\tWoof, woof") diff --git a/Lektion3/DecoratorExample.py b/Lektion3/DecoratorExample.py new file mode 100644 index 0000000000000000000000000000000000000000..7f90b241dfaf1869e5d3763e4f8f886483498726 --- /dev/null +++ b/Lektion3/DecoratorExample.py @@ -0,0 +1,48 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +# Das ist unser selbst erstellter Decorator. Eine Funktion wird als Parameter übergeben +def our_decorator(function_we_want_to_modify): + # In ihm wird eine weitere Funktion definiert die, die übergebene Funktion umschließt + def wrapper(): + print("\t\tDie Funktion wird um diese Ausgabe erweitert: 1/2") + function_we_want_to_modify() + print("\t\tDie Funktion wird um diese Ausgabe erweitert: 2/2 \n") + return wrapper + + +# wir können mit @ einer Funktion einen Decorator zuweisen. +@our_decorator +def decorated_function(): + print("\t\tMeine Dekoration wird über das @-Zeichen bestimmt.") + + +# Oder wir definieren wie sonst auch unsere Funktion und rufen die Decorator Funktion auf. +def unsuspecting_function(): + print("\t\tMeine Dekoration wird direkt zugewiesen.") + + +# Das Resultat ist dasselbe +def decorator_example(): + function_handle = our_decorator(unsuspecting_function) + function_handle() + decorated_function() + diff --git a/Lektion3/RunLecture3.py b/Lektion3/RunLecture3.py new file mode 100644 index 0000000000000000000000000000000000000000..928823d0aea30f55634289c7e93739c99340b8a0 --- /dev/null +++ b/Lektion3/RunLecture3.py @@ -0,0 +1,85 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +# Import Statements um die Inhalte der anderen Dateien hier zugänglich zu machen. +import sys +import os +sys.path.append(os.path.abspath(os.path.dirname(__file__))) +from BasicDog import BasicDog +from DecoratedDog import DecoratedDog +from Calculator import Calculator +from DecoratorExample import decorator_example + + +# Diese Methode wird in main.py ausgeführt und soll die Ergebnisse in einer leserlichen Form ausgeben. +def run_lecture_three(): + print("\n*** Lecture 3 Start ***") + print(" --Verwendung der verschiedenen Methodenarten-- \n") + + text_wrapper("Einfacher Zugriff auf ein Objektattribut", basic_attribute_output_example) + text_wrapper("Aufruf einer Instanz Methode", instance_method_example) + text_wrapper("Verwendung einer Klassenmethode", class_method_example) + text_wrapper("Verwendung einer statischen Methode", static_method_example) + + print(" --Ausgabe des Decorator Beispiels-- \n") + text_wrapper("Decorator Beispiel", decorator_example) + print("*** Lecture 3 End ***") + + +# Um eine gleichmässige Ausgabe zu gewährleisten und Code zu sparen haben wir hier eine kleine Hilfsfunktion +def text_wrapper(title, example): + print(f"\t - {title} -") + example() + print("\n") + + +# Das erste Dog Beispiel +def basic_attribute_output_example(): + dog1 = BasicDog("Zeus", 3) + print("\t\t Output: {}".format(dog1.get_name())) + + +# Beispiel einer einfachen Instanz Methode +def instance_method_example(): + dog1 = DecoratedDog("Bello") + dog1.bark_in_language("french") + DecoratedDog.bark() + + +# Beispiel einer Klassenmethode +def class_method_example(): + dog1 = DecoratedDog("Bello") + + DecoratedDog.print_species() # Print über Klasse + dog1.print_species() # Print über Referenz + + dog1.species = "Katze" + print("\t\t{}".format(dog1.species)) + dog1.print_species() + + +# Beispiel einer statischen Methode +def static_method_example(): + calc = Calculator() + print("\t\t{}".format(Calculator.addNumbers({2, 4, 6}))) + print("\t\t{}".format(calc.addNumbers({2, 4, 6}))) + + diff --git a/Lektion3/__init__.py b/Lektion3/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..d44fdb1fb30c457e842c551bcb89ace6f4fb66f6 --- /dev/null +++ b/Lektion3/__init__.py @@ -0,0 +1 @@ +# Dieses File wird verwendet, um ein Paket in Python zu erstellen. diff --git a/Lektion4/Animal.py b/Lektion4/Animal.py new file mode 100644 index 0000000000000000000000000000000000000000..00e59e5cd0239a2f8fff257755dfbd44dc02d11e --- /dev/null +++ b/Lektion4/Animal.py @@ -0,0 +1,42 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +# Von dieser Klasse werden die anderen Tiere erben. +class Animal: + __name = "" + __age = 0 + + def __init__(self, name, age): + self.__name = name + self.__age = age + + def get_name(self): + return self.__name + + def get_age(self): + return self.__age + + def eat(self): + print("\t\t{} is eating.".format(type(self).__name__)) + + def say_hello(self): + print("\t\tAnimal noises") + diff --git a/Lektion4/Beaver.py b/Lektion4/Beaver.py new file mode 100644 index 0000000000000000000000000000000000000000..8747e31fe56f62d0392b959db812e319588e4642 --- /dev/null +++ b/Lektion4/Beaver.py @@ -0,0 +1,33 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from Animal import Animal + + +class Beaver(Animal): + def __init__(self, name, age): + super().__init__(name, age) + + def knock(self): + print("\t\tKnock, knock.") + + def say_hello(self): + print("\t\tgrunt, grunt") + diff --git a/Lektion4/Duck.py b/Lektion4/Duck.py new file mode 100644 index 0000000000000000000000000000000000000000..49e3bc53af4940933e33b7e3aae877f85d722787 --- /dev/null +++ b/Lektion4/Duck.py @@ -0,0 +1,33 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from Animal import Animal + + +class Duck(Animal): + def __init__(self, name, age): + super().__init__(name, age) + + def swim(self): + print("\t\tThis animal can swim") + + def say_hello(self): + print("\t\tquack quack") + diff --git a/Lektion4/InheritanceDog.py b/Lektion4/InheritanceDog.py new file mode 100644 index 0000000000000000000000000000000000000000..f88e8d43e43a7598c219172fb68affa155a20b7c --- /dev/null +++ b/Lektion4/InheritanceDog.py @@ -0,0 +1,32 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from Animal import Animal + + +class InheritanceDog(Animal): + def __init__(self, name, age): + super().__init__(name, age) + + def say_hello(self): + print("\t\twoof woof") + + + diff --git a/Lektion4/Platypus.py b/Lektion4/Platypus.py new file mode 100644 index 0000000000000000000000000000000000000000..c8cb4228445f55bbe466a3526bcdba00e56d7496 --- /dev/null +++ b/Lektion4/Platypus.py @@ -0,0 +1,34 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +from Duck import Duck +from Beaver import Beaver + + +# Mehrfachvererbung um die Methoden von Duck und Beaver zu erhalten +class Platypus(Duck, Beaver): + def __init__(self, name, age): + super().__init__(name, age) + + def say_hello(self): + # Wir müssen die Klasse übergeben, von der wir nicht verwenden wollen + return super(Duck, self).say_hello() + diff --git a/Lektion4/RunLecture4.py b/Lektion4/RunLecture4.py new file mode 100644 index 0000000000000000000000000000000000000000..149e761f03a007d2f85b849f4aaa7d57aa7724d8 --- /dev/null +++ b/Lektion4/RunLecture4.py @@ -0,0 +1,68 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +# Import Statements um die Inhalte der anderen Dateien hier zugänglich zu machen. +import sys +import os +sys.path.append(os.path.abspath(os.path.dirname(__file__))) +from Duck import Duck +from InheritanceDog import InheritanceDog +from Beaver import Beaver +from Platypus import Platypus + + +# Diese Methode wird in main.py ausgeführt und soll die Ergebnisse in einer leserlichen Form ausgeben. +def run_lecture_four(): + print("\n*** Lecture 4 Start ***") + print(" --Verwendung von einfacher Vererbung-- \n") + text_wrapper("Ausgabe des Vererbungs-Beispiels", basic_inheritance_example) + + print(" --Verwendung von mehrfach Vererbung-- \n") + text_wrapper("Ausgabe des Mehrfachvererbungs-Beispiels", multi_inheritance_example) + + print("*** Lecture 4 End ***") + + +# Um eine gleichmässige Ausgabe zu gewährleisten und Code zu sparen haben wir hier eine kleine Hilfsfunktion +def text_wrapper(title, example): + print(f"\t - {title} -") + example() + print("\n") + + +# Das erste Vererbungsbeispiel. Wir sehen, dass die Methoden aus der Animal Klasse verwendet werden können. +def basic_inheritance_example(): + hund1 = InheritanceDog("Zeus", 2) + hund1.say_hello() + ente1 = Duck("Friedolin", 4) + ente1.eat() + ente1.say_hello() + bieber = Beaver("Günther", 3) + bieber.say_hello() + + +# Unser Schnabeltier kann Methoden aus der Duck und Beaver Klasse verwenden. +def multi_inheritance_example(): + schnabel = Platypus("Hercules", 2) + schnabel.swim() + schnabel.knock() + schnabel.say_hello() + diff --git a/Lektion4/__init__.py b/Lektion4/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..d44fdb1fb30c457e842c551bcb89ace6f4fb66f6 --- /dev/null +++ b/Lektion4/__init__.py @@ -0,0 +1 @@ +# Dieses File wird verwendet, um ein Paket in Python zu erstellen. diff --git a/README.md b/README.md index 27b94c72e681819945faea549ede5816d0c243b2..f9b99bf3c97fbdc74762d0242d3520cc17a2aa71 100644 --- a/README.md +++ b/README.md @@ -1,92 +1,11 @@ -# Programmieren lernen mit Python - OOP +# Programmieren lernen mit Python - Objektorientierte Programmierung +Dieses Projekt enthält die Beispiele aus dem iMooX.at Kurs +"Programmieren Lernen mit Python: Objektorientierte Programmierung". +Zu jeder Lektion finden Sie ein Paket das den Lektionscode und Helfermethoden enthält. +## Ausführen -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://gitlab.tugraz.at/llt_public/learninglab_coop/programmieren-lernen-mit-python/python-oop.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://gitlab.tugraz.at/llt_public/learninglab_coop/programmieren-lernen-mit-python/python-oop/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +Um die Inhalte auszugeben, führen Sie das main.py Script aus. Nutzen Sie dafür entweder +die Eingabeaufforderung -> dir/to/file> python3 main.py +oder eine IDE ihrer Wahl. diff --git a/main.py b/main.py new file mode 100644 index 0000000000000000000000000000000000000000..4d5fffd7e3ec03e3d68cf43e777b7e472b34dcbd --- /dev/null +++ b/main.py @@ -0,0 +1,32 @@ +# Copyright (c) 2023 Daniel Dohr, Josef Wachtler +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + +# Hier werden die Packages importiert die unsere Beispiele beinhalten +from Lektion2.RunLecture2 import run_lecture_two +from Lektion3.RunLecture3 import run_lecture_three +from Lektion4.RunLecture4 import run_lecture_four + +# Wenn wir unser Script starten wird der Code von allen 3 Lektionen ausgeführt und ausgegeben. +# Wenn nur der Code von einer Lektion ausgegeben werden soll, die anderen einfach auskommentieren. +if __name__ == '__main__': + run_lecture_two() + run_lecture_three() + run_lecture_four()