Benutzer-Werkzeuge

Webseiten-Werkzeuge


python

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
python [2024-07-02 11:10:26] – [MySQL] manfredpython [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, dafür gibt es pipx, welches ein separates venv für das jeweilige paket managed, d.h.
 +    * ''pipx install'': pakete installieren
 +    * ''pip install'' (innerhalb venv): abhängigkeiten für ein projekt in einem venv installieren (abhängigkeiten stehen üblicherweise in einer ''requirements.txt'' datei)
 +    * ''pip install'' (außerhalb venv): vermeiden wenn möglich
 +
 +relevante python pakete in debian
 +
 +<code bash>
 +sudo apt install python-is-python3 python3 python3-pip python3-venv pipx
 +</code>
 +
 +paket mit pipx installieren
 +(black ist übrigens ein super python formatter, es ist sowas wie das gofmt von python)
 +
 +<code bash>
 +pipx install black
 +</code>
 +
 +venv erstellen
 +
 +<code bash>
 +mkdir /tmp/python-venv-demo
 +cd /tmp/python-venv-demo
 +
 +type python
 +
 +python -m venv .venv
 +. .venv/bin/activate
 +
 +type python
 +
 +touch requirements.txt
 +pip install -r requirements.txt
 +python -c 'print("hello world")'
 +
 +deactivate
 +
 +type python
 +</code>
 +
 +
 +===== best practices =====
 +
 +sicherstellen, dass der main code nur ausgeführt wird, wenn die datei als programm ausgeführt wird, nicht wenn sie als modul importiert wird (das macht der ''%%__name__ == "__main__"%%'' check)
 +
 +<code python main.py>
 +#!/usr/bin/env python3
 +
 +def main():
 +    print("Hello World!")
 +
 +if __name__ == "__main__":
 +    main()
 +</code>
  
  
Zeile 494: Zeile 553:
                              database='mysql',                              database='mysql',
                              connect_timeout=10,                              connect_timeout=10,
 +                             autocommit='True',
                              cursorclass=pymysql.cursors.DictCursor                              cursorclass=pymysql.cursors.DictCursor
                              )                              )
 print ("connect successful!!") print ("connect successful!!")
 try: try:
 +    print ("================================================================================")
     with connection.cursor() as cursor:     with connection.cursor() as cursor:
         # SQL         # SQL
Zeile 507: 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 524: Zeile 586:
                              port=3306,                              port=3306,
                              database='mysql',                              database='mysql',
 +                             ssl={"fake_flag_to_enable_tls":True},
                              ssl_ca='/var/lib/mysql/ca.pem',                              ssl_ca='/var/lib/mysql/ca.pem',
                              connect_timeout=10,                              connect_timeout=10,
 +                             autocommit='True',
                              cursorclass=pymysql.cursors.DictCursor                              cursorclass=pymysql.cursors.DictCursor
-                             )+                              
 print ("connect successful!!") print ("connect successful!!")
 try: try:
 +    print ("================================================================================")
     with connection.cursor() as cursor:     with connection.cursor() as cursor:
-        # SQL +        # SQL-Variablen 
-        sql = "SELECT Host,User,ssl_type,ssl_cipher,max_connections,max_user_connections,plugin,max_statement_time FROM user;"+        sql = "SHOW VARIABLES LIKE '%ssl%';"
         # Den AbfragenBefehl implementieren (Execute Query).         # Den AbfragenBefehl implementieren (Execute Query).
         cursor.execute(sql)         cursor.execute(sql)
Zeile 539: 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 '%ssl%';"
 +        # Den AbfragenBefehl implementieren (Execute Query).
 +        cursor.execute(sql)
 +        print ("cursor.description: ", cursor.description)
 +        print()
 +        for row in cursor:
 +            print(row)
 +    print ("================================================================================")
 finally: finally:
     # Die Verbindung schließen (Close connection).     # Die Verbindung schließen (Close connection).
Zeile 574: Zeile 650:
   * [[https://www.youtube.com/watch?v=5TzqfheD3rQ|Die Mandelbrotmenge]]   * [[https://www.youtube.com/watch?v=5TzqfheD3rQ|Die Mandelbrotmenge]]
   * [[https://www.youtube.com/watch?v=vWz5d6FZsbk|Ägyptische Brüche (und zwei ungelöste mathematische Probleme)]]   * [[https://www.youtube.com/watch?v=vWz5d6FZsbk|Ägyptische Brüche (und zwei ungelöste mathematische Probleme)]]
 +
 +
 +===== Raspberry Pi 5 =====
 +
 +
 +==== Pins schalten ====
 +
 +//Das ist nur ein Code-Schnipsel, an dem zu erkennen ist, dass die Pin-Steuerung beim Pi 5 jetzt anders erfolgt als es beim Pi4 war.//
 +
 +<code python>
 +import os
 +from gpiozero import OutputDevice, PWMOutputDevice
 +
 +class MotorController:
 +    def __init__(self, ena_pin, in1_pin, in2_pin, enb_pin, in3_pin, in4_pin):
 +        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("INIT OK")
 +
 +    def set_motor(self, motor, direction, speed):
 +        """
 +        Motor steuern.
 +        motor: 'A1', 'A2', 'B1' oder 'B2'
 +        direction: 'forward' oder 'backward'
 +        speed: Geschwindigkeit (0-1)
 +        """
 +        if motor == 'A1':
 +            self.ENA.value = speed
 +            if direction == 'forward':
 +                self.IN1.on()
 +                self.IN2.off()
 +            elif direction == 'backward':
 +                self.IN1.off()
 +                self.IN2.on()
 +        elif motor == 'B1':
 +            self.ENB.value = speed
 +            if direction == 'forward':
 +                self.IN3.on()
 +                self.IN4.off()
 +            elif direction == 'backward':
 +                self.IN3.off()
 +                self.IN4.on()
 +        elif motor == 'A2':
 +            self.ENA.value = speed
 +            if direction == 'forward':
 +                self.IN1.on()
 +                self.IN2.off()
 +            elif direction == 'backward':
 +                self.IN1.off()
 +                self.IN2.on()
 +        elif motor == 'B2':
 +            self.ENB.value = speed
 +            if direction == 'forward':
 +                self.IN3.on()
 +                self.IN4.off()
 +            elif direction == 'backward':
 +                self.IN3.off()
 +                self.IN4.on()
 +
 +    def stop_motor(self, motor):
 +        """
 +        Motor stoppen.
 +        motor: 'A1', 'A2', 'B1' oder 'B2'
 +        """
 +        if motor == 'A1' or motor == 'A2':
 +            self.ENA.value = 0
 +            self.IN1.off()
 +            self.IN2.off()
 +        elif motor == 'B1' or motor == 'B2':
 +            self.ENB.value = 0
 +            self.IN3.off()
 +            self.IN4.off()
 +</code>
  
  
/home/http/wiki/data/attic/python.1719918626.txt · Zuletzt geändert: von manfred