Skip to content

Latest commit

 

History

History
127 lines (96 loc) · 21.9 KB

SKILL.md

File metadata and controls

127 lines (96 loc) · 21.9 KB
name python-cookbook
description Practical, example-driven recipes for the Python standard library and everyday idioms - string/text wrangling, formatting, regex, numbers, the core data structures (list/tuple/set/dict) and comprehensions/itertools, functions, classes, dates/times, modules/imports, files/paths/IO, env vars and argparse, subprocess, serialization (JSON/CSV/pickle/XML/gzip), concurrency, networking, logging, debugging, introspection, CPU/memory profiling, exceptions, testing with pytest/unittest, virtualenvs, packaging, and code quality. Use when the user asks "how do I do X in Python?", needs a stdlib snippet or idiom, is formatting strings/numbers, writing a regex, transforming a dict/list, handling paths/files, parsing CLI args, running a subprocess, reading/writing JSON/CSV, adding logging, profiling slow code, tracking memory, writing tests, pinning dependencies, or packaging a project. Distilled from "Stas' Python Cookbook" open book, the latest version of which can be found at https://github.com/stas00/python-cookbook The latest SKILL.md version can be found at https://github.com/stas00/python-cookbook/blob/master/SKILL.md

Stas' Python Cookbook

Distilled from Stas' Python Cookbook open book by Stas Bekman - source: https://github.com/stas00/python-cookbook (CC BY-SA 4.0). This skill is a condensed index; each section links back to the full chapter for depth, runnable snippets, and gotchas.

A practical, standard-library-first reference for the Python idioms that come up again and again in real work. It leads with the why, shows the how with copy-paste snippets, and calls out the gotchas that bite in practice. Targets Python 3.8+; version-specific features are flagged inline. For deep single-process/tool debugging (gdb, strace, py-spy, core files, CUDA) pair this with The Art of Debugging.

Core principles

  • Reach for the standard library first. A third-party package is only suggested when the stdlib genuinely falls short - and it's flagged with pip install.
  • Prefer the modern idiom. f-strings over %/.format(), pathlib over os.path, subprocess.run over os.system, dataclasses over ad-hoc tuples, logging over print in real programs.
  • Know the gotcha before it bites. Mutable default arguments, shallow vs deep copy, pass-by-object-reference, naive vs aware datetimes, the GIL - each chapter flags the trap.
  • Read the linked section before applying a recipe - each has worked examples, caveats, and edge cases the one-liner here omits.

Part I - Language core

Full chapters: Strings · Formatting · Numbers · Regex.

Part I - Data structures

Full chapters: Lists · Tuples · Sets · Dictionaries · Comprehensions/itertools.

Part I - Functions, classes, time

Full chapters: Functions · Classes · Dates.

Part II - Runtime & environment

Full chapters: Modules · Files/IO · Env & args · Subprocess.

Part II - Data, concurrency, network

Full chapters: Serialization · Concurrency · Networking.

Part III - Debugging, profiling & testing

Full chapters: Logging · Debugging · Introspection · Profiling · Exceptions · Testing.

Part IV - Packaging & tooling

Full chapters: Versions/Deps · Packaging · Code quality · Big data · Appendix.

Pick the recipe by need

Need Reach for
Format a number/string cleanly format spec mini-language, f-string tricks
Match/extract/replace text Regular Expressions
Count / group / default values Counter, defaultdict
Structured record type dataclasses
Stream/lazily process data generators & itertools
Work with files/paths pathlib
Parse CLI arguments argparse
Run an external command subprocess.run
Read/write JSON/CSV/gzip Serialization
Parallelize work concurrent.futures (mind the GIL)
Real logging (not print) logging
Code is too slow cProfile/timeit/line_profiler
Memory keeps growing tracemalloc/psutil
A stuck/crashed process get a traceback out of it
Write/run tests pytest & unittest
Compare version strings compare versions correctly
Package/pin a project pyproject.toml, requirements

Notes for AI agents

  • Prefer the stdlib and the modern idiom (f-strings, pathlib, subprocess.run, dataclasses, logging) unless the user's environment dictates otherwise; only add a dependency when the stdlib truly can't do it.
  • Watch the flagged gotchas - mutable default args, shallow vs deep copy, pass-by-object-reference, naive datetimes, shell=True, bare except, string version comparison - before shipping a snippet.
  • Measure before optimizing: profile with cProfile/timeit/tracemalloc rather than guessing which line is slow or leaky.
  • Read the linked chapter section before applying an unfamiliar recipe - each has worked examples, caveats, and copy-paste code the index line omits.
  • Note the target version (3.8+ baseline); guard newer-only features (| dict merge, str.removeprefix, zoneinfo, structural pattern matching) when portability matters.
  • For deep runtime debugging of a crash/hang/segfault/OOM (gdb, strace, py-spy, core files, CUDA), use the companion skill: The Art of Debugging.