Clean up and refactor Cbh.py#54
Merged
Merged
Conversation
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 inCbh.py.Changes
Modernize type hints
from __future__ import annotationstyping.Dict,List,Optional,Unionwith built-in generics andX | Ysyntaxtypingimportsverboseandengineparameter types:Optional→ plain typesRefactor
__init___normalize_paths()for path handling logic_read_netcdf(),_read_zarr(),_read_ascii()from__init____init__is now a clear orchestrator (~30 lines vs ~100)ValueErrorfor unknown engines (previously leftdsunbound)_read_zarr()now raisesValueErrorinstead of printing errors and leavingdsunboundUse context manager in
write_asciiopen()/close()withwith open()context managerAdd
__repr__Cbh(src_path=[...], variables=N)for debuggingMove module-level constants to class constants
var_crosswalk→_VAR_CROSSWALKtemp_units→_TEMP_UNITSprecip_units→_PRECIP_UNITSNA_VALS_DEFAULT→_NA_VALS_DEFAULTDATA_SEP→_DATA_SEPCode cleanup
pd.to_numeric()calls with a loopTesting
All 290 tests pass.