Hong-Phuc Bui
2024-07-01 82915d6ee31fd763e7cd2391a25c225060cf1120
Vererbung
5 files modified
9 files added
234 ■■■■■ changed files
oop/vererbung/vererbung.py 46 ●●●●● patch | view | raw | blame | history
study-effort/dem-vorlesung.html 28 ●●●●● patch | view | raw | blame | history
study-effort/pyproject.toml 2 ●●● patch | view | raw | blame | history
study-effort/src/mystudy/DataFormatError.py 4 ●●●● patch | view | raw | blame | history
study-effort/src/mystudy/lecture.py 41 ●●●● patch | view | raw | blame | history
study-effort/src/mystudy/main.py 36 ●●●●● patch | view | raw | blame | history
study-effort/tests/mystudy/lecture_test.py 2 ●●● patch | view | raw | blame | history
study-effort/vorlesung.csv 4 ●●●● patch | view | raw | blame | history
turtle-geo-inheritance/pyproject.toml 15 ●●●●● patch | view | raw | blame | history
turtle-geo-inheritance/src/turtlegeo/TtGeo.py 18 ●●●●● patch | view | raw | blame | history
turtle-geo-inheritance/src/turtlegeo/__init__.py patch | view | raw | blame | history
turtle-geo-inheritance/src/turtlegeo/main.py 12 ●●●●● patch | view | raw | blame | history
turtle-geo-inheritance/tests/__init__.py patch | view | raw | blame | history
vererbung-demo/demo.py 26 ●●●●● patch | view | raw | blame | history
oop/vererbung/vererbung.py
New file
@@ -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)
study-effort/dem-vorlesung.html
New file
@@ -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>
study-effort/pyproject.toml
@@ -11,5 +11,5 @@
where = ["src"]
[project.scripts]
mystudy = "mystudy.main:main"
mystudy = "mystudy.main:start"
study-effort/src/mystudy/DataFormatError.py
New file
@@ -0,0 +1,4 @@
class DataFormatError(Exception):
    def __init__(self):
        super().__init__()
    pass
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}"
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")
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}")
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
turtle-geo-inheritance/pyproject.toml
New file
@@ -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"
turtle-geo-inheritance/src/turtlegeo/TtGeo.py
New file
@@ -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
turtle-geo-inheritance/src/turtlegeo/__init__.py
turtle-geo-inheritance/src/turtlegeo/main.py
New file
@@ -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()
turtle-geo-inheritance/tests/__init__.py
vererbung-demo/demo.py
New file
@@ -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)