Skip to content

Latest commit

 

History

History
177 lines (93 loc) · 6.9 KB

File metadata and controls

177 lines (93 loc) · 6.9 KB

Python HowTo

Learning

Chicago Python has learning resources

Cheat Sheets

mypy typing hints

Formatting

For a long time I used tabs but the standard is spaces. I surrender and am switching to spaces The following command will change all python files to space indentation

find . -name '*.py' ! -type d -exec bash -c 'expand -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \;

Python Version Management

uv

hatch

Hatch is python packaging authority tool.

pyenv

abandoned for uv and hatch

Tools

Vulnerability testing: Python safety checks? Does this exist?

mypy

Install the mypy VS Code extension

Instructions in that extension tell you to install the language server:

$ python -m venv ~/.mypyls
$ ~/.mypyls/bin/pip install "https://github.com/matangover/mypyls/archive/master.zip#egg=mypyls[default-mypy]"

On Debian, must install python3-dev

sudo apt install python3-dev

VS Code

Hinting and Linting in VS Code

https://devblogs.microsoft.com/python/python-linting-video/?ocid=python_eml_tnp_autoid10_readmore

PYTHONPATH

This is more complex that it should be. I often have one or two library directories that need to be available on the path.

settings.json will give you the path in terminals. Since the debugger is run from a terminal, this is where the debugger gets the path.

"terminal.integrated.env.osx": {
    "PYTHONPATH": "${workspaceRoot}/lib"	        
},
"terminal.integrated.env.linux": {
    "PYTHONPATH": "${workspaceRoot}/lib"	        
}

.env file.

PYTHONPATH=lib

Setting python.analysis.extraPaths tells pylance where to look for source code.

Profiling

RunSnakeRun provide a GUI that allows you to view (Python) cProfile or Profile profiler dumps in a sortable GUI view.

kcachegrind a KDE GUI display of profilers.

Message Queue

MQ Libraries

  • kombu provides a high level interface.
  • amqp Pure python client alternate to librabbitmq.
  • librabbitmq High performance interface to librabbit.

Tasks

Asynchronous Tasks With Django and Celery

includes how to use redis and how to use redis as the cellery broker and database back end. How to use celery with django.

Frameworks

James Bennett's asyncio blog post (8/2022) lists several frameworks.

Libraries

attrs

Attrs module provides much of the class writing boiler plate. Lighter than pydantic, which is data validation.

pydantic

Pydantic module "provides data validation and settings management using pythong type annotations."

Database

Python as a DB API specification in PEP249. As I understand, database drives implement this interface and provide the interface to various database servers. This is a low level interface. In theory, I think, you can change database by importing a different DBAPI driver.

Sql Server pyodbc is the driver for MS SQL Server and other ODBC. Source on GitHub

aiodbc is an async ODBC driver that uses pyodbc and creates threads to manage async.

On top of DBAPI drivers, several higher level frameworks provide a more abstract database interface.

SQLAlchemy - generally what people use for DB /ORM.

Databases Project Databases gives you simple asyncio support for a range of databases. It allows you to make queries using the powerful SQLAlchemy Core expression language, and provides support for PostgreSQL, MySQL, and SQLite.

SQLModel SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness. Powered by Pydanic and SQLSlchemy. Designe to simplify interacting with SQL database in FastAPI.

Why choose SQL Model over SQLAlchemy?

Requests

The requests is the standard for making HTTP requests. Requests recommends several packages:

  • CacheControl caching

  • Requests-Threads is a Requests session that returns the amazing Twisted’s awaitable Deferreds instead of Response objects. This allows the use of async/await keyword usage on Python 3, or Twisted’s style of programming, if desired.

  • Requests-Toolbelt is a collection of utilities that some users of Requests may desire, but do not belong in Requests proper.

  • requests-oauthlib makes it possible to do the OAuth dance from Requests automatically. This is useful for the large number of websites that use OAuth to provide authentication. It also provides a lot of tweaks that handle ways that specific OAuth providers differ from the standard specifications.

  • Certifi is a carefully curated collection of Root Certificates for validating the trustworthiness of SSL certificates while verifying the identity of TLS hosts. It has been extracted from the Requests project.

HTTPX HTTPX is a fully featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2. Mostly a drop in replacement for requests.

Caching general

dogpile provide threaded caching, including managing updates.

Web frameworks

Django

Flask

Shiny for Python Interactive web applications.

nicegui Web interface to your application.

Web Servers

A web framework needs to be run by a web server:

Gunicorn a WSGI HTTP server for unix.

ASGI server implementations, like Daphne, Hypercorn, and Uvicorn.

API

Starlette Starlette is a lightweight ASGI framework/toolkit, which is ideal for building async web services in Python.

FastAPI is the web framework that seems to have the most interest/noise around it.

LightStar API framework. Featured on Talk Python to Me.

msgpack fast message serialization. Used by LightStar.