python
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| python [2024-07-02 10:47:56] – [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 447: | Zeile 506: | ||
| ===== MySQL ===== | ===== MySQL ===== | ||
| - | * [[https:// | + | * [[https:// |
| + | * [[https:// | ||
| + | * [[https:// | ||
| * Python-shebang (Linux/ | * Python-shebang (Linux/ | ||
| Zeile 485: | Zeile 546: | ||
| # Connect to the database | # Connect to the database | ||
| - | connection = pymysql.connect(host=' | + | connection = pymysql.connect( |
| | | ||
| | | ||
| + | | ||
| + | | ||
| | | ||
| - | | + | connect_timeout=10, |
| + | | ||
| + | cursorclass=pymysql.cursors.DictCursor | ||
| + | ) | ||
| print (" | print (" | ||
| try: | try: | ||
| + | print (" | ||
| with connection.cursor() as cursor: | with connection.cursor() as cursor: | ||
| # SQL | # SQL | ||
| Zeile 501: | Zeile 568: | ||
| for row in cursor: | for row in cursor: | ||
| print(row) | print(row) | ||
| + | print (" | ||
| + | finally: | ||
| + | # Die Verbindung schließen (Close connection). | ||
| + | connection.close() | ||
| + | </ | ||
| + | |||
| + | <file python mysql-test_+_tls.py> | ||
| + | # | ||
| + | |||
| + | import pymysql.cursors | ||
| + | |||
| + | # Connect to the database | ||
| + | connection = pymysql.connect( | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | ) | ||
| + | print (" | ||
| + | try: | ||
| + | print (" | ||
| + | with connection.cursor() as cursor: | ||
| + | # SQL-Variablen | ||
| + | sql = "SHOW VARIABLES LIKE ' | ||
| + | # Den AbfragenBefehl implementieren (Execute Query). | ||
| + | cursor.execute(sql) | ||
| + | print (" | ||
| + | print() | ||
| + | for row in cursor: | ||
| + | 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 536: | 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.1719917276.txt · Zuletzt geändert: von manfred
