Project Style Guideο
π¨ This guide explains the coding and formatting conventions used in the project, and how to keep things consistent when adding or editing code.
It combines Python code conventions, documentation standards, and metadata formatting guidelines.
π§βπ» Python Coding Conventionsο
β Function Definitionsο
Use type hints for all parameters and return types.
def convert_units_var(
values: xr.DataArray,
current: str,
target: str,
) -> xr.DataArray: ...
π Namingο
Functions and variables:
snake_caseConstants (e.g., in xarray datasets):
ALL_CAPS
π Docstringsο
Use NumPy-style docstrings
Required for all functions.
Sections include:
Parameters,Returns,Notes, (optionallyExamples,References)
Example:
def convert_units_var(
var_values: xr.DataArray,
current_unit: str,
new_unit: str,
unit_conversion: dict = unit_conversion,
) -> xr.DataArray:
"""
Convert variable values from one unit to another.
Parameters
----------
var_values : xr.DataArray
The numerical values to convert.
current_unit : str
Unit of the original values.
new_unit : str
Desired unit for the output values.
unit_conversion : dict, optional
Dictionary containing conversion factors between units.
Returns
-------
xr.DataArray
Converted values in the desired unit.
Notes
-----
If no valid conversion is found, the original values are returned unchanged.
"""
π NumPy-style docstrings render cleanly in Sphinx documentation (e.g. on Read the Docs). See NumPy docstring style.
π‘ Alternatives like Google-style and reST-style docstrings are also valid with Sphinx if configured properly.
π Dataset & Metadata Conventionsο
𧬠Variable Namesο
ALL CAPITALS for variables and dimensions:
TRANSPORT,DEPTH,TIMEKeep short and unambiguous
π§Ύ Attributesο
Follow OceanGliders OG1 format
Use
units,long_name,commentAvoid placing units in variable names β use attributes instead
π Consistencyο
Units are checked and converted using helper functions
Time is standardized using utility functions across the project
π Automating Formattingο
The project uses ruff for linting, formatting, and type-annotation checks (via its ANN rules), plus pytest for tests. Style is enforced by the CI lint job, which runs ruff check . and ruff format --check . on every pull request. See the linting & formatting guide for the commands to run locally.
π Optional: VSCode Setup for Auto-formattingο
To automatically format code when you save a file, add this to your Workspace Settings (.vscode/settings.json) using the Ruff VSCode extension:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
You can also add a VSCode task to format manually:
{
"label": "Format Code",
"type": "shell",
"command": "ruff format . && ruff check . --fix",
...
}
Then bind it to a keyboard shortcut (e.g.
Cmd+Shift+R) or run it from the command palette.
π‘ Future Considerationsο
Use Sphinx for auto-generating documentation (already partially configured)
β By following these conventions, your code will integrate smoothly with the rest of the project β clean, consistent, and ready for collaboration.