Little gems
https://www.trickster.dev/post/lesser-known-parts-of-python-standard-library/
Dependency management
https://nielscautaerts.xyz/python-dependency-management-is-a-dumpster-fire.html
- Create a definition file
- Generate a lock file
- Sync your environment with the lock file
- Track both the definition file and the lock file in version control
- As of Nov 2024, use uv (pypi ecosystem) or Pixi (Conda)
https://dublog.net/blog/so-many-python-package-managers/
Best practices
https://www.b-list.org/weblog/2022/may/13/boring-python-dependencies/
- always work in, and deploy in, a virtual environment
- specify a range of versions you’re willing to accept for direct dependencies
- “compile” a list of direct dependencies to a full pinned and hashed tree of all your dependencies (see pip-tools below)
- requirements/app.in → app.txt
pip-tools
https://til.simonwillison.net/python/pip-tools
- compile requirements.in → requirements.txt
- sync
Modern use of pyproject.toml with setuptools
- PEP-517
- PEP-518
- pyproject.toml
Awesome example
https://github.com/tiangolo/fastapi/blob/master/pyproject.toml
https://godatadriven.com/blog/a-practical-guide-to-setuptools-and-pyproject-toml/
https://hynek.me/til/pip-tools-and-pyproject-toml/
https://snarky.ca/what-the-heck-is-pyproject-toml/
https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html
http://ivory.idyll.org/blog/2021-transition-to-pyproject.toml-example.html
Rye
- All-in-one tooling written in Rust (dependencies, linter, build, publish)
-
- Wrapper for a number of standard tools
Why avoid Poetry
https://usrme.xyz/posts/why-i-moved-away-from-poetry-for-python/#the-fault-in-poetrys-stars
Version of dependencies
https://stackoverflow.com/questions/54720072/dependency-version-syntax-for-python-poetry
- how to specify loose versions in setup.py, requirements.in or pyproject.toml
Don’t cap upper versions
https://iscinumpy.dev/post/bound-version-constraints/#tldr
ALL packaging and distribution tools
https://packaging.python.org/en/latest/key_projects.html
Project template with cookiecutter
https://github.com/TezRomacH/python-package-template
Dev tools
To create CLI tools
https://click.palletsprojects.com/en/stable/
https://google.github.io/python-fire/
Modules that double as CLI commands
https://www.pythonmorsels.com/cli-tools/
- http.server
- webbrowser
- json.tool
- calendar
- …
- pip, venv, pdb
- zipapp
- tokenize, ast, dis, inspect, pyclbr
- asyncio
- cProfile, profile, pstats
- pickle, pickletools
Hot reloading
https://github.com/reloadware/reloadium
Profilers
Scalene
https://github.com/plasma-umass/scalene
- CPU, GPU, memory
- Percentage in Python vs C
Memray
https://github.com/bloomberg/memray
Tools for code quality
https://missing.csail.mit.edu/2020/debugging-profiling/
https://testdriven.io/blog/python-code-quality
- linters
- formatters
- code quality scanners
- CI setup
Cached package installation with wheels
Example from Earthly
https://github.com/earthly/earthly/blob/main/examples/python/Earthfile
VERSION 0.7
FROM python:3
WORKDIR /code
deps:
RUN pip install wheel
COPY requirements.txt ./
RUN pip wheel -r requirements.txt --wheel-dir=wheels
build:
FROM +deps
COPY src src
SAVE ARTIFACT src /src
SAVE ARTIFACT wheels /wheels
docker:
COPY +build/src src
COPY +build/wheels wheels
COPY requirements.txt ./
RUN pip install --no-index --find-links=wheels -r requirements.txt
ENTRYPOINT ["python3", "./src/hello.py"]
SAVE IMAGE --push earthly/examples:python
Hosting private packages on S3 bucket
https://github.com/WhoopInc/mkwheelhouse
https://github.com/gorilla-co/s3pypi
Publishing packages to PyPi and ReadTheDocs
https://testdriven.io/blog/python-project-workflow
Trusted publishers and attestations
https://docs.pypi.org/trusted-publishers/
https://blog.trailofbits.com/2023/05/23/trusted-publishing-a-new-benchmark-for-packaging-security/
https://blog.trailofbits.com/2024/11/14/attestations-a-new-generation-of-signatures-on-pypi/
Packaging and distributing as zip file
https://docs.python.org/3/library/zipapp.html#creating-standalone-applications-with-zipapp
(And as executable)
Pex - Python executable
https://github.com/pex-tool/pex
Performance - GIL and concurrency
https://pythonspeed.com/articles/python-gil/
asyncio - async / await pros and cons
https://www.dcaulfield.com/onboarding-antipatterns
https://lucumr.pocoo.org/2016/10/30/i-dont-understand-asyncio/
https://techspot.zzzeek.org/2015/02/15/asynchronous-python-and-databases/
mypyc and other concerns
https://blog.meadsteve.dev/programming/2022/09/27/making-python-fast-for-free/
- compilation
- performance optimization
- packaging with setuptools
- publishing prebuilt wheels from GH Actions
Packaging and publishing with fbs
https://github.com/mherrmann/fbs-tutorial
Extremely good advice on style, code organization
https://leontrolski.github.io/mostly-pointless.html
- simple
- no OOP equivalents
https://leontrolski.github.io/sane-config.html
- simple config
functools
https://martinheinz.dev/blog/52
- caching, memoization
- comparing and ordering
- overloading
- partial
- decorators and wrapping
- reduce
Named tuples
https://death.andgravity.com/namedtuples
Cryptographic random number / ID generation
https://docs.python.org/3/library/secrets.html
Refactoring, code upgrades
https://github.com/asottile/pyupgrade
Ecosystem
compiler from type-annotated python to c extension
https://github.com/python/mypy/tree/master/mypyc
Incremental parsing of large JSON
https://pythonspeed.com/articles/json-memory-streaming/
Debugging / logging packages
https://stribny.name/blog/2019/06/debugging-python-programs/
- extensive list of debuggint tools / techniques
https://github.com/gruns/icecream
Memory profilers
https://github.com/bloomberg/memray https://bloomberg.github.io/memray/
Tracing
https://github.com/kunalb/panopticon
Template for excellent Python project
https://antonz.org/python-packaging/
Unit tests
- addCleanup(…) to clean up resources regardless of test pass or failure
Books
Effective Python
Fluent Python
Python Distilled
http://www.dabeaz.com/python-distilled/
Automate The Boring Stuff with Python
https://automatetheboringstuff.com/
Serious Python
https://nostarch.com/seriouspython
Language changes
3.12
- New generic type parameter syntax
- Lifts some restrictions on f-strings
- Per-interpreter GIL
- Low-overhead profiling interface
- Makes buffer protocol accessible to Python code
- Comprehension inlining
- TypeDict for type hints
- @override decorator