# 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. ```python def convert_units( values: xr.DataArray, current: str, target: str, ) -> xr.DataArray: ... ``` ### ๐Ÿ Naming - Functions and variables: `snake_case` - Constants (e.g., in xarray datasets): `ALL_CAPS` ### ๐Ÿ“ Docstrings - Use **NumPy-style** docstrings - Required for all functions. - Sections include: `Parameters`, `Returns`, `Notes`, (optionally `Examples`, `References`) Example: ```python 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](https://numpydoc.readthedocs.io/en/latest/format.html). > > ๐Ÿ’ก Alternatives like [Google-style](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) and [reST-style](https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#info-field-lists) docstrings are also valid with Sphinx if configured properly. --- ## ๐Ÿ—‚ Dataset & Metadata Conventions ### ๐Ÿงฌ Variable Names - Variable naming conventions are currently inconsistent across the codebase - We need to verify and align with [OceanSites variable naming recommendations](https://www.ocean-ops.org/oceansites/data/index.html) - Mix of `ALL_CAPS` (`TIME`, `DEPTH`) and lowercase (`temperature`, `pressure`) currently in use - Keep names short and unambiguous ### ๐Ÿงพ Attributes - Follow [OceanSites format conventions](https://www.ocean-ops.org/oceansites/data/index.html) - Use `units`, `long_name`, `comment` - Avoid 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 tools like `black`, `ruff`, and `pytest` to enforce style, linting, and test consistency. These are integrated into the workflow using pre-commit hooks. **Pre-commit tools used:** ```bash black . # Format code with black ruff check . # Run ruff linter ruff check . --fix # Auto-fix issues where possible pre-commit run --all-files # Run all pre-commit hooks codespell # Check for spelling errors ``` You don't need to run them manually, but setting up pre-commit ensures your code follows project standards automatically. --- ## ๐Ÿ›  Optional: VSCode Setup for Auto-formatting To automatically format code when you save a file, add this to your **Workspace Settings** (`.vscode/settings.json`): ```json { "python.formatting.provider": "black", "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true } } ``` You can also add a VSCode task to format manually: ```json { "label": "Format Code", "type": "shell", "command": "black . && ruff check . --fix", ... } ``` - Then bind it to a keyboard shortcut (e.g. `Cmd+Shift+R`) or run it from the command palette. --- ## ๐Ÿ’ก Future Considerations - Add `mypy` for static type checking - 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.