From 82915d6ee31fd763e7cd2391a25c225060cf1120 Mon Sep 17 00:00:00 2001 From: Hong-Phuc Bui <hong-phuc.bui@htwsaar.de> Date: Mon, 01 Jul 2024 15:34:08 +0200 Subject: [PATCH] Vererbung --- study-effort/vorlesung.csv | 4 turtle-geo-inheritance/src/turtlegeo/TtGeo.py | 18 ++++ study-effort/src/mystudy/main.py | 36 +++++--- study-effort/tests/mystudy/lecture_test.py | 2 turtle-geo-inheritance/tests/__init__.py | 0 vererbung-demo/demo.py | 26 ++++++ study-effort/dem-vorlesung.html | 28 +++++++ study-effort/src/mystudy/lecture.py | 41 +++++++++- turtle-geo-inheritance/src/turtlegeo/main.py | 12 +++ oop/vererbung/vererbung.py | 46 +++++++++++ turtle-geo-inheritance/pyproject.toml | 15 +++ study-effort/src/mystudy/DataFormatError.py | 4 + study-effort/pyproject.toml | 2 turtle-geo-inheritance/src/turtlegeo/__init__.py | 0 14 files changed, 211 insertions(+), 23 deletions(-) diff --git a/oop/vererbung/vererbung.py b/oop/vererbung/vererbung.py new file mode 100644 index 0000000..ce74cc7 --- /dev/null +++ b/oop/vererbung/vererbung.py @@ -0,0 +1,46 @@ +class Base: + def __init__(self, a, b): + self.a = a + self.b = b + + def sum(self): + return self.a + self.b + + def diff(self): + return self.a - self.b + + +class Derivate(Base): + def product(self): + return self.a * self.b + +class Derivate2(Base): + def __init__(self, a, b , c): + super().__init__(a, b) + self.c = c + + def quote(self): + return self.a / self.c + + +class Common: + def __init__(self, a, b): + self.a, self.b = a,b + def sum(self): + return self.a + self.b + +def makeMath(x:Base): + s = x.sum() + print(s) + +t = Common(8,9) +makeMath(t) + +x:Base = Base(1, 3) +makeMath(x) + +y:Derivate = Derivate(3, 4) +makeMath(y) + +z:Derivate2 = Derivate2(1, 2, 3) +makeMath(y) diff --git a/study-effort/dem-vorlesung.html b/study-effort/dem-vorlesung.html new file mode 100644 index 0000000..e6454e9 --- /dev/null +++ b/study-effort/dem-vorlesung.html @@ -0,0 +1,28 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Vorlesung</title> +</head> +<body> + +<table> + <tr> + <td>Mathematik</td> + <td>2.5</td> + <td>5</td> + </tr> + <tr> + <td>Prog 2</td> + <td>2.0</td> + <td>5</td> + </tr> + <tr> + <td>Informatik 2</td> + <td>2.0</td> + <td>5</td> + </tr> +</table> + +</body> +</html> \ No newline at end of file diff --git a/study-effort/pyproject.toml b/study-effort/pyproject.toml index 042a0be..b1f922e 100644 --- a/study-effort/pyproject.toml +++ b/study-effort/pyproject.toml @@ -11,5 +11,5 @@ where = ["src"] [project.scripts] -mystudy = "mystudy.main:main" +mystudy = "mystudy.main:start" diff --git a/study-effort/src/mystudy/DataFormatError.py b/study-effort/src/mystudy/DataFormatError.py new file mode 100644 index 0000000..259a4b3 --- /dev/null +++ b/study-effort/src/mystudy/DataFormatError.py @@ -0,0 +1,4 @@ +class DataFormatError(Exception): + def __init__(self): + super().__init__() + pass \ No newline at end of file diff --git a/study-effort/src/mystudy/lecture.py b/study-effort/src/mystudy/lecture.py index b6861de..64de7ca 100644 --- a/study-effort/src/mystudy/lecture.py +++ b/study-effort/src/mystudy/lecture.py @@ -1,5 +1,7 @@ from typing import Final +from mystudy.DataFormatError import DataFormatError + lecture_time: Final[int] = 15 ects_effort: Final[int] = 30 @@ -27,8 +29,17 @@ effort_per_week = effort_in_time / duration return effort_per_week - (self._frequency * 1.5) + def name(self): + return self._name + + def ects(self): + return self._ects + + def frequency(self): + return self._frequency + def __repr__(self): - return f"Vorlesungsname {self._name} ECTS: {self._ects}" + return f"Vorlesungsname {self._name} ECTS: {self._ects} Zeit/Woche {self.cal_effort()}" # def __str__(self): # return self.__repr__() @@ -41,11 +52,31 @@ count = float(words[1]) ects = int(words[2]) return Lecture(name, ects, count) + pass +class Lecture2HTMLConverter: + def __init__(self): + self.__head = """<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>Vorlesung</title> +</head> +<body>""" + self.__foot = """ + </body> +</html> + """ - - - - + def to_html(self, lectures: list[Lecture]) -> str: + html = "" + for l in lectures: + html += f"""<tr> + <td>{l.name()}<td> + <td>{l.ects()}</td> + <td>{l.frequency()}</td> + <td>{l.cal_effort()}</td> + </tr>""" + return f"{self.__head}<table>{html}</table>{self.__foot}" diff --git a/study-effort/src/mystudy/main.py b/study-effort/src/mystudy/main.py index f90d7d9..2110e83 100644 --- a/study-effort/src/mystudy/main.py +++ b/study-effort/src/mystudy/main.py @@ -1,21 +1,29 @@ -from mystudy.lecture import Lecture, parse_lecture_line +from mystudy.lecture import Lecture, parse_lecture_line, Lecture2HTMLConverter from sys import argv def read_file_to_lecture(filename: str) -> list[Lecture]: lectures = [] - with open(filename, 'r') as file: - while line := file.readline(): - if len(line.strip()) > 0: - l = parse_lecture_line(line) - lectures.append(l) - return lectures + count_line = 0 + try: + with open(filename, 'r') as file: + while line := file.readline(): + count_line += 1 + if len(line.strip()) > 0: + l = parse_lecture_line(line) + lectures.append(l) + return lectures + except (IndexError, ValueError): + print(f"[{count_line}] Dateiformat fehlerbehaftet") -def main(): - filename = argv[1] - print(filename) - lectures = read_file_to_lecture(filename) - for l in lectures: - print(l) - +def start(): + try: + filename = argv[1] + lectures = read_file_to_lecture(filename) + if lectures is not None: + converter = Lecture2HTMLConverter() + html_code = converter.to_html(lectures) + print(html_code) + except FileNotFoundError: + print(f"Datei '{filename}' nicht gefunden") diff --git a/study-effort/tests/mystudy/lecture_test.py b/study-effort/tests/mystudy/lecture_test.py index cc6f0f8..2e91970 100644 --- a/study-effort/tests/mystudy/lecture_test.py +++ b/study-effort/tests/mystudy/lecture_test.py @@ -33,7 +33,7 @@ self.assertTrue(diff < EPSILON) def test_parse_lecture_line(self): - data = "English; 3; 2" + data = "English; 3 ; 2" english = parse_lecture_line(data) text = "Vorlesungsname English ECTS: 2" self.assertEqual(text, f"{english}") diff --git a/study-effort/vorlesung.csv b/study-effort/vorlesung.csv index 88eab23..a47af31 100644 --- a/study-effort/vorlesung.csv +++ b/study-effort/vorlesung.csv @@ -1,3 +1,3 @@ Mathematik; 2.5; 5 -Prog-2 ; 2 ; 5 - +Prog-2 ; 2.0; 5 +Informatik 2; 2; 5 diff --git a/turtle-geo-inheritance/pyproject.toml b/turtle-geo-inheritance/pyproject.toml new file mode 100644 index 0000000..f07ac71 --- /dev/null +++ b/turtle-geo-inheritance/pyproject.toml @@ -0,0 +1,15 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "turtle-geo" +version = "0.0.1" + +[tool.setuptools.packages.find] +# All the following settings are optional: +where = ["src"] + +[project.scripts] +ttgeo = "turtlegeo.main:main" + diff --git a/turtle-geo-inheritance/src/turtlegeo/TtGeo.py b/turtle-geo-inheritance/src/turtlegeo/TtGeo.py new file mode 100644 index 0000000..5a248c0 --- /dev/null +++ b/turtle-geo-inheritance/src/turtlegeo/TtGeo.py @@ -0,0 +1,18 @@ +from turtle import Turtle + + +class TtGeo(Turtle): + + def triangle(self, length, x, y): + self.teleport(x, y) + head = self.heading() + self.setheading(0) + self.forward(length) + self.left(120) + self.forward(length) + self.left(120) + self.forward(length) + self.setheading(head) + pass + + pass \ No newline at end of file diff --git a/turtle-geo-inheritance/src/turtlegeo/__init__.py b/turtle-geo-inheritance/src/turtlegeo/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/turtle-geo-inheritance/src/turtlegeo/__init__.py diff --git a/turtle-geo-inheritance/src/turtlegeo/main.py b/turtle-geo-inheritance/src/turtlegeo/main.py new file mode 100644 index 0000000..31ac99f --- /dev/null +++ b/turtle-geo-inheritance/src/turtlegeo/main.py @@ -0,0 +1,12 @@ +from turtle import Turtle +from random import random + +from turtlegeo.TtGeo import TtGeo + + +def main(): + t = TtGeo() + t.triangle(50, 20, 25) + t.circle(50) + t.dot(25) + t.screen.mainloop() diff --git a/turtle-geo-inheritance/tests/__init__.py b/turtle-geo-inheritance/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/turtle-geo-inheritance/tests/__init__.py diff --git a/vererbung-demo/demo.py b/vererbung-demo/demo.py new file mode 100644 index 0000000..0c9e807 --- /dev/null +++ b/vererbung-demo/demo.py @@ -0,0 +1,26 @@ +class BaseClass: + def __init__(self, a, b): + self.a = a + self.b = b + + def sumOf(self): + return self.a + self.b + + +class ExtendClass(BaseClass): + def diffOf(self): + return self.a - self.b + + +x = BaseClass(10, 9) +s = x.sumOf() +print(s) + +y = ExtendClass(12, 4) +sy = y.sumOf() +dy = y.diffOf() +print(sy, dy) + + + + -- Gitblit v1.10.0-SNAPSHOT