Python
flake8
Tool For Style Guide Enforcement:
flake8 <file_to_check> --ignore=E501
Usefull Libraries
Computer Vision: https://github.com/facebookresearch/detectron2?tab=readme-ov-file
Database/MySQL
mydb = mysql.connector.connect(
host="<hostname>",
user="<user>",
password="<password>",
database="<database>"
)
cursor = mydb.cursor(buffered=True,dictionary=True)
query = ("SELECT * FROM <table> WHERE <condition>;")
cursor.execute(query)
for entry in cursor_cleanup:
print(entry)
print(entry['<row_name>'])
# Do your Stuff here
cursor.close()
# TRUNCAT <table>
cursor_truncate = mydb.cursor(buffered=True,dictionary=True)
query = ("TRUNCATE <table>")
cursor_truncate.execute(query)
mydb.commit()
cursor_truncate.close()
# Clean entrys older a week
cursor_cleanup = mydb.cursor(buffered=True,dictionary=True)
query = ("DELETE FROM <table> WHERE time_of_entry < NOW() - INTERVAL 1 WEEK")
cursor_cleanup.execute(query)
mydb.commit()
cursor_cleanup.close()
insert_cursor = mydb.cursor(buffered=True,dictionary=True)
sql = """ INSERT INTO <table> (
<value_1>,
<value_2>,
<value_3>,
<value_4>
) VALUES (%s, %s, %s, %s)
"""
val = [
(
<insert_value_1>,
"TEST123",
<insert_value_3>,
0
)
]
insert_cursor.executemany(sql, val)
mydb.commit()
insert_cursor.close()
mydb.close()
ASGI web server
pip install uvicorn
Logging
import logging
import datetime as dt
from datetime import datetime
import traceback
# Setup Logging
logging.basicConfig(
level=logging.DEBUG,
# level=logging.INFO,
format="Start: " + str(dt.datetime.now()).replace(" ", "_") + " | %(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("/<path>-_" + str(datetime.today().strftime('%Y-%m-%d')) + "_-_debug.log"),
logging.StreamHandler(sys.stdout)
]
)
# Logg something
try:
# do stuff
except Exception as e:
logging.debug("There was an error: " + str(e))
logging.debug("Stacktrace: " + str(traceback.format_exc()))
Show all available logger dictionarys
# Get the logger dictionary
logger_dict = logging.root.manager.loggerDict
# Print all available loggers
for logger_name in logger_dict:
print(logger_name)
Set Log-Level of specific logger dictionarys
logging.getLogger('requests').setLevel(logging.CRITICAL)
logging.getLogger('fontTools').setLevel(logging.CRITICAL)
logging.getLogger('weasyprint').setLevel(logging.ERROR)
Format Output
print('{} | {1:<8}'.format('Fruit', 'Quantity'))
print('{} | {1:^8}'.format('Banana', '3'))
print('{} | {1:>8}'.format('Apple', '2'))