feat: Add context manager support to DataSet (closes #67) - #70
Open
sahiljhawar wants to merge 1 commit into
Open
feat: Add context manager support to DataSet (closes #67)#70sahiljhawar wants to merge 1 commit into
sahiljhawar wants to merge 1 commit into
Conversation
- DataSet now supports `__enter__`/`__exit__` (and `__del__`) alongside the existing direct-instantiation usage, so callers can choose `with DataSet(...) as ds:` when they need lazily opened .nc file handles released deterministically instead of relying on garbage collection. - `close()` closes any `xr.Dataset` groups opened by `load_netcdf_data_lazy` and tracked via a new `_open_resources` list, and also materializes any variable that is still a lazy, unmaterialized `xr.Variable` into a plain array. The latter fixes a real bug: variables cached but never individually accessed (e.g. loading "Flux" also caches "alpha_local" from the same file) would silently reopen the file via xarray's `CachingFileManager` on first access, even after `close()` had already run, leaving the file locked for writing. - Adds unit tests covering the context manager, `close()` idempotency, the reference-cycle/lazy-materialization regression, and the overwrite-while-open `PermissionError` this all stems from. - Adds a new tutorial notebook `6_dataset_context_manager.ipynb` demonstrating both usage styles.
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.
DataSet now supports
__enter__/__exit__(and__del__) alongside the existing direct-instantiation usage, so callers can choosewith DataSet(...) as ds:when they need lazily opened .nc file handles released deterministically instead of relying on garbage collection.close()closes anyxr.Datasetgroups opened byload_netcdf_data_lazyand tracked via a new_open_resourceslist, and also materializes any variable that is still a lazy, unmaterializedxr.Variableinto a plain array. The latter fixes a real bug: variables cached but never individually accessed (e.g. loading "Flux" also caches "alpha_local" from the same file) would silently reopen the file via xarray'sCachingFileManageron first access, even afterclose()had already run, leaving the file locked for writing.Adds unit tests covering the context manager,
close()idempotency, the reference-cycle/lazy-materialization regression, and the overwrite-while-openPermissionErrorthis all stems from.Adds a new tutorial notebook
6_dataset_context_manager.ipynbdemonstrating both usage styles.