These tools help SDK Code Example developers' integration with internal docs tooling. This is mainly in the form of .doc_gen SOS metadata validation, with other tools as necessary.
SDK Code Examples tools team uses Python for our tools because of its cross-platform runtime and broad knowledge base.
- Use Python latest for tooling (this is not the same rule as AWS SDK for Python, be aware).
- Our “standard library” adds
flake8
,black
,pyyaml
,yamale
, andrequests
.- Current versions are in base_requirements.txt.
- Run
.tools/verify_python.py
to ensure python tools are configured as expected.- This will install our “standard library” to site_packages and ensure that the python version is at least 3.(current - 1).
- Use a venv in
.venv
if the project needs packages beyond our standard library.- Include those libraries in the
requirements.txt
. - Copy
.tools/base-requirements.txt
to the tool’s folder to seedrequirements.txt
if you like. - With an active venv, run
pip freeze > requirements.txt
to update the tool’s requirements.
- Include those libraries in the
- Keep tools self executable.
- Mark them executable (
chmod a+x <script.py>
in *nix). - Add a shebang
#!/usr/bin/env python3
. - Use
__name__ == "__main__"
check that delegates to amain
function.
- Mark them executable (
- Use f-strings for string building.
- Use
logging
to write diagnostics to the console. - Format using Black with default settings.
- Lint using
flake8
with overrides for Black default settings. Treat Warnings as errors. - Verify type annotations using mypy.
- Type annotations are strongly recommended.
- Run tests using pytest.
- Parse arguments using argparse.
- All scripts must run headless, without user interaction.
- Use pathlib for files and paths.
- Prefer os.scandir to os.listdir.
- Parse & dump yaml using PyYAML (imports from
yaml
).- Validate yaml schemas using
yamale
.
- Validate yaml schemas using
- When working with dates, import from
datetime
and always include a timezone (usuallytimezone.utc
). - Prefer data classes or immutable tuples for base data types.
- Use requests for web calls, but prefer a native wrapper (like boto3 or pygithub) when available.
- If you http/2, you might consider httpx.
- Use subprocess for system calls, but prefer a native wrapper (like gitpython) when available.
- When reading project files, use
encoding="utf-8-sig"
, as many.cs
files have a BOM.
Python 3.12:
- PEP 701 -- More flexible f-string parsing.
- PEP 695 -- New type annotations syntax for generic classes.
Python 3.11:
- PEP 654 -- Exception Groups and except*.
Python 3.10:
Python 3.9:
- PEP 585, Type Hinting Generics In Standard Collections.
- PEP 602, Python adopts a stable annual release cadence.
Python 3.8
Python 3.7
- PEP 557, Data Classes.