Skip to content

Clean up and refactor Cbh.py#54

Merged
pnorton-usgs merged 6 commits into
developmentfrom
refactor/cbh-cleanup
May 11, 2026
Merged

Clean up and refactor Cbh.py#54
pnorton-usgs merged 6 commits into
developmentfrom
refactor/cbh-cleanup

Conversation

@pnorton-usgs
Copy link
Copy Markdown
Member

Clean up and refactor Cbh.py

Summary

Modernizes type hints, refactors the long __init__ into focused methods, improves error handling, and cleans up code style in Cbh.py.

Changes

Modernize type hints

  • Add from __future__ import annotations
  • Replace typing.Dict, List, Optional, Union with built-in generics and X | Y syntax
  • Remove unused typing imports
  • Fix verbose and engine parameter types: Optional → plain types

Refactor __init__

  • Extract _normalize_paths() for path handling logic
  • Extract _read_netcdf(), _read_zarr(), _read_ascii() from __init__
  • __init__ is now a clear orchestrator (~30 lines vs ~100)
  • Add default case to engine match that raises ValueError for unknown engines (previously left ds unbound)
  • _read_zarr() now raises ValueError instead of printing errors and leaving ds unbound

Use context manager in write_ascii

  • Replace open()/close() with with open() context manager
  • Ensures file is properly closed even if an exception occurs

Add __repr__

  • Returns Cbh(src_path=[...], variables=N) for debugging

Move module-level constants to class constants

  • var_crosswalk_VAR_CROSSWALK
  • temp_units_TEMP_UNITS
  • precip_units_PRECIP_UNITS
  • NA_VALS_DEFAULT_NA_VALS_DEFAULT
  • DATA_SEP_DATA_SEP
  • Better encapsulation: only used by the Cbh class

Code cleanup

  • Remove commented-out dead code
  • Replace 6 repeated pd.to_numeric() calls with a loop

Testing

All 290 tests pass.

- Add 'from __future__ import annotations' for runtime compatibility
- Replace typing.Dict, List, Optional, Union with built-in generics
  and X | Y syntax
- Remove unused typing imports (Dict, List, Optional, Sequence, Union)
- Fix verbose and engine parameter types: Optional -> plain types
  (None was never a meaningful value for either)
- Extract _normalize_paths() for path handling logic
- Extract _read_netcdf(), _read_zarr(), _read_ascii() from __init__
- Add default case to engine match that raises ValueError for unknown
  engines (previously would silently leave 'ds' unbound)
- Change _read_zarr() to raise ValueError instead of printing errors
  and leaving 'ds' unbound
- __init__ is now a clear orchestrator (~30 lines vs ~100)
- Replace open()/close() with 'with open()' context manager
- Ensures file is properly closed even if an exception occurs
  during writing
- Move var_crosswalk, temp_units, precip_units, NA_VALS_DEFAULT,
  DATA_SEP from module level to class constants (_VAR_CROSSWALK,
  _TEMP_UNITS, _PRECIP_UNITS, _NA_VALS_DEFAULT, _DATA_SEP)
- Better encapsulation: these are only used by the Cbh class
- Remove commented-out debug prints, old variable references, and
  alternative implementations
- Replace 6 repeated pd.to_numeric() calls with a loop over time
  columns
@pnorton-usgs pnorton-usgs self-assigned this May 11, 2026
@pnorton-usgs pnorton-usgs merged commit 4f117a1 into development May 11, 2026
7 checks passed
@pnorton-usgs pnorton-usgs deleted the refactor/cbh-cleanup branch May 11, 2026 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant