1.6 KiB
1.6 KiB
Systems Python Guidelines
CLI tools and system utilities
- Prefer
argparseortyperfor CLIs, not ad-hocsys.argvparsing. - Always provide:
--helpwith clear descriptions- examples in docstrings or README
- reasonable defaults and safe behavior.
- For non-trivial tools, separate:
- CLI entrypoint (
main()or Typer app) - core logic in importable modules (no side effects on import).
- CLI entrypoint (
Logging and diagnostics
- Use the
loggingmodule instead ofprintfor diagnostics. - Provide configurable log levels; default to
INFOfor CLI tools. - For structured logging, prefer JSON-compatible formats when integrating with centralized logging.
- Avoid logging secrets, access tokens, and PII.
Configuration
- Prefer explicit configuration (CLI args, env vars, config files) over hidden magic.
- Use
pydanticordataclassesfor structured config when appropriate. - For multi-environment setups, support:
- baseline config
- overrides via env or separate files.
Packaging and layout
- Use modern packaging (
pyproject.tomlwithsetuptools/hatch/poetry). - Standard layout:
src/<package_name>/...tests/- minimal, well-documented dependencies.
- For tools that may be reused, publishable or installable layout is preferred even in internal projects.
Error handling
- Fail fast on invalid configuration or obvious misuse.
- Wrap external calls (filesystem, network, subprocesses) with clear error messages.
- Use custom exception types where it improves clarity.
- Never silently swallow exceptions; at minimum, log with context.