python
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| python [2024-07-02 11:13:04] – [MySQL] manfred | python [2024-07-28 10:02:24] (aktuell) – [venv] david | ||
|---|---|---|---|
| Zeile 7: | Zeile 7: | ||
| 2017: Instagram wechselte zu 100% auf Python 3.5 | 2017: Instagram wechselte zu 100% auf Python 3.5 | ||
| 2020: seit 01. Jan. 2020 gibt es keinen Support für Python-2 mehr | 2020: seit 01. Jan. 2020 gibt es keinen Support für Python-2 mehr | ||
| + | |||
| + | |||
| + | ===== venv ===== | ||
| + | |||
| + | * es wird stark empfohlen, venv in python zu nutzen | ||
| + | * um pakete mit pip global zu installieren ist venv jedoch unpraktisch, | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | relevante python pakete in debian | ||
| + | |||
| + | <code bash> | ||
| + | sudo apt install python-is-python3 python3 python3-pip python3-venv pipx | ||
| + | </ | ||
| + | |||
| + | paket mit pipx installieren | ||
| + | (black ist übrigens ein super python formatter, es ist sowas wie das gofmt von python) | ||
| + | |||
| + | <code bash> | ||
| + | pipx install black | ||
| + | </ | ||
| + | |||
| + | venv erstellen | ||
| + | |||
| + | <code bash> | ||
| + | mkdir / | ||
| + | cd / | ||
| + | |||
| + | type python | ||
| + | |||
| + | python -m venv .venv | ||
| + | . .venv/ | ||
| + | |||
| + | type python | ||
| + | |||
| + | touch requirements.txt | ||
| + | pip install -r requirements.txt | ||
| + | python -c ' | ||
| + | |||
| + | deactivate | ||
| + | |||
| + | type python | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== best practices ===== | ||
| + | |||
| + | sicherstellen, | ||
| + | |||
| + | <code python main.py> | ||
| + | # | ||
| + | |||
| + | def main(): | ||
| + | print(" | ||
| + | |||
| + | if __name__ == " | ||
| + | main() | ||
| + | </ | ||
| Zeile 499: | Zeile 558: | ||
| print (" | print (" | ||
| try: | try: | ||
| + | print (" | ||
| with connection.cursor() as cursor: | with connection.cursor() as cursor: | ||
| # SQL | # SQL | ||
| Zeile 508: | Zeile 568: | ||
| for row in cursor: | for row in cursor: | ||
| print(row) | print(row) | ||
| + | print (" | ||
| finally: | finally: | ||
| # Die Verbindung schließen (Close connection). | # Die Verbindung schließen (Close connection). | ||
| Zeile 525: | Zeile 586: | ||
| | | ||
| | | ||
| + | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| - | ) | + | |
| print (" | print (" | ||
| try: | try: | ||
| + | print (" | ||
| with connection.cursor() as cursor: | with connection.cursor() as cursor: | ||
| - | # SQL | + | # SQL-Variablen |
| - | sql = "SELECT Host, | + | sql = "SHOW VARIABLES LIKE ' |
| # Den AbfragenBefehl implementieren (Execute Query). | # Den AbfragenBefehl implementieren (Execute Query). | ||
| cursor.execute(sql) | cursor.execute(sql) | ||
| Zeile 541: | Zeile 604: | ||
| for row in cursor: | for row in cursor: | ||
| print(row) | print(row) | ||
| + | print (" | ||
| + | with connection.cursor() as cursor: | ||
| + | # SQL-Status | ||
| + | sql = "SHOW STATUS LIKE ' | ||
| + | # Den AbfragenBefehl implementieren (Execute Query). | ||
| + | cursor.execute(sql) | ||
| + | print (" | ||
| + | print() | ||
| + | for row in cursor: | ||
| + | print(row) | ||
| + | print (" | ||
| finally: | finally: | ||
| # Die Verbindung schließen (Close connection). | # Die Verbindung schließen (Close connection). | ||
| Zeile 576: | Zeile 650: | ||
| * [[https:// | * [[https:// | ||
| * [[https:// | * [[https:// | ||
| + | |||
| + | |||
| + | ===== Raspberry Pi 5 ===== | ||
| + | |||
| + | |||
| + | ==== Pins schalten ==== | ||
| + | |||
| + | //Das ist nur ein Code-Schnipsel, | ||
| + | |||
| + | <code python> | ||
| + | import os | ||
| + | from gpiozero import OutputDevice, | ||
| + | |||
| + | class MotorController: | ||
| + | def __init__(self, | ||
| + | self.ENA = PWMOutputDevice(ena_pin) | ||
| + | self.IN1 = OutputDevice(in1_pin) | ||
| + | self.IN2 = OutputDevice(in2_pin) | ||
| + | self.ENB = PWMOutputDevice(enb_pin) | ||
| + | self.IN3 = OutputDevice(in3_pin) | ||
| + | self.IN4 = OutputDevice(in4_pin) | ||
| + | print(" | ||
| + | |||
| + | def set_motor(self, | ||
| + | """ | ||
| + | Motor steuern. | ||
| + | motor: ' | ||
| + | direction: ' | ||
| + | speed: Geschwindigkeit (0-1) | ||
| + | """ | ||
| + | if motor == ' | ||
| + | self.ENA.value = speed | ||
| + | if direction == ' | ||
| + | self.IN1.on() | ||
| + | self.IN2.off() | ||
| + | elif direction == ' | ||
| + | self.IN1.off() | ||
| + | self.IN2.on() | ||
| + | elif motor == ' | ||
| + | self.ENB.value = speed | ||
| + | if direction == ' | ||
| + | self.IN3.on() | ||
| + | self.IN4.off() | ||
| + | elif direction == ' | ||
| + | self.IN3.off() | ||
| + | self.IN4.on() | ||
| + | elif motor == ' | ||
| + | self.ENA.value = speed | ||
| + | if direction == ' | ||
| + | self.IN1.on() | ||
| + | self.IN2.off() | ||
| + | elif direction == ' | ||
| + | self.IN1.off() | ||
| + | self.IN2.on() | ||
| + | elif motor == ' | ||
| + | self.ENB.value = speed | ||
| + | if direction == ' | ||
| + | self.IN3.on() | ||
| + | self.IN4.off() | ||
| + | elif direction == ' | ||
| + | self.IN3.off() | ||
| + | self.IN4.on() | ||
| + | |||
| + | def stop_motor(self, | ||
| + | """ | ||
| + | Motor stoppen. | ||
| + | motor: ' | ||
| + | """ | ||
| + | if motor == ' | ||
| + | self.ENA.value = 0 | ||
| + | self.IN1.off() | ||
| + | self.IN2.off() | ||
| + | elif motor == ' | ||
| + | self.ENB.value = 0 | ||
| + | self.IN3.off() | ||
| + | self.IN4.off() | ||
| + | </ | ||
/home/http/wiki/data/attic/python.1719918784.txt · Zuletzt geändert: von manfred
