projekt:python_erste_skripte
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| projekt:python_erste_skripte [2026/02/15 17:33] – [Quellcode] torsten.roehl | projekt:python_erste_skripte [2026/02/23 08:19] (aktuell) – [Example 2: reflex_advanced] torsten.roehl | ||
|---|---|---|---|
| Zeile 20: | Zeile 20: | ||
| - | ===== Example 1: reflexio | + | ====== Example 1: reflex_simple.py |
| Zeile 26: | Zeile 26: | ||
| **Aufgabe - " | **Aufgabe - " | ||
| - | Schreibe ein Python-Programm ('' | + | Schreibe ein Python-Programm ('' |
| * Beim Start soll eine kleine Anleitung erscheinen. | * Beim Start soll eine kleine Anleitung erscheinen. | ||
| Zeile 34: | Zeile 34: | ||
| </ | </ | ||
| - | ==== Quellcode ==== | + | ===== Quellcode |
| - | <code python | + | <code python |
| # | # | ||
| Zeile 41: | Zeile 41: | ||
| # @author: DEIN NAME | # @author: DEIN NAME | ||
| # @version: 1.0 | # @version: 1.0 | ||
| - | # @usage: | + | # @usage: |
| Zeile 47: | Zeile 47: | ||
| import random | import random | ||
| - | VERSION = "This is reflexio v.1.0" | + | VERSION = "This is reflex_simple v1.0.0" |
| def main(): | def main(): | ||
| print(VERSION) | print(VERSION) | ||
| - | print(" | + | print(" |
| print(" | print(" | ||
| print(" | print(" | ||
| Zeile 81: | Zeile 81: | ||
| </ | </ | ||
| - | ==== Programm ausführen ==== | + | ===== Programm ausführen |
| - | Damit ein Python-Skript per './reflexio' ausführbar ist, müssen die unten aufgeführten Punkte erfüllt sein. | + | Damit ein Python-Skript per './reflex_simple.py' ausführbar ist, müssen die unten aufgeführten Punkte erfüllt sein. |
| - Erste Zeile (**Shebang**) in die Datei einfügen: ''# | - Erste Zeile (**Shebang**) in die Datei einfügen: ''# | ||
| - | - Datei ausführbar machen: '' | + | - Datei ausführbar machen: '' |
| - | - Starten: .''/ | + | - Starten: .''/ |
| - | ===== Example | + | ====== Example |
| Zeile 96: | Zeile 96: | ||
| **Aufgabe – „Schnell, schneller, am schnellsten“** | **Aufgabe – „Schnell, schneller, am schnellsten“** | ||
| - | Schreibe ein Python-Programm ('' | + | Schreibe ein Python-Programm ('' |
| * Beim Programmstart sollen folgende Optionen ausgewählt werden können: | * Beim Programmstart sollen folgende Optionen ausgewählt werden können: | ||
| Zeile 118: | Zeile 118: | ||
| </ | </ | ||
| - | ==== Environment anlegen ==== | ||
| - | <WRAP center round box 80%> | ||
| - | **Aufgabe: Anlegen einer Python-Environment** | ||
| - | Lege eine Umgebung | ||
| - | * '' | ||
| - | Hierfür muss der Ordner '' | ||
| - | </ | + | ===== Programmstruktur anlegen |
| - | <note tip> | + | ==== Hauptverzeichnis des Projekts: |
| - | Das Anlegen und Managen einer Umgebung ist hier | + | |
| - | [[python_python-umgebung_env_einrichten_vorbereitungen_fuer_projekte_schaffen|Python ENV]] | + | |
| - | ausführlich beschrieben. | + | |
| - | </ | + | |
| - | + | ||
| - | Wechsle anschließend in die Umgebung, um das Example zu bearbeiten! | + | |
| - | + | ||
| - | ==== Programmstruktur anlegen | + | |
| - | + | ||
| - | === Hauptverzeichnis des Projekts: | + | |
| <code bash> | <code bash> | ||
| - | pi@raspi88: | + | pi@raspi88: |
| - | / | + | / |
| </ | </ | ||
| - | === Programmstrukur | + | ==== Programmstruktur ==== |
| <code bash> | <code bash> | ||
| - | └── | + | └── |
| - | ├── reflexio_advanced_env | + | │ |
| - | │ └── ... | + | |
| - | | + | |
| └── src # Ordner für den Quellcode | └── src # Ordner für den Quellcode | ||
| ├── core # core-Verzeichnis | ├── core # core-Verzeichnis | ||
| │ | │ | ||
| - | │ └── utility.py | + | │ ├── utility.py |
| │ | │ | ||
| - | └── | + | └── |
| </ | </ | ||
| Zeile 162: | Zeile 145: | ||
| - | ==== Quellcode ==== | + | ===== Quellcode ===== |
| + | |||
| + | ==== ohne Programmstruktur ==== | ||
| + | |||
| + | < | ||
| + | Der untenstehende Code funktioniert, | ||
| + | Er ist alles andere als elegant – so etwas nennt man auch **Spaghetti-Code**. | ||
| + | |||
| + | Der Code ist | ||
| + | - sehr unübersichtlich und daher fehleranfällig, | ||
| + | - nicht gut wiederverwendbar, | ||
| + | - schwer zu debuggen, falls etwas nicht stimmt. | ||
| + | |||
| + | Der Code – insbesondere die Funktionen – sollen ebenfalls dokumentiert werden, was im Kurs gezeigt wird. | ||
| + | </ | ||
| ++++ ohne Programmstruktur | | ++++ ohne Programmstruktur | | ||
| - | <code python | + | |
| + | <code python | ||
| # | # | ||
| Zeile 172: | Zeile 171: | ||
| import os | import os | ||
| - | VERSION = "This is reflexio-advanced | + | VERSION = "This is reflex_spaghetti |
| DATA_FILENAME = " | DATA_FILENAME = " | ||
| DATA_DIR = " | DATA_DIR = " | ||
| - | def get_file(): | + | def main(): |
| + | print(VERSION) | ||
| + | |||
| + | # --- Beginn get_file() --- | ||
| base_dir = os.path.dirname(__file__) | base_dir = os.path.dirname(__file__) | ||
| data_path = os.path.join(base_dir, | data_path = os.path.join(base_dir, | ||
| os.makedirs(data_path, | os.makedirs(data_path, | ||
| - | | + | |
| + | # --- Ende get_file() --- | ||
| + | while True: | ||
| - | def start_measurement(): | + | # --- Beginn show_menu() --- |
| - | print(" | + | |
| - | input() | + | |
| - | print(" | + | |
| - | + | ||
| - | time.sleep(random.uniform(2, | + | |
| - | + | ||
| - | print(" | + | |
| - | print(" | + | |
| - | + | ||
| - | start = time.perf_counter() | + | |
| - | input() | + | |
| - | end = time.perf_counter() | + | |
| - | return end - start | + | |
| - | + | ||
| - | + | ||
| - | def save_value(file_path, | + | |
| - | with open(file_path, | + | |
| - | f.write(f" | + | |
| - | + | ||
| - | + | ||
| - | def load_values(file_path): | + | |
| - | if not os.path.exists(file_path): | + | |
| - | return [] | + | |
| - | + | ||
| - | with open(file_path, | + | |
| - | return [float(line.strip()) for line in f if line.strip()] | + | |
| - | + | ||
| - | + | ||
| - | def calculate_average(values): | + | |
| - | if not values: | + | |
| - | return None | + | |
| - | return sum(values) / len(values) | + | |
| - | + | ||
| - | + | ||
| - | def reset_data(file_path): | + | |
| - | if os.path.exists(file_path): | + | |
| - | os.remove(file_path) | + | |
| - | return True | + | |
| - | return False | + | |
| - | + | ||
| - | + | ||
| - | def main(): | + | |
| - | print(VERSION) | + | |
| - | + | ||
| - | file_path = get_file() | + | |
| - | + | ||
| - | while True: | + | |
| print(" | print(" | ||
| print(" | print(" | ||
| Zeile 239: | Zeile 196: | ||
| print(" | print(" | ||
| print(" | print(" | ||
| + | # --- Ende show_menu() --- | ||
| choice = input(" | choice = input(" | ||
| + | # ===================================================== | ||
| + | # MESSEN | ||
| + | # ===================================================== | ||
| if choice == " | if choice == " | ||
| - | | + | |
| + | # --- Beginn start_measurement() --- | ||
| + | print(" | ||
| + | input() | ||
| + | print(" | ||
| + | |||
| + | time.sleep(random.uniform(2, | ||
| + | |||
| + | print(" | ||
| + | |||
| + | start = time.perf_counter() | ||
| + | input() | ||
| + | end = time.perf_counter() | ||
| + | | ||
| + | # --- Ende start_measurement() | ||
| print(" | print(" | ||
| print(f" | print(f" | ||
| - | save_value(file_path, | + | |
| - | | + | with open(file_path, |
| + | f.write(f" | ||
| + | | ||
| - | | + | |
| - | if avg is not None: | + | if os.path.exists(file_path): |
| + | with open(file_path, | ||
| + | values | ||
| + | else: | ||
| + | | ||
| + | # --- Ende load_values() --- | ||
| + | |||
| + | # --- Beginn calculate_average() --- | ||
| + | if values: | ||
| + | avg = sum(values) / len(values) | ||
| print(f" | print(f" | ||
| print(f" | print(f" | ||
| + | # --- Ende calculate_average() --- | ||
| + | # ===================================================== | ||
| + | # STATISTIK | ||
| + | # ===================================================== | ||
| elif choice == " | elif choice == " | ||
| - | | + | |
| + | # --- Beginn load_values() --- | ||
| + | if os.path.exists(file_path): | ||
| + | with open(file_path, | ||
| + | | ||
| + | else: | ||
| + | values = [] | ||
| + | # --- Ende load_values() | ||
| if not values: | if not values: | ||
| print(" | print(" | ||
| else: | else: | ||
| - | avg = calculate_average(values) | + | |
| + | | ||
| print(f" | print(f" | ||
| print(f" | print(f" | ||
| + | # --- Ende calculate_average() --- | ||
| + | # ===================================================== | ||
| + | # RESET | ||
| + | # ===================================================== | ||
| elif choice == " | elif choice == " | ||
| - | if reset_data(file_path): | + | |
| + | # --- Beginn | ||
| + | if os.path.exists(file_path): | ||
| + | os.remove(file_path) | ||
| print(" | print(" | ||
| else: | else: | ||
| print(" | print(" | ||
| + | # --- Ende reset_data() --- | ||
| + | # ===================================================== | ||
| + | # HILFE | ||
| + | # ===================================================== | ||
| elif choice == " | elif choice == " | ||
| + | |||
| + | # --- Beginn show_help() --- | ||
| print(" | print(" | ||
| print(" | print(" | ||
| Zeile 282: | Zeile 295: | ||
| print(" | print(" | ||
| print(" | print(" | ||
| + | # --- Ende show_help() --- | ||
| + | # ===================================================== | ||
| + | # BEENDEN | ||
| + | # ===================================================== | ||
| elif choice == " | elif choice == " | ||
| print(" | print(" | ||
| Zeile 298: | Zeile 315: | ||
| + | ==== mit Programmstruktur ==== | ||
| + | |||
| + | < | ||
| + | Der Code ist nun modularisiert. | ||
| + | Er ist deutlich übersichtlicher und lässt sich dadurch besser warten und verstehen. | ||
| + | |||
| + | Außerdem können z.B. die Module wiederverwendet werden – ebenso kann das Utility-Modul als Vorlage für andere Programme dienen. | ||
| + | |||
| + | Die Dokumentation der Methoden (Docstrings) fehlt hier übrigens noch. | ||
| + | |||
| + | * **Logik** Enthält die „Game-Logik“, | ||
| + | * **Utility** Enthält Hilfsfunktionen, | ||
| + | * **Hauptprogramm** Das eigentliche Programm. Hier sollte man starten, um die Funktionsweise des Programms zu überprüfen. | ||
| + | |||
| + | </ | ||
| ++++ mit Programmstruktur | | ++++ mit Programmstruktur | | ||
| - | === Logik === | + | |
| + | |||
| + | ==== Logik ==== | ||
| <code python logic.py > | <code python logic.py > | ||
| import time | import time | ||
| Zeile 357: | Zeile 391: | ||
| </ | </ | ||
| - | === utility === | + | ==== utility |
| <code python utility.py> | <code python utility.py> | ||
| import os | import os | ||
| Zeile 366: | Zeile 400: | ||
| DATADIR = " | DATADIR = " | ||
| FILENAME = " | FILENAME = " | ||
| - | |||
| # | # | ||
| # FUNKTIONEN | # FUNKTIONEN | ||
| # | # | ||
| - | |||
| def get_file(): | def get_file(): | ||
| Zeile 402: | Zeile 434: | ||
| print(" | print(" | ||
| print(" | print(" | ||
| - | |||
| </ | </ | ||
| - | === Hauptprogramm === | + | ==== Hauptprogramm |
| - | <code python | + | <code python |
| # | # | ||
| Zeile 420: | Zeile 451: | ||
| from core.utility import get_file, show_menu, show_help | from core.utility import get_file, show_menu, show_help | ||
| - | VERSION = "This is reflexio-advanced | + | VERSION = "This is reflex_advanced |
| def main(): | def main(): | ||
projekt/python_erste_skripte.1771176839.txt.gz · Zuletzt geändert: von torsten.roehl
